#!/bin/sh

#------------------------------ global defines --------------------------------
export TEXTDOMAINDIR=/usr/share/locale
export TEXTDOMAIN=nimol

#------------------------------ local defines ---------------------------------
# command alias
ARP="/sbin/arp"
AWK="/bin/awk"
CAT="/bin/cat"
CHMOD="/bin/chmod"
CP="/bin/cp"
ECHO="/bin/echo -e"
EXPORTFS="/usr/sbin/exportfs"
EGREP="/usr/bin/egrep"
GETTEXT="/usr/bin/gettext"
GREP="/bin/grep"
GZIP="/usr/bin/gzip"
HOST="/usr/bin/host"
HOSTNAME="/bin/hostname"
IFCONFIG="/sbin/ifconfig"
if [[ -s /sbin/iptables ]]; then
    IPTABLES="/sbin/iptables"
else
    IPTABLES="/usr/sbin/iptables"  # HMC
fi
LN="/bin/ln"
LS="/bin/ls"
LSLPP="/usr/bin/lslpp"
MKDIR="/bin/mkdir"
MOUNT="/bin/mount"
MV="/bin/mv"
PS="/bin/ps -ef"
RM="/bin/rm"
RPM="/bin/rpm"
RSH="/usr/bin/rsh"
SED="/usr/bin/sed"
SORT="/usr/bin/sort"
TAR="/bin/tar"
TEE="/usr/bin/tee"
UMOUNT="/bin/umount"
XARGS="/usr/bin/xargs"

if [[ -s /etc/rc.d/init.d/xinetd ]]; then
    INITD_DIR="/etc/rc.d/init.d"
else
    INITD_DIR="/etc/init.d"   # HMC
fi
XINETD="${INITD_DIR}/xinetd"
DHCPD="${INITD_DIR}/dhcpd"
SYSLOG="${INITD_DIR}/syslog"
PORTMAP="${INITD_DIR}/portmap"
NFS="${INITD_DIR}/nfs"
NFSSERVER="${INITD_DIR}/nfsserver"    # does not exist on Redhat
NIMOL_BOOTREPLYD_CONF="/etc/nimoltab"
NIMOL_BOOTREPLYD_VAR="/var/nimol_bootreplyd"
NIMOL_BOOTREPLYD_START="/usr/sbin/nimol_bootreplyd -l -d -f $NIMOL_BOOTREPLYD_CONF"
NIMOL_BOOTREPLYD_PID="$NIMOL_BOOTREPLYD_VAR/pid"

# resources
SPOT="SPOT"
ISPOT="ispot.tar.Z"
ISPOT_TAR="ispot.tar"
DISPOT="D-ispot.tar.Z"
DISPOT_TAR="D-ispot.tar"
MKSYSB="mksysb"
MKSYSBFF="mksysb.bff"
BOOTI="booti.chrp.mp.ent"
BOOTIZ="booti.chrp.mp.ent.Z"
LPP_SOURCE="lpp_source"
RESOLV_CONF="resolv.conf"
CUST_SCRIPT="cust.script"
IMAGE_DATA="image.data"
BOSINST_DATA="bosinst.data"

# other
TFTPBOOT="/tftpboot"
NIMOL_TFTPBOOT=
TMPDIR=/var/tmp/nimol.$$
PERM_TMP=/var/tmp/nimol_original
NIMOL_CONF=/etc/nimol.conf
CMD_ERR=
ERR=/var/tmp/nimol.err.$$
IP_OUT=
SYSLOGD_VAR=
SUBNET=
NIMOL_INTERFACE=
AIX_RES_ROOT="/opt/aix-res"
DVD_RPM_LOC="RPMS/linux/aix-res-*.rpm"

# list of packages to copy into lpp_source by default
SIMAGES_OPTIONS="./RPMS/ppc/cdrecord.aix5.2.ppc.rpm
./RPMS/ppc/mkisofs.aix4.3.ppc.rpm
./installp/ppc/xlC.msg.en_US.cpp
./installp/ppc/xlC.cpp
./installp/ppc/xlC.aix50
./installp/ppc/xlC.rte
./installp/ppc/rsct.msg.EN_US
./installp/ppc/rsct.msg.en_US
./installp/ppc/rsct.core
./installp/ppc/invscout.com
./installp/ppc/rpm.rte
./installp/ppc/printers.rte
./installp/ppc/perl.rte
./installp/ppc/perl.libext
./installp/ppc/perfagent.tools
./installp/ppc/invscout.rte
./installp/ppc/invscout.ldb
./installp/ppc/infocenter.man.EN_US.commands
./installp/ppc/ifor_ls.base
./installp/ppc/bos.rte.tty
./installp/ppc/bos.rte.shell
./installp/ppc/bos.rte.serv_aid
./installp/ppc/bos.rte.security
./installp/ppc/bos.rte.printers
./installp/ppc/bos.rte.odm
./installp/ppc/bos.rte.net
./installp/ppc/bos.rte.misc_cmds
./installp/ppc/bos.rte.methods
./installp/ppc/bos.rte.man
./installp/ppc/csm.msg.en_US
./installp/ppc/csm.msg.EN_US
./installp/ppc/csm.dsh
./installp/ppc/csm.diagnostics
./installp/ppc/csm.diagnostics
./installp/ppc/csm.core
./installp/ppc/csm.core
./installp/ppc/csm.client
./installp/ppc/bos.txt
./installp/ppc/bos.txt
./installp/ppc/bos.terminfo
./installp/ppc/bos.terminfo
./installp/ppc/bos.sysmgt
./installp/ppc/bos.suma
./installp/ppc/bos.rte
./installp/ppc/bos.rte.lvm
./installp/ppc/bos.rte.loc
./installp/ppc/bos.rte.libpthreads
./installp/ppc/bos.rte.libcur
./installp/ppc/bos.rte.libcfg
./installp/ppc/bos.rte.libc
./installp/ppc/bos.rte.jfscomp
./installp/ppc/bos.rte.install
./installp/ppc/bos.rte.iconv
./installp/ppc/bos.rte.filesystem
./installp/ppc/bos.rte.edit
./installp/ppc/bos.rte.diag
./installp/ppc/Tivoli_Management_Agent.client
./installp/ppc/bos.rte.ILS
./installp/ppc/bos.rte.SRC
./installp/ppc/bos.rte.aio
./installp/ppc/bos.rte.boot
./installp/ppc/bos.rte.bosinst
./installp/ppc/bos.rte.archive
./installp/ppc/bos.rte.bind_cmds
./installp/ppc/bos.rte.commands
./installp/ppc/bos.rte.control
./installp/ppc/bos.rte.cron
./installp/ppc/bos.pmapi
./installp/ppc/bos.perf
./installp/ppc/bos.net
./installp/ppc/bos.msg.en_US
./installp/ppc/bos.msg.en_US
./installp/ppc/bos.mh
./installp/ppc/bos.loc.iso
./installp/ppc/bos.iconv
./installp/ppc/bos.help.msg.en_US
./installp/ppc/bos.help.msg.en_US
./installp/ppc/bos.diag
./installp/ppc/bos.cdmount
./installp/ppc/bos.alt_disk_install
./installp/ppc/bos.adt
./installp/ppc/bos.adt
./installp/ppc/bos.acct
./installp/ppc/bos.64bit
./installp/ppc/bos.mp64
./installp/ppc/bos.mp
./installp/ppc/bos
./installp/ppc/devices*
./installp/ppc/bos.vendor.profile
./installp/ppc/.toc"

################################################################################
#
#  err - prints an error msg and exits
#
#  parameters:
#      $1  - return code
#      $2  - error message format
#      $3+ - parameters to message
#
################################################################################
function err {

    typeset -i rc=$1
    typeset format=$2
    shift; shift

    printf "$(${GETTEXT} -se "%s ERROR: \n")" "${0##*/}" >&2
    printf "$(${GETTEXT} -se "$format")\n" "$@" >&2

    # if this was an error from a command, display the error output
    [[ -n $CMD_ERR && -s $ERR ]] && ${CAT} $ERR >&2

    undo

    exit $rc

}


################################################################################
#
#  warn - prints a warning message without exiting
#
#  parameters:
#      $1 - warning message format
#      $2+ - parameters to message
#
################################################################################
function warn {
    typeset format=$1
    shift

    printf "$(${GETTEXT} -se "%s WARNING: \n")" "${0##*/}"
    printf "$(${GETTEXT} -se "$format")\n" "$@"
}


################################################################################
#
#  cmd_err - prints an error msg, prints command error message, and exits
#
#  parameters:
#      $1 - return code
#      $2 - failed command
#
################################################################################
function cmd_err {

    CMD_ERR=yes
    typeset errcode=$1
    shift

    err $errcode "error from command %s\n" "$@"
}

################################################################################
#
#  cmd_warn - prints a warning message without exiting and command error message
#
#  parameters:
#      $1 - command
#
################################################################################
function cmd_warn {

    warn "error from command %s\n" "$@"

    [[ -s $ERR ]] && ${CAT} $ERR >&2
}

################################################################################
#
#  msg - prints a message without exiting
#
#  parameters:
#      $1 - message format
#      $2 - message parameters
#
################################################################################
function msg {

    typeset format=$1
    shift

    printf "$(${GETTEXT} -se "%s MESSAGE: \n")" "${0##*/}"

    printf "$(${GETTEXT} -se "$format")\n" "$@" >&2

}

################################################################################
#
#  usage - prints a usage message and exits
#
#  parameters:
#      $1 - message format
#      $2 - message parameters
#
################################################################################
function usage {

    typeset format=$1
    shift

    printf "$(${GETTEXT} -se "%s USAGE: \n")" "${0##*/}"

    printf "$(${GETTEXT} -se "$format")\n" "$@" >&2

    exit 1
}

################################################################################
#
#  err_signal - error signal handler
#
#  parameters:
#
################################################################################
function err_signal {

    trap "" 1 2 11 15

    err 1 "error signal received"

} # end of err_signal 


################################################################################
#
#  err_signal - error signal handler
#
#  parameters:
#      $1 - hostname
#
################################################################################
function get_ipaddr_from_hostname {

IP_OUT=
host=$1

if [[ -s /etc/hosts ]]
then
    IP_OUT=$( ${AWK} '{if ($2 == hostname || $3 == hostname) print $1;}' hostname=${host} /etc/hosts )
fi

# look for the first ip address that isn't 127.0.0.1
for ip in $IP_OUT
do
    if [[ $ip != "127.0.0.1" ]]
    then
	IP_OUT=$ip
	break
    fi
done

# if the only ip address is 127.0.0.1, then null it out
[[ $IP_OUT = "127.0.0.1" ]] && IP_OUT=
[[ $IP_OUT = "#"* ]] && IP_OUT=

if [[ -z ${IP_OUT} ]]
then
	# nothing found in hosts, try dns lookup
	HOST_OUT=$( ${HOST} $host )

	# verify the output is an ip address
	IP_OUT=$( echo $HOST_OUT | ${AWK} '{ print $4 }' )
	ip1=$( echo ${IP_OUT} | ${AWK} 'BEGIN { FS="." } {print $1}' |grep "[0123456789]\+")
	ip2=$( echo ${IP_OUT} | ${AWK} 'BEGIN { FS="." } {print $2}' |grep "[0123456789]\+")
	ip3=$( echo ${IP_OUT} | ${AWK} 'BEGIN { FS="." } {print $3}' |grep "[0123456789]\+")
	ip4=$( echo ${IP_OUT} | ${AWK} 'BEGIN { FS="." } {print $4}' |grep "[0123456789]\+")
	
	if [[ -n ${ip1} && -n ${ip2} && -n ${ip3} && -n ${ip4} ]]
	then
	    # the ip address looks good
	    :
	else
	    IP_OUT=
	fi
fi

return 0

}

################################################################################
#
#  get_syslogd_param_var
#
#  parameters:
#
################################################################################
function get_syslogd_param_var {

    [[ ! -s /etc/sysconfig/syslog ]] && err 1 "The file %s does not exist.\n" "/etc/sysconfig/syslog"
    
    # add the NIMOL subnet if it does not exist
    if ${GREP} "SYSLOGD_OPTIONS" /etc/sysconfig/syslog >/dev/null 2>/dev/null
    then
	SYSLOGD_VAR="SYSLOGD_OPTIONS"
    elif ${GREP} "SYSLOGD_PARAMS" /etc/sysconfig/syslog >/dev/null 2>/dev/null
    then
	SYSLOGD_VAR="SYSLOGD_PARAMS"
    else
	err 1 "Unable to determine the \"%s\" for \"%s\".\n" "parameters variable" "/etc/sysconfig/syslog"
    fi

}

################################################################################
#
#  get_subnet
#
#  parameters:
#
################################################################################
function get_subnet {

    typeset ip=$1
    typeset nm=$2

    #echo "ip address: $ip, netmask: $nm"
    ip1=$( echo ${ip} | ${AWK} 'BEGIN { FS="." } {print $1}' )
    ip2=$( echo ${ip} | ${AWK} 'BEGIN { FS="." } {print $2}' )
    ip3=$( echo ${ip} | ${AWK} 'BEGIN { FS="." } {print $3}' )
    ip4=$( echo ${ip} | ${AWK} 'BEGIN { FS="." } {print $4}' )

    nm1=$( echo ${nm} | ${AWK} 'BEGIN { FS="." } {print $1}' )
    nm2=$( echo ${nm} | ${AWK} 'BEGIN { FS="." } {print $2}' )
    nm3=$( echo ${nm} | ${AWK} 'BEGIN { FS="." } {print $3}' )
    nm4=$( echo ${nm} | ${AWK} 'BEGIN { FS="." } {print $4}' )

    sm1=$( echo $(( $ip1 & $nm1 )) )
    sm2=$( echo $(( $ip2 & $nm2 )) )
    sm3=$( echo $(( $ip3 & $nm3 )) )
    sm4=$( echo $(( $ip4 & $nm4 )) )

    SUBNET="${sm1}.${sm2}.${sm3}.${sm4}"

}


################################################################################
#
#  mk_tftp
#
#  parameters:
#
################################################################################
function mk_tftp {

    typeset tftp_file="/etc/xinetd.d/tftp"

    $ECHO "# default: off" >${tftp_file} || cmd_err 1 "creating ${tftp_file}"
    $ECHO "# description: tftp service is provided primarily for booting or when a" >>${tftp_file}
    $ECHO "#       router need an upgrade. Most sites run this only on machines acting as" >>${tftp_file}
    $ECHO "#       \"boot servers\"." >>${tftp_file}
    $ECHO "service tftp" >>${tftp_file}
    $ECHO "{" >>${tftp_file}
    $ECHO "        socket_type             = dgram" >>${tftp_file}
    $ECHO "        protocol                = udp" >>${tftp_file}
    $ECHO "        wait                    = yes" >>${tftp_file}
    $ECHO "        user                    = root" >>${tftp_file}
    $ECHO "        server                  = /usr/sbin/in.tftpd" >>${tftp_file}
    $ECHO "        server_args             = -s /tftpboot" >>${tftp_file}
    $ECHO "        disable                 = yes" >>${tftp_file}
    $ECHO "}" >>${tftp_file}
    
    [[ ! -s ${tftp_file} ]] && \
	err 1 "The file %s does not exist.\n" "${tftp_file}"

    msg "Created %s.\n" "${tftp_file}"
}



################################################################################
#
#  get_nimol_interface
#
#  parameters:
#
################################################################################
function get_nimol_interface {

    if_list=
    good_if_list=
    if1=
    if2=

    if_list=$( ${IFCONFIG} | ${AWK} '{ print $1 }' )

    for if1 in $if_list
    do
        if ${IFCONFIG} $if1 >/dev/null 2>/dev/null
	then
	    good_if_list="${good_if_list} ${if1}"
	fi
    done

    for if2 in $good_if_list
    do
        if ${IFCONFIG} $if2 | ${GREP} ${NIMOL_SERVER_IP} >/dev/null 2>/dev/null
	then
	    NIMOL_INTERFACE=$if2
	    break
	fi
    done

    if [[ -z ${NIMOL_INTERFACE} ]]; then
	err 1 "Couldn't determine the interface for %s.\n" "${NIMOL_SERVER_IP}"
    fi
}

