declare -F msgutil_r &>/dev/null  || function msgutil_r {
   local logserver=$1
   local msgtype=$2
   local msgstr=$3
   local logfile=$4
   local logtag=$5

   if [ -z "$msgtype"  ]; then
      msgtype="debug"
   fi

   if [ -z "$logtag" ]; then
      logtag="xcat"
   fi

   if [ -n "$logserver" ];then
      logger -n $logserver -t $logtag -p local4.$msgtype "$msgstr"
      if [ "$?" != "0" ];then
         exec 3<>/dev/udp/$logserver/514 >/dev/null;logger -s -t $logtag -p local4.$msgtype "$msgstr" 2>&3
         if [ "$?" != "0" ];then
            logger -s -t $logtag -p local4.$msgtype "$msgstr" 2>&1|nc $logserver 514 >/dev/null 2>&1
            if [ "$?" != "0" ];then
               logger -t $logtag -p local4.$msgtype "$msgstr"
            fi
         fi
      fi
   else
       logger -t $logtag -p local4.$msgtype "$msgstr"
   fi
   if [ -n "$logfile"  ]; then
      local logdir="$(dirname $logfile)"
      if [ ! -d "$logdir" ]; then
         mkdir -p "$logdir"
         touch "$logfile"
      fi

      echo "$(date) [$msgtype]: $logtag: $msgstr" >> $logfile
   fi

}

declare -F msgutil &>/dev/null  || function msgutil {
    msgutil_r "" "$@"
}

declare -F xcat_enable_active_nm_autoconnect &>/dev/null || function xcat_enable_active_nm_autoconnect {
    local log_changes="${1:-}"

    command -v nmcli >/dev/null 2>&1 || return 0

    nmcli -g NAME,STATE con show | \
        awk -F: '$1 != "lo" && $2 == "activated" {print $1}' | \
        while IFS= read -r con_name
        do
            [ -n "$con_name" ] || continue
            if [ -n "$log_changes" ]; then
                case "$XCATDEBUGMODE" in
                "1"|"2")
                    msgutil_r "$MASTER_IP" "info" "set connection $con_name to be activated on system boot" "/var/log/xcat/xcat.log"
                    ;;
                esac
            fi
            nmcli con mod "$con_name" connection.autoconnect yes
        done
}

declare -F xcat_download_postscripts &>/dev/null || function xcat_download_postscripts {
    local server="$1"
    local install_dir="${2:-/install}"
    local postroot="${3:-/xcatpost}"
    local log_file="${4:-/tmp/wget.log}"

    export LANG=C
    wget -l inf -N -r --waitretry=10 --random-wait --retry-connrefused -e robots=off -nH --cut-dirs=2 --reject "index.html*,post.xcat.ng,post.xcat.rhels10" --no-parent -t 20 -T 60 "http://${server}${install_dir}/postscripts/" -P "$postroot" 2> "$log_file"
}

declare -F set_sles11_uefi_bootloader &>/dev/null || function set_sles11_uefi_bootloader {
    local cmdline="${1:-/proc/cmdline}"
    local profile="${2:-/tmp/profile/modified.xml}"

    if grep -E 'install=.*sles11' "$cmdline" >/dev/null 2>&1; then
        # SLES 11 AutoYaST keeps the template's legacy MBR bootloader
        # location unless the UEFI path explicitly selects elilo.
        sed -i -e '/<lba_support /d' \
            -e '/<linear /d' \
            -e 's!<location>mbr</location>!<loader_type>elilo</loader_type>!' \
            "$profile"
    fi
}
