#!/bin/sh

. /usr/lib/nimol_lib


#------------------------------ local defines ---------------------------------
TARGET_DIR=/export/aix
DONT_CONFIG=
DONT_COPY=
NIMOL_SERVER=
NIMOL_SERVER_IP=
SOURCE_SYS=
EXTRACT_SUCCESSFUL=

# variables set based on the parameters
COMM_METH=
ONLY_CONFIG_NIMOL=
SOURCE_DIR=
DONT_GLOBAL_EXPORT=
LIST=
LABEL=
REMOVE_LABEL=
NIMOL_SERVER=
CONFIG_SYSLOG=
UNCONFIG_NIMOL=
AUX_SOURCE_DIR=
ALT_BID=

# Variables for undo function
MNT_SUCCESS=
MK_CONF_SUCCESS=
TFTP_DISABLED=
TFTP_ENABLED=
SYSLOG_SUCCESS=
NIMOL_LOG_SUCCESS=
DHCP_SUCCESS=
DHCPD_CONF_CREATED=
DHCPD_STARTED=
DHCPD_RESTARTED=
DHCPD_INTERFACE_ADDED=
CP_BOOTI_SUCCESS=
CP_TFTP_SUCCESS=
CP_SPOT_SUCCESS=
CP_MKSYSB_SUCCESS=
NFS_EXPORT_SUCCESS=
NFS_SUCCESS=
NFSSERVER_SUCCESS=
PORTMAP_SUCCESS=
MK_PERM_TMP_SUCCESS=
MK_TDIR_SUCCESS=
IPTABLES_SUCCESS=
STARTED_BOOTREPLY=
AIX_VERSION=
RPM_NAME=
FROM_CD=

################################################################################
#
#  undo - undo changes made
#
#  parameters:
#
################################################################################
function undo {

msg "Running undo...\n"

if [[ -n $MK_CONF_SUCCESS ]]; then
    ${RM} -f $NIMOL_CONF
fi

if [[ -n $TFTP_DISABLED && -n $TFTP_ENABLED ]]; then

    ${MV} ${PERM_TMP}/tftp /etc/xinetd.d/tftp

    msg "Copied back the original file %s.\n" "/etc/xinetd.d/tftp"

    $XINETD restart || warn "Failed to restart %s.\n" "xinetd"
fi

if [[ -n $SYSLOG_SUCCESS ]]; then

    ${MV} ${PERM_TMP}/syslog /etc/sysconfig/syslog

    msg "Copied back the original file %s.\n" "/etc/sysconfig/syslog"
fi

if [[ -n $NIMOL_LOG_SUCCESS ]]; then

    ${MV} ${PERM_TMP}/syslog.conf /etc/syslog.conf

    msg "Copied back the original file %s.\n" "/etc/syslog.conf"
fi

if [[ -n $SYSLOG_SUCCESS || -n $NIMOL_LOG_SUCCESS ]]; then
    ${SYSLOG} restart || warn "Failed to restart %s.\n" "${SYSLOG}"
fi

if [[ $STARTED_BOOTREPLY = "yes" ]]
then
	if [[ -s $NIMOL_BOOTREPLYD_PID ]] 
	then
		pid=$($CAT $NIMOL_BOOTREPLYD_PID) 
		kill $pid || warn "Failed to stop %s.\n" "nimol_bootreplyd"
		msg "Executed %s.\n" "kill $pid"
	fi
fi

if [[ -n $DHCP_SUCCESS ]]; then

    ${MV} ${PERM_TMP}/dhcpd.conf /etc/dhcpd.conf

    msg "Copied back the original file %s.\n" "/etc/dhcpd.conf"

fi

if [[ -n $DHCPD_CONF_CREATED ]]; then

    ${RM} -rf /etc/dhcpd.conf

    msg "Removed %s.\n" "/etc/dhcpd.conf"

fi

if [[ -n $DHCPD_STARTED ]]; then

    ${DHCPD} stop || warn "Failed to stop %s.\n" "dhcpd"

    msg "Executed %s.\n" "${DHCPD} stop"
elif [[ -n $DHCPD_RESTARTED ]]; then
    ${DHCPD} restart || warn "Failed to restart %s.\n" "dhcpd"

    msg "Executed %s.\n" "${DHCPD} restart"
fi

if [[ -n $CP_BOOTI_SUCCESS ]]; then

    ${RM} ${TARGET_DIR}/${BOOTI}
    msg "Removed %s.\n" "${TARGET_DIR}/${BOOTI}"
fi

if [[ -n $CP_TFTP_SUCCESS ]]; then

    ${RM} ${TFTPBOOT}/${LABEL}.chrp.mp.ent
    msg "Removed %s.\n" "${TFTPBOOT}/${LABEL}.chrp.mp.ent"
fi

if [[ -n $CP_SPOT_SUCCESS ]]; then

    ${RM} ${TARGET_DIR}/${SPOT}
    msg "Removed %s.\n" "{TARGET_DIR}/${SPOT}"
fi

if [[ -n $CP_MKSYSB_SUCCESS ]]; then

    ${RM} ${TARGET_DIR}/${MKSYSB}
    msg "Removed %s.\n" "${TARGET_DIR}/${MKSYSB}"
fi

if [[ -n $NFS_EXPORT_SUCCESS ]]; then

    ${AWK} '{if (!($1 == dir && $2 == "*(rw,insecure,no_root_squash)")) print $0;}' dir="${TARGET_DIR}" \
	/etc/exports >${TMPDIR}/exports.tmp && ${MV} ${TMPDIR}/exports.tmp /etc/exports

    msg "Removed \"%s\" from the file \"%s\"\n" "${TARGET_DIR} *(rw,insecure,no_root_squash)" "/etc/exports"

    ${EXPORTFS} -ua 2>$ERR || cmd_warn "${EXPORTFS} -ua"
    msg "Executed %s.\n" "${EXPORTFS} -ua"

    ${EXPORTFS} -a 2>$ERR || cmd_warn "${EXPORTFS} -a"
    msg "Executed %s.\n" "${EXPORTFS} -a"

fi

if [[ -n $NFS_SUCCESS ]]; then
    ${NFS} stop || err 1 "Failed to stop %s.\n" "${NFS}"
fi

if [[ -n $NFSSERVER_SUCCESS ]]; then
    ${NFSSERVER} stop || err 1 "Failed to stop %s.\n" "${NFSSERVER}"
fi

if [[ -n $PORTMAP_SUCCESS ]]; then
    ${PORTMAP} stop || err 1 "Failed to stop %s.\n" "${PORTMAP}"
fi

if [[ -n $MK_PERM_TMP_SUCCESS ]]; then 
    ${RM} -fr $PERM_TMP
    msg "Removed %s.\n" "$PERM_TMP"
fi

if [[ -n $MK_TDIR_SUCCESS ]]; then
    ${RM} -fr $TARGET_DIR
    msg "Removed %s.\n" "$TARGET_DIR"
fi
 
if [[ -n $IPTABLES_SUCCESS ]]; then
    ${IPTABLES} -D INPUT -s $SOURCE_SYS -j ACCEPT
    msg "Executed %s.\n" "${IPTABLES} -D INPUT -s $SOURCE_SYS -j ACCEPT"
fi

return
}


################################################################################
#
#  unconfig - unconfigure the NIMOL server
#
#  parameters:
#
################################################################################
function unconfig {

typeset tftp_en=
typeset remote_syslog_en=
typeset nimol_syslog=
typeset dhcp_bootp_en=
typeset dhcp_bootp_prev=
typeset dhcpd_started=
typeset dhcp_subnet_added=
typeset portmap_started=
typeset nfsserver_started=
typeset nfs_started=
typeset orig_file_dir=
typeset hmc_syslog_mod=

msg "Unconfiguring the NIMOL server...\n"

#
# Don't unconfigure if clients are set up to install
#
[[ ! -s $NIMOL_CONF ]] && err 1 "The file %s does not exist.\n" "${NIMOL_CONF}"

VALID_CLIENTS=$( ${AWK} '{if ( $1 == "CLIENT" ) print "\t"$2;}' $NIMOL_CONF )
if [[ -n $VALID_CLIENTS ]]
then 
    err 1 "Cannot unconfigure NIMOL because the following clients are\nready to install:\n%s\n" "$VALID_CLIENTS"
fi

# If we enabled tftp when configuring the NIMOL server, then disable it
tftp_en=$( ${AWK} '{if ($1 == "TFTP_ENABLED") print $2;}' $NIMOL_CONF 2>$ERR )
[[ $? != 0 ]] && cmd_warn "${AWK}"
if [[ $tftp_en = "yes" ]]
then
    if ${GREP} -e "^[[:space:]*]*disable.*no" /etc/xinetd.d/tftp >/dev/null 2>/dev/null
    then
	if ${AWK} '{if ($1 == "disable") print "\tdisable\t\t\t= yes"; else print $0}' \
	    /etc/xinetd.d/tftp >${TMPDIR}/tftp.tmp 2>$ERR
	then
	    ${MV} ${TMPDIR}/tftp.tmp /etc/xinetd.d/tftp 2>$ERR || \
		cmd_warn "${MV} ${TMPDIR}/tftp.tmp /etc/xinetd.d/tftp"
	    msg "Removed \"%s\" from the file \"%s\"\n" "disable = no" "/etc/xinetd.d/tftp"
	    msg "Added \"%s\" to the file \"%s\"\n" "disable = yes" "/etc/xinetd.d/tftp"
	else
	    cmd_warn "${AWK}" 1
	fi
    elif ${GREP} -e "^[[:space:]*]*disable.*yes" /etc/xinetd.d/tftp >/dev/null 2>/dev/null
    then
	:
    else
	if ${AWK} '{if ($1 == "server") print $0 "\n\tdisable\t\t\t= yes"; else print $0}' \
	    /etc/xinetd.d/tftp >${TMPDIR}/tftp.tmp 2>$ERR
	then
	    ${MV} ${TMPDIR}/tftp.tmp /etc/xinetd.d/tftp 2>$ERR || \
		cmd_warn "${MV} ${TMPDIR}/tftp.tmp /etc/xinetd.d/tftp"
	    msg "Added \"%s\" to the file \"%s\"\n" "disable = no" "/etc/xinetd.d/tftp"
	else
	    cmd_warn "${AWK}"
	fi
    fi

    ${XINETD} restart || warn "Failed to restart %s.\n" "${XINETD}"
fi
  
# If we enabled remote syslog when configuring the NIMOL server, then disable it
remote_syslog_en=$( ${AWK} '{if ($1 == "REMOTE_SYSLOG_ENABLED" ) print $2;}' $NIMOL_CONF 2>$ERR )
[[ $? != 0 ]] && cmd_warn "${AWK}"
if [[ $remote_syslog_en = "yes" ]]
then 

    # Determine whether the variable is SYSLOGD_OPTIONS (Redhat) or SYSLOGD_PARAMS (HMC)
    # This function will set the global variable SYSLOGD_VAR with the correct name
    get_syslogd_param_var

    SYSLOGD_OPTIONS=$( ${AWK} 'BEGIN {FS="="} {if ($1 == var) print $2}' var="${SYSLOGD_VAR}" /etc/sysconfig/syslog )
    if 	[[ ${SYSLOGD_OPTIONS} = *-r* ]]
    then

	# remove the quotes
	SYSLOGD_OPTIONS=${SYSLOGD_OPTIONS#\"}
	SYSLOGD_OPTIONS=${SYSLOGD_OPTIONS%\"}

	SYSLOGD_OPTIONS="${SYSLOGD_OPTIONS#-r }"

	if ${AWK} 'BEGIN {FS="="} {if ($1 == var) printf ("%s=\"%s\"\n", var, opts); else print $0; }' var="${SYSLOGD_VAR}" opts="${SYSLOGD_OPTIONS}" /etc/sysconfig/syslog >${TMPDIR}/syslog.tmp 2>$ERR
	then
	    ${MV} /${TMPDIR}/syslog.tmp /etc/sysconfig/syslog 2>$ERR || \
		cmd_warn "${MV} ${TMPDIR}/syslog.tmp /etc/sysconfig/syslog"

	    msg "Removed \"%s\" from the file \"%s\"\n" "${SYSLOGD_VAR}=\"-r ${SYSLOGD_OPTIONS}\"" "/etc/sysconfig/syslog"
	    msg "Added \"%s\" to the file \"%s\"\n" "${SYSLOGD_VAR}=\"$SYSLOGD_OPTIONS\"" "/etc/sysconfig/syslog"
	else
	    cmd_warn "${AWK}"
	fi
    fi
fi


# Remove the nimol.log from /etc/syslog.conf if it is there
nimol_syslog=$( ${AWK} '{if ($1 == "NIMOL_SYSLOG_FACILITY" ) print $2;}' $NIMOL_CONF 2>$ERR )
[[ $? != 0 ]] && cmd_warn "${AWK}"
if [[ -n $nimol_syslog ]]
then
    if ${AWK} '{if (!($1 ~ nimol_sysl && $2 == "/var/log/nimol.log" )) print $0; }' \
	nimol_sysl="${nimol_syslog}" /etc/syslog.conf >${TMPDIR}/syslog.conf.tmp 2>$ERR
    then
	${MV} /${TMPDIR}/syslog.conf.tmp /etc/syslog.conf 2>$ERR || \
	    cmd_warn "${MV} ${TMPDIR}/syslog.conf.tmp /etc/syslog.conf"
	
	msg "Removed \"%s\" from the file \"%s\"\n" "${nimol_syslog}.*   /var/log/nimol.log" "/etc/syslog.conf"
    fi
fi

# Replace the HMC line that we removed from /etc/syslog.conf
hmc_syslog_mod=$( ${AWK} '{if ($1 == "HMC_SYSLOG_CONF_MODIFIED" ) print $2;}' $NIMOL_CONF 2>$ERR )
[[ $? != 0 ]] && cmd_warn "${AWK}"
if [[ -n $hmc_syslog_mod ]]
then
    if ${AWK} '{if ($1 == "local3.*" && $2 == "-/var/log/localmessages" ) \
	print "local2,local3.*\t\t\t-/var/log/localmessages"; else print $0 }' \
	/etc/syslog.conf >${TMPDIR}/syslog.conf.tmp 2>$ERR
    then
	${MV} /${TMPDIR}/syslog.conf.tmp /etc/syslog.conf 2>$ERR || \
	    cmd_warn "${MV} ${TMPDIR}/syslog.conf.tmp /etc/syslog.conf"
	
	msg "Removed \"%s\" from the file \"%s\"\n" "local3.*  -/var/log/localmessages" "/etc/syslog.conf"
	msg "Added \"%s\" to the file \"%s\"\n" "local2,local3.*  -/var/log/localmessages" "/etc/syslog.conf"
    fi
fi

${SYSLOG} restart || warn "Failed to restart %s.\n" "${SYSLOG}"

nimol_br_en=$( ${AWK} '{if ($1 == "NIMOL_BOOTREPLYD" ) print $2;}' $NIMOL_CONF 2>$ERR )
[[ $? != 0 ]] && cmd_warn "${AWK}"

if [[ $nimol_br_en = "yes" ]]
then
	if [[ -s $NIMOL_BOOTREPLYD_PID ]] 
	then
		pid=$($CAT $NIMOL_BOOTREPLYD_PID) 
		kill $pid || warn "Failed to stop %s.\n" "nimol_bootreplyd"
		msg "Executed %s.\n" "kill $pid"
	fi
else
	[[ ! -s /etc/dhcpd.conf ]] && warn "The file %s does not exist.\n" "/etc/dhcpd.conf"

	# copy the dhcpd.conf and modify the copied version
	${CP} /etc/dhcpd.conf ${TMPDIR}/dhcpd.conf.tmp 2>$ERR || \
		cmd_warn "${CP} /etc/dhcpd.conf ${TMPDIR}/dhcpd.conf.tmp"

	# Restore DHCP to its original state
	dhcp_bootp_en=$( ${AWK} '{if ($1 == "DHCP_BOOTP_ENABLED" ) print $2;}' $NIMOL_CONF 2>$ERR )
	[[ $? != 0 ]] && cmd_warn "${AWK}"
	if [[ $dhcp_bootp_en = "yes" ]]
	then

		dhcp_bootp_prev=$( ${AWK} '{if ($1 == "DHCP_BOOTP_PREVIOUS" ) print $2;}' $NIMOL_CONF 2>$ERR )
		[[ $? != 0 ]] && cmd_warn "${AWK}"
		if [[ $dhcp_bootp_prev = "deny" ]]
		then
		if ${AWK} '{if ( $0 ~ /^[ \t\f\n\r\v]*allow[ \t\f\n\r\v]+bootp/ ) print "deny bootp;"; else print $0}' \
			${TMPDIR}/dhcpd.conf.tmp >${TMPDIR}/dhcpd.conf.tmp.1 2>$ERR
		then
			${MV} ${TMPDIR}/dhcpd.conf.tmp.1 ${TMPDIR}/dhcpd.conf.tmp 2>$ERR || \
			cmd_warn "${MV} ${TMPDIR}/dhcpd.conf.tmp.1 ${TMPDIR}/dhcpd.conf.tmp"
			msg "Added \"%s\" to the file \"%s\"\n" "deny bootp;" "/etc/dhcpd.conf"
			msg "Removed \"%s\" from the file \"%s\"\n" "allow bootp;" "/etc/dhcpd.conf"
		fi
		elif [[ $dhcp_bootp_prev = "ignore" ]]
		then
		if ${AWK} '{if ( $0 ~ /^[ \t\f\n\r\v]*allow[ \t\f\n\r\v]+bootp/ ) print "ignore bootp;"; else print $0}' \
			${TMPDIR}/dhcpd.conf.tmp >${TMPDIR}/dhcpd.conf.tmp.1 2>$ERR
		then
			${MV} ${TMPDIR}/dhcpd.conf.tmp.1 ${TMPDIR}/dhcpd.conf.tmp 2>$ERR || \
			cmd_warn "${MV} ${TMPDIR}/dhcpd.conf.tmp.1 ${TMPDIR}/dhcpd.conf.tmp"
			msg "Added \"%s\" to the file \"%s\"\n" "ignore bootp;" "/etc/dhcpd.conf"
			msg "Removed \"%s\" from the file \"%s\"\n" "allow bootp;" "/etc/dhcpd.conf"
		fi
		else
		if ${AWK} '{if (!( $0 ~ /^[ \t\f\n\r\v]*allow[ \t\f\n\r\v]+bootp/ )) print $0;}' \
			${TMPDIR}/dhcpd.conf.tmp >${TMPDIR}/dhcpd.conf.tmp.1 2>$ERR
		then
			${MV} ${TMPDIR}/dhcpd.conf.tmp.1 ${TMPDIR}/dhcpd.conf.tmp 2>$ERR || \
			cmd_warn "${MV} ${TMPDIR}/dhcpd.conf.tmp.1 ${TMPDIR}/dhcpd.conf.tmp"
			msg "Removed \"%s\" from the file \"%s\"\n" "allow bootp;" "/etc/dhcpd.conf"
		fi
		fi
	fi
	
	dhcp_bootp_en=$( ${AWK} '{if ($1 == "DHCP_NOT_AUTHORITATIVE_ADDED" ) print $2;}' $NIMOL_CONF 2>$ERR )
	[[ $? != 0 ]] && cmd_warn "${AWK}"
	if [[ $dhcp_bootp_en = "yes" ]]
	then
		# remove "not authoritative" from the dhcpd.conf file
		if ${AWK} '{if (!( $0 ~ /^.*not[ \t\f\n\r\v]+authoritative/ )) print $0}' \
		${TMPDIR}/dhcpd.conf.tmp >${TMPDIR}/dhcpd.conf.tmp.1 2>$ERR
		then
		${MV} ${TMPDIR}/dhcpd.conf.tmp.1 ${TMPDIR}/dhcpd.conf.tmp 2>$ERR || \
			cmd_warn "${MV} ${TMPDIR}/dhcpd.conf.tmp.1 ${TMPDIR}/dhcpd.conf.tmp"
		msg "Removed \"%s\" from the file \"%s\"\n" "not authoritative;" "/etc/dhcpd.conf"
		fi
	fi
	
	dhcp_subnet_added=$( ${AWK} '{if ($1 == "DHCP_SUBNET_ADDED" ) print $2;}' $NIMOL_CONF 2>$ERR )
	[[ $? != 0 ]] && cmd_warn "${AWK}"
	if [[ -n $dhcp_subnet_added ]]
	then
		for dhcp_sub in $dhcp_subnet_added
		do
			# remove "subnet" from the dhcpd.conf file
			if ${AWK} 'BEGIN { RS = "\n\n" } {if (!( $1 == "subnet" && $2 == SUBNET )) print $0}' SUBNET="$dhcp_sub" \
			${TMPDIR}/dhcpd.conf.tmp >${TMPDIR}/dhcpd.conf.tmp.1 2>$ERR
		then
			${MV} ${TMPDIR}/dhcpd.conf.tmp.1 ${TMPDIR}/dhcpd.conf.tmp 2>$ERR || \
			cmd_warn "${MV} ${TMPDIR}/dhcpd.conf.tmp.1 ${TMPDIR}/dhcpd.conf.tmp"
			msg "Removed \"%s\" from the file \"%s\"\n" "subnet $dhcp_sub" "/etc/dhcpd.conf"
		fi
		done
	fi
	
	# remove /etc/dhcpd.conf if nimol created it, otherwise copy back the modified version
	dhcpd_conf_created=$( ${AWK} '{if ($1 == "DHCPD_CONF_CREATED" ) print $2;}' $NIMOL_CONF 2>$ERR )
	[[ $? != 0 ]] && cmd_warn "${AWK}"
	if [[ $dhcpd_conf_created = "yes" ]]
	then
		${RM} -f /etc/dhcpd.conf 2>$ERR || cmd_warn "${RM} -f /etc/dhcpd.conf"
		msg "Removed %s.\n" "/etc/dhcpd.conf"
	else
		${MV} ${TMPDIR}/dhcpd.conf.tmp /etc/dhcpd.conf 2>$ERR || \
		cmd_warn "${MV} ${TMPDIR}/dhcpd.conf.tmp /etc/dhcpd.conf"
	fi
	
	# Stop DHCPD if NIMOL started it
	dhcpd_started=$( ${AWK} '{if ($1 == "DHCPD_STARTED" ) print $2;}' $NIMOL_CONF 2>$ERR )
	[[ $? != 0 ]] && cmd_warn "${AWK}"
	if [[ $dhcpd_started = "yes" ]]
	then
		${DHCPD} stop || warn "Failed to stop %s.\n" "${DHCPD}"
		msg "Executed %s.\n" "${DHCPD} stop"
	else
		${DHCPD} restart || warn "Failed to restart %s.\n" "${DHCPD}"
		msg "Executed %s.\n" "${DHCPD} restart"
	fi
fi


orig_file_dir=$( ${AWK} '{if ($1 == "ORIGINAL_FILES_DIR" ) print $2;}' $NIMOL_CONF 2>$ERR )
[[ $? != 0 ]] && cmd_warn "${AWK}"
if [[ -n $orig_file_dir ]]
then
    ${RM} -fr $orig_file_dir
    msg "Removed %s.\n" "$orig_file_dir"
fi

# Stop NFSSERVER if NIMOL started it
nfsserver_started=$( ${AWK} '{if ($1 == "NFSSERVER_STARTED" ) print $2;}' $NIMOL_CONF 2>$ERR )
[[ $? != 0 ]] && cmd_warn "${AWK}"
if [[ $nfsserver_started = "yes" ]]
then
    ${NFSSERVER} stop || warn "Failed to stop %s.\n" "${NFSSERVER}"
    msg "Executed %s.\n" "${NFSSERVER} stop"
fi

# Stop NFS if NIMOL started it
nfs_started=$( ${AWK} '{if ($1 == "NFS_STARTED" ) print $2;}' $NIMOL_CONF 2>$ERR )
[[ $? != 0 ]] && cmd_warn "${AWK}"
if [[ $nfs_started = "yes" ]]
then
    ${NFS} stop || warn "Failed to stop %s.\n" "${NFS}"
    msg "Executed %s.\n" "${NFS} stop"
fi

# Stop PORTMAP if NIMOL started it
portmap_started=$( ${AWK} '{if ($1 == "PORTMAP_STARTED" ) print $2;}' $NIMOL_CONF 2>$ERR )
[[ $? != 0 ]] && cmd_warn "${AWK}"
if [[ $portmap_started = "yes" ]]
then
    ${PORTMAP} stop || warn "Failed to stop %s.\n" "${PORTMAP}"
    msg "Executed %s.\n" "${PORTMAP} stop"
fi

# Should error out if something above fails
${RM} -f $NIMOL_CONF
msg "Removed %s.\n" "$NIMOL_CONF"


return
}


################################################################################
#
#  cleanup - standard cleanup 
#
#  parameters:
#
################################################################################
function cleanup {

    trap "" 0

    if [[ -n $MNT_SUCCESS ]]; then
	${UMOUNT} /mnt/nimol || cmd_warn "${UMOUNT} /mnt/nimol"
    fi

    ${RM} -rf $TMPDIR

    ${RM} -rf $ERR

} # end of cleanup 

################################################################################
#
#  extract_nim_res - extracts the NIM resources from a tar file
#
#  parameters:
#    location - directory containing tar file of NIM resources
#  global:
#    TARGET_DIR
#
#  return codes:
#    0 - successfully extracted NIM resources
#    1 - failed to find the tar file or other error extracting files
#
################################################################################
function extract_nim_res {

    typeset tar_file=
    location=$1

    #
    # Create the target directory and copy the AIX Install resources locally
    #
    if [[ ! -d $TARGET_DIR ]]; then
        ${MKDIR} -p $TARGET_DIR 2>$ERR || cmd_err 1 "${MKDIR} -p $TARGET_DIR"
        MK_TDIR_SUCCESS=yes
    fi

    # Move to the target directory
    cd ${TARGET_DIR}

    # Is the location pointing to the tar file directly
    if [[ -f $location ]]
    then
        ${TAR} -xvf ${location} ./index >/dev/null 2>/dev/null
	if [[ $? = 0 ]]; then
	    tar_file=${location}
	fi
    else
	# search for the tar file in the directory
	for file in $(ls $location)
	do
	    ${TAR} -xvf ${location}/${file} ./index >/dev/null 2>/dev/null
	    if [[ $? = 0 ]]; then
		tar_file=${location}/${file}
		break
	    fi
	done
    fi

    [[ -z ${tar_file} ]] && return 1

    msg "Checking %s for existing resources.\n" "${tar_file}"

    # Extract everything into the target directory
    ${TAR} -xvf ${tar_file} >/dev/null 2>$ERR || cmd_err 1 "${TAR} -xvf ${tar_file}"

    # Rename everything based off the ./index file
    for line in $( ${CAT} ${TARGET_DIR}/index )
    do
        if [[ ${line%:*} = "spot" ]]; then
	    if [[ ${line#*:} != "./${SPOT}" ]]; then 
	        ${MV} ${line#*:} ${SPOT} || cmd_err 1 "${MV} ${line#*:} ${SPOT}"
	    fi
	elif [[ ${line%:*} = "mksysb" ]]; then
	    if [[ ${line#*:} != "./${MKSYSB}" ]]; then 
	        ${MV} ${line#*:} ${MKSYSB} || cmd_err 1 "${MV} ${line#*:} ${MKSYSB}"
	    fi
	elif [[ ${line%:*} = "boot" ]]; then
	    if [[ ${line#*:} != "./${BOOTI}" ]]; then 
	        ${MV} ${line#*:} ${BOOTI} || cmd_err 1 "${MV} ${line#*:} ${BOOTI}"
	    fi
	elif [[ ${line%:*} = "bosinst_data" ]]; then
	    if [[ ${line#*:} != "./${BOSINST_DATA}" ]]; then 
	        ${MV} ${line#*:} ${BOSINST_DATA} || cmd_err 1 "${MV} ${line#*:} ${BOSINST_DATA}"
	    fi
	elif [[ ${line%:*} = "resolv_conf" ]]; then
	    if [[ ${line#*:} != "./${RESOLV_CONF}" ]]; then 
	        ${MV} ${line#*:} ${RESOLV_CONF} || cmd_err 1 "${MV} ${line#*:} ${RESOLV_CONF}"
	    fi
	elif [[ ${line%:*} = "image_data" ]]; then
	    if [[ ${line#*:} != "./${IMAGE_DATA}" ]]; then 
	        ${MV} ${line#*:} ${IMAGE_DATA} || cmd_err 1 "${MV} ${line#*:} ${IMAGE_DATA}"
	    fi
	elif [[ ${line%:*} = "lpp_source" ]]; then
	    if [[ ${line#*:} != "./${LPP_SOURCE}" ]]; then 
	        ${MV} ${line#*:} ${LPP_SOURCE} || cmd_err 1 "${MV} ${line#*:} ${LPP_SOURCE}"
	    fi
	elif [[ ${line%:*} = "cust_script" ]]; then
	    if [[ ${line#*:} != "./${CUST_SCRIPT}" ]]; then 
	        ${MV} ${line#*:} ${CUST_SCRIPT} || cmd_err 1 "${MV} ${line#*:} ${CUST_SCRIPT}"
	    fi
	fi
    done

    # Copy the bootimage to the tftpboot directory
    ${CP} ${TARGET_DIR}/$BOOTI ${TFTPBOOT}/${LABEL}.chrp.mp.ent 2>$ERR || \
	cmd_err 1 "${CP} ${TARGET_DIR}/$BOOTI ${TFTPBOOT}/${LABEL}.chrp.mp.ent"
    CP_TFTP_SUCCESS=yes

    #
    # Make the scripts directory
    #
    ${MKDIR} ${TARGET_DIR}/scripts 2>$ERR || cmd_err 1 "${MKDIR} ${TARGET_DIR}/scripts"

    EXTRACT_SUCCESSFUL="yes"

    return 0

} # end of extract_nim_res

################################################################################
#
#  copy_resources - copy resources from the source directory to the target dir
# - checks that all required resources exist before trying to do any copying
# - copies required resources or fails
# - copies optional resources
#
#  parameters:
#  global:
#    TARGET_DIR
#    SOURCE_DIR
#
################################################################################
function copy_resources {

    typeset SOURCE_SPOT=
    typeset SOURCE_MKSYSB=
    typeset SOURCE_BOOTI=
    typeset SIMAGES_LIST=`echo $SIMAGES_OPTIONS`

    #
    # Check that the required resources exist
    #
    [[ -s ${SOURCE_DIR}/${SPOT} ]] && SOURCE_SPOT=${SOURCE_DIR}/${SPOT}
    [[ -s ${SOURCE_DIR}/${ISPOT} ]] && SOURCE_SPOT=${SOURCE_DIR}/${ISPOT}
    [[ -s ${SOURCE_DIR}/nimol/${ISPOT} ]] && SOURCE_SPOT=${SOURCE_DIR}/nimol/${ISPOT}
    [[ -s ${SOURCE_DIR}/${DISPOT} ]] && SOURCE_SPOT=${SOURCE_DIR}/${DISPOT} 
    [[ -s ${SOURCE_DIR}/nimol/${DISPOT} ]] && SOURCE_SPOT=${SOURCE_DIR}/nimol/${DISPOT} 
    [[ -n $AIX_VERSION && -s ${AIX_RES_ROOT}/${AIX_VERSION}/${ISPOT} ]] && SOURCE_SPOT=${AIX_RES_ROOT}/${AIX_VERSION}/${ISPOT}

    [[ -z ${SOURCE_SPOT} ]] && err 1 "Cannot find the resource %s in %s.\n" "SPOT" "${SOURCE_DIR}"


    [[ -s ${SOURCE_DIR}/${MKSYSB} ]] && SOURCE_MKSYSB=${SOURCE_DIR}/${MKSYSB}
    [[ -s ${SOURCE_DIR}/${MKSYSBFF} ]] && SOURCE_MKSYSB=${SOURCE_DIR}/${MKSYSBFF}
    if [[ -z ${SOURCE_MKSYSB} ]]
    then
        # look for a file with mksysb in the name
        for file in $(ls ${SOURCE_DIR})
        do
            if [[ $file = *mksysb* ]]; then
    	        SOURCE_MKSYSB="${SOURCE_DIR}/${file}"
	        break
	    fi
        done
    fi

    [[ -z ${SOURCE_MKSYSB} ]] && [[ ! -s ${SOURCE_DIR}/${LPP_SOURCE} ]] && \
	[[ ! -s ${SOURCE_DIR}/installp/ppc ]] && \
	err 1 "The directory \"%s\" must have a mksysb or a lpp_source.\n" "${SOURCE_DIR}"


    [[ -s ${SOURCE_DIR}/${BOOTI} ]] && SOURCE_BOOTI=${SOURCE_DIR}/${BOOTI}
    [[ -s ${SOURCE_DIR}/${BOOTIZ} ]] && SOURCE_BOOTI=${SOURCE_DIR}/${BOOTIZ}
    [[ -s ${SOURCE_DIR}/nimol/${BOOTIZ} ]] && SOURCE_BOOTI=${SOURCE_DIR}/nimol/${BOOTIZ}
    [[ -n $AIX_VERSION && -s ${AIX_RES_ROOT}/${AIX_VERSION}/${BOOTIZ} ]] && SOURCE_BOOTI=${AIX_RES_ROOT}/${AIX_VERSION}/${BOOTIZ}

    [[ -z ${SOURCE_BOOTI} ]] && err 1 "Cannot find the resource %s in %s.\n" "boot image" "${SOURCE_DIR}"


    #
    # Create the target directory and copy the AIX Install resources locally
    #
    if [[ ! -d $TARGET_DIR ]]; then
        ${MKDIR} -p $TARGET_DIR 2>$ERR || cmd_err 1 "${MKDIR} -p $TARGET_DIR"
        MK_TDIR_SUCCESS=yes
    fi

    cd ${TARGET_DIR}

    # Copy bootimage
    msg "Copying %s to %s...\n" "${SOURCE_BOOTI}" "${TARGET_DIR}"
    ${CP} ${SOURCE_BOOTI} $TARGET_DIR 2>$ERR || \
	cmd_err 1 "${CP} ${SOURCE_BOOTI} ${TARGET_DIR}"
    if [[ -s ${TARGET_DIR}/${BOOTIZ} ]]; then
	$GZIP -d ${TARGET_DIR}/${BOOTIZ} || cmd_err 1 "$GZIP -d ${TARGET_DIR}/${BOOTIZ}"
    fi
    CP_BOOTI_SUCCESS=yes


    # Copy the bootimage to the tftpboot directory
    ${CP} ${TARGET_DIR}/$BOOTI ${TFTPBOOT}/${LABEL}.chrp.mp.ent 2>$ERR || \
	cmd_err 1 "${CP} ${TARGET_DIR}/$BOOTI ${TFTPBOOT}/${LABEL}.chrp.mp.ent"
    CP_TFTP_SUCCESS=yes


    # Copy SPOT
    msg "Copying %s to %s...\n" "${SOURCE_SPOT}" "${TARGET_DIR}"
    ${CP} -R ${SOURCE_SPOT} ${TARGET_DIR} 2>$ERR || \
	cmd_err 1 "${CP} ${SOURCE_SPOT} ${TARGET_DIR}"
    if [[ -s ${TARGET_DIR}/${ISPOT} ]]; then
	$GZIP -d ${TARGET_DIR}/${ISPOT} || cmd_err 1 "$GZIP -d ${TARGET_DIR}/${ISPOT}"
	$TAR -xf ${TARGET_DIR}/${ISPOT_TAR} || cmd_err 1 "$TAR -xf ${TARGET_DIR}/${ISPOT_TAR}"
	$RM -f ${TARGET_DIR}/${ISPOT_TAR} || cmd_err 1 "$RM -f ${TARGET_DIR}/${ISPOT_TAR}"
    elif [[ -s ${TARGET_DIR}/${DISPOT} ]]; then
	$GZIP -d ${TARGET_DIR}/${DISPOT} || cmd_err 1 "$GZIP -d ${TARGET_DIR}/${DISPOT}"
	$TAR -xf ${TARGET_DIR}/${DISPOT_TAR} || cmd_err 1 "$TAR -xf ${TARGET_DIR}/${DISPOT_TAR}"
	$RM -f ${TARGET_DIR}/${DISPOT_TAR} || cmd_err 1 "$RM -f ${TARGET_DIR}/${DISPOT_TAR}"
	# ALSO copy the associated file system - probably a cd file system
    fi
    CP_SPOT_SUCCESS=yes

    #add contents of CD filesystem if the source is from CD
    if [[ -d $SOURCE_DIR/usr ]]
    then
	(cd $SOURCE_DIR/usr && $TAR -cf - ./bin ./sbin ./lib ./ccs ./swlag ./lpp) | (cd $TARGET_DIR/$SPOT/usr && $TAR -xf -) || cmd_warn $TAR
    fi

    # Copy mksysb - we want it to be named "mksysb", not "mksysb.bff"
    if [[ -n ${SOURCE_MKSYSB} ]]
    then
	msg "Copying %s to %s...\n" "${SOURCE_MKSYSB}" "${TARGET_DIR}/$MKSYSB"
	${CP} ${SOURCE_MKSYSB} ${TARGET_DIR}/${MKSYSB} 2>$ERR || \
	    cmd_err 1 "${CP} ${SOURCE_MKSYSB}  ${TARGET_DIR}/${MKSYSB}"
	CP_MKSYSB_SUCCESS=yes
    fi

    #
    # Copy optional resources
    #
    if [[ -s ${SOURCE_DIR}/${IMAGE_DATA} ]]
    then
	msg "Copying %s to %s...\n" "${SOURCE_DIR}/${IMAGE_DATA}" "${TARGET_DIR}/${IMAGE_DATA}"
	${CP} ${SOURCE_DIR}/${IMAGE_DATA} ${TARGET_DIR} 2>$ERR || \
	    cmd_warn "${CP} ${SOURCE_DIR}/${IMAGE_DATA} ${TARGET_DIR}"
    fi

    # the aix-res rpm will have a bosinst.data, but use a user-provided one before that one
    # or ignore it if it's from a CD/DVD and AIX_VERSION is defined (that one way, we make
    # sure the one from the RPM is used)
    [[ -s ${SOURCE_DIR}/${BOSINST_DATA} && $FROM_CD != "yes" ]] && SRC_BID=${SOURCE_DIR}/${BOSINST_DATA}
    [[ -n $AIX_VERSION && -s ${AIX_RES_ROOT}/${AIX_VERSION}/${BOSINST_DATA} && -z $SRC_BID ]] && SRC_BID=${AIX_RES_ROOT}/${AIX_VERSION}/${BOSINST_DATA}

	# check for alternate bosinst.data from -b option
	if [[ -n $ALT_BID ]]
	then
		if [[ ! -s $ALT_BID ]]
		then
		err 1 "The file %s does not exist.\n" "$ALT_BID"
		fi

		SRC_BID=$ALT_BID
	fi

    if [[ -s $SRC_BID ]]
    then
	msg "Copying %s to %s...\n" "$SRC_BID" "${TARGET_DIR}/${BOSINST_DATA}"
	${CP} ${SRC_BID} ${TARGET_DIR}/${BOSINST_DATA} 2>$ERR || \
	    cmd_warn "${CP} ${SRC_BID} ${TARGET_DIR}"
    fi

    if [[ -s ${SOURCE_DIR}/${CUST_SCRIPT} ]]
    then
	msg "Copying %s to %s...\n" "${SOURCE_DIR}/${CUST_SCRIPT}" "${TARGET_DIR}/${CUST_SCRIPT}"
	${CP} ${SOURCE_DIR}/${CUST_SCRIPT} ${TARGET_DIR} 2>$ERR || \
	    cmd_warn "${CP} ${SOURCE_DIR}/${CUST_SCRIPT} ${TARGET_DIR}"
	if [[ ! -x ${TARGET_DIR}/${CUST_SCRIPT} ]]
	then
	    ${CHMOD} +x ${TARGET_DIR}/${CUST_SCRIPT} || \
		cmd_warn "${CHMOD} +x ${TARGET_DIR}/${CUST_SCRIPT}"
	fi
    fi

    if [[ -s ${SOURCE_DIR}/${RESOLV_CONF} ]]
    then
	msg "Copying %s to %s...\n" "${SOURCE_DIR}/${RESOLV_CONF}" "${TARGET_DIR}/${RESOLV_CONF}"
	${CP} ${SOURCE_DIR}/${RESOLV_CONF} ${TARGET_DIR} 2>$ERR || \
	    cmd_warn "${CP} ${SOURCE_DIR}/${RESOLV_CONF} ${TARGET_DIR}"
    fi

    if [[ -d ${SOURCE_DIR}/${LPP_SOURCE} ]]
    then
	msg "Copying %s to %s...\n" "${SOURCE_DIR}/${LPP_SOURCE}" "${TARGET_DIR}/${LPP_SOURCE}"
	(cd ${SOURCE_DIR}/${LPP_SOURCE} && $TAR -cf - $SIMAGES_LIST 2>$ERR) | (cd ${TARGET_DIR} && $TAR -xf - 2>$ERR) || \
	    cmd_warn "$TAR"
    elif [[ -d ${SOURCE_DIR}/installp/ppc || -d ${SOURCE_DIR}/RPMS ]]
    then
	mkdir ${TARGET_DIR}/${LPP_SOURCE}
	msg "Copying %s to %s...\n" "${SOURCE_DIR}/installp" "${TARGET_DIR}/${LPP_SOURCE}"
	(cd ${SOURCE_DIR} && $TAR -cf - $SIMAGES_LIST 2>$ERR) | (cd ${TARGET_DIR}/${LPP_SOURCE} && $TAR -xf - 2>$ERR) || \
	    cmd_warn "$TAR"
    fi

    #
    # Make the scripts directory
    #
    ${MKDIR} ${TARGET_DIR}/scripts 2>$ERR || cmd_warn "${MKDIR} ${TARGET_DIR}/scripts"
}


################################################################################
#
#  check_for_resources - checks if resources exist in the target dir
# - if the required resources exist, then it sets DONT_COPY=yes
#   
#  parameters:
#  global:
#    TARGET_DIR
#
################################################################################
function check_for_resources {

    msg "Checking %s for existing resources.\n" ${TARGET_DIR}

    #
    # Check that resources exist
    #
    if [[ ! -s ${TARGET_DIR}/$SPOT ]]; then
	msg "The file %s does not exist.\n" "${TARGET_DIR}/$SPOT"
	return 1
    fi
    if [[ ! -s ${TARGET_DIR}/$MKSYSB && ! -d ${TARGET_DIR}/$LPP_SOURCE ]]; then
	msg "The directory \"%s\" must have a mksysb or a lpp_source.\n" "${TARGET_DIR}"
	return 1
    fi
    if [[ ! -s ${TARGET_DIR}/$BOOTI ]]; then
	msg "The file %s does not exist.\n" "${TARGET_DIR}/$BOOTI"
	return 1
    fi

    # Copy the bootimage to the tftpboot directory
    if [[ ! -s ${TFTPBOOT}/${LABEL}.chrp.mp.ent ]]
    then
	${CP} ${TARGET_DIR}/$BOOTI ${TFTPBOOT}/${LABEL}.chrp.mp.ent 2>$ERR || \
	    cmd_err 1 "${CP} ${TARGET_DIR}/$BOOTI ${TFTPBOOT}/${LABEL}.chrp.mp.ent"
	CP_TFTP_SUCCESS=yes
    fi

    #
    # Don't check for optional resources
    #
    #
    # Make the scripts directory
    #
    if [[ ! -d ${TARGET_DIR}/scripts ]]
    then
	${MKDIR} ${TARGET_DIR}/scripts 2>$ERR || cmd_warn "${MKDIR} ${TARGET_DIR}/scripts"
    fi

    DONT_COPY=yes
}

################################################################################
#
#  install_aix_res_rpm - if the source was originally AIX product media,
#	and if the media contains the rpm RPMS/linux/aix-res-*.rpm, install this
#	RPM and set the variables AIX_VERSION and RPM_NAME
#   
#  parameters: mount point of source
#  global: AIX_VERSION RPM_NAME
#
################################################################################
function install_aix_res_rpm
{
	mnt_pnt=$1
	rpm_file=
	this_rpm_name=
	rpm_w_ver=
	installed_rpm=
	rpm_rc=
	file_line=

	rpm_file=`$LS ${mnt_pnt}/${DVD_RPM_LOC}`

	[[ -z $rpm_file || $? -ne 0 ]] && return 0 #just return if the RPM isn't on the media
	
	#determine if the RPM for this AIX version is installed
	# - if it's installed, but downlevel from this one, do RPM update
	# - if it's installed and at the same level, return and do nothing

	this_rpm_name=${rpm_file##*/} #cut off the path
	rpm_w_ver=${this_rpm_name%.noarch.rpm} #cut off the .rpm extension
	this_rpm_name=${this_rpm_name%%-[0-9].[0-9]*} #cut off the version part of the RPM name

	#save the AIX version name and the RPM name- we know by now we're definitely using the RPM
	RPM_NAME=$this_rpm_name
	AIX_VERSION=${RPM_NAME#aix-res-} #cut off the "aix-res-" prefix from the RPM name

	installed_rpm=`$RPM -q $this_rpm_name 2>/dev/null` #query the DB for the RPM name
	rpm_rc=$?

	if [[ $rpm_rc -eq 0 ]]
	then
		#just return and do nothing- already installed
		[[ $installed_rpm = $rpm_w_ver ]] && return 0

		# now, determine if our file is newer than the installed version
		echo $rpm_w_ver >/tmp/$$.rpmcmp
		echo $installed_rpm >>/tmp/$$.rpmcmp
		$SORT /tmp/$$.rpmcmp >/tmp/$$.rpmcmp2 2>/dev/null || cmd_warn $SORT
		$MV /tmp/$$.rpmcmp2 /tmp/$$.rpmcmp 2>/dev/null || cmd_warn $MV

		# here, we grab the first line of the temp file
		file_line=`$CAT /tmp/$$.rpmcmp | while read line; do echo $line;break; done`
		$RM -f /tmp/$$.rpmcmp >/dev/null 2>&1

		# The sort will have put the lowest level at the first line of this file
		# (which should only contain two lines).  If the low one = our RPM, then
		# do nothing and return because that means the currently installed version
		# is a later version than what we've got on the CD/DVD
		[[ $file_line = $rpm_w_ver ]] && return 0

		# if we're here, we can do an RPM update cuz we have a newer to install
		${RPM} -Uvh $rpm_file || err 1 "Failed to start %s.\n" "${RPM} -Uvh $rpm_file"
		msg "Executed %s.\n" "${RPM} -Uvh $rpm_file"
	else
		# if we're here, then the RPM wasn't previously installed and we can just go
		# ahead and install it

		#install the RPM
		${RPM} -ivh $rpm_file || err 1 "Failed to start %s.\n" "${RPM} -ivh $rpm_file"
		msg "Executed %s.\n" "${RPM} -ivh $rpm_file"
	fi
} #end install_aix_res_rpm

# ----------------------------------- MAIN ------------------------------------
#
#
#
#
# -----------------------------------------------------------------------------

# set parameters from command line
while getopts :B:Cd:DeI:lL:m:rs:St:Ubv: c
do
	case ${c} in

		B)		# bosinst.data file to apply to NIMOL label
				ALT_BID=$OPTARG
				if [[ ! -s $ALT_BID ]]
		        then
		        err 1 "The file %s does not exist.\n" "$ALT_BID"
		        fi
				;;

        C)		# do not configure syslog to accept messages from clients
				ONLY_CONFIG_NIMOL="yes"
				;;

        d)		# source directory to copy AIX install resources from
				SOURCE_DIR=$OPTARG
				;;
	
        D)		# debug output
				set -x
				;;
			
        e)		# don't export the LABEL directory globally
				DONT_GLOBAL_EXPORT="yes"
				;;

		I)		# auxiliary directory to copy AIX install resources from
				AUX_SOURCE_DIR=$OPTARG
				;;

		l)		# list the AIX install resources for a label
				LIST=yes
				;;
		
        m)		# remote access method for running commands on clients
				COMM_METH=$OPTARG
				;;
		
        L)		# the label for the AIX install resources
				LABEL=$OPTARG
			;;
				
        r)		# remove a label
				REMOVE_LABEL=yes
				;;

        s)		# the hostname of the nimol server
				NIMOL_SERVER=$OPTARG
				;;
				
        S)		# do not configure syslog to accept messages from clients
				CONFIG_SYSLOG="no"
				;;

        t)		# target directory to copy AIX install resources to
				TARGET_DIR=$OPTARG
				;;

        U)		# unconfigure NIMOL
				UNCONFIG_NIMOL="yes"
				;;

	b)		# use nimol_bootreplyd instead of dhcp
				NO_DHCP="yes"
				;;

	v)		# supply AIX version corresponding to resources RPM
				AIX_VERSION=$OPTARG
				RPM_NAME="aix-res-$AIX_VERSION"
				;;


        \?)		# unknown option
				usage "\nUsage nimol_config: Configure Linux server to install AIX.\n      nimol_config       [-d <directory containing AIX resources>]\n                         [-t <target directory to copy resources>]\n                         [-L <install resources label>]\n                         [-s <NIMOL server hostname>]\n                         [-m <remote access method>]\n                         [-C] [-e] [-l] [-r] [-S] [-U] [-D]\n\nExamples:\n  nimol_config -L 530 -d /mnt/aix # create 530 resources from /mnt/aix\n  nimol_config -C                 # configure server; don't create resources\n  nimol_config -l                 # list existing resource labels\n  nimol_config -L 530 -r          # remove 530 resources\n\ndefaults:\n\t-d    /mnt/cdrom\n\t-t    /export/aix\n\t-L    default\n\t-s    hostname of local machine\n\t-m    rsh\n"

				;;
	esac
done

trap err_signal 1 2 11 15
trap cleanup 0

#if an AIX_VERSION was provided, make sure the RPM for it exists
if [[ -n $AIX_VERSION ]]
then
	$RPM -q $RPM_NAME >/dev/null 2>&1 || err 1 "The file %s does not exist.\n" "${AIX_RES_ROOT}"
	[[ -d $AIX_RES_ROOT ]] || err 1 "The file %s does not exist.\n" "${AIX_RES_ROOT}"
fi

# tmp directory
${MKDIR} ${TMPDIR} 2>$ERR || cmd_err 1 "${MKDIR} ${TMPDIR}"

####################################################
#
# LIST
#
####################################################
if [[ -n $LIST ]]
then

    [[ ! -s $NIMOL_CONF ]] && err 1 "The file %s does not exist.\n" "${NIMOL_CONF}"

    # If a label is not specified, then list the valid labels
    if [[ -z $LABEL ]]
    then
        VALID_LABELS=$( ${AWK} '{if ( $1 == "LABEL" ) print "\t"$2;}' $NIMOL_CONF )
	if [[ -z $VALID_LABELS ]]
	then
	    msg "No resource labels are defined.\n"
	else
	    msg "The following resource labels are defined:\n%s\n" "$VALID_LABELS"
	fi 
    else
        # A label was specified, so list the label's resources
	msg "The following AIX install resources exist for the label \"%s\":\n" "$LABEL"

        # Determine directory
	dir=$( ${AWK} '{if ( $1 == "LABEL" && $2 == label ) print $3;}' label="$LABEL" $NIMOL_CONF 2>$ERR )
	[[ $? != 0 ]] && cmd_err 1 "${AWK}"
	[[ -z $dir ]] && err 1 "Unable to find the directory corresponding to %s in %s\n" "$LABEL" "$NIMOL_CONF"

	msg "%s directory: %s\n" "$LABEL" "$dir"
    
	${LS} $dir
    fi

    exit 0
fi


####################################################
#
# REMOVE
#
####################################################
if [[ -n $REMOVE_LABEL ]]
then

    [[ -z $LABEL ]] && err 1 "Please specify a label to remove.\n"

    [[ ! -s $NIMOL_CONF ]] && err 1 "The file %s does not exist.\n" "${NIMOL_CONF}"

    # Is a client setup to install with this label
    if ! ${AWK} '{if ( $1 == "CLIENT" && $6 == label ) exit 1;}' label="$LABEL" $NIMOL_CONF
    then 
	err 1 "Cannot remove label %s.  A client is setup to install with it.\n" "$LABEL"
    fi

    # Determine directory
    dir=$( ${AWK} '{if ( $1 == "LABEL" && $2 == label ) print $3;}' label="$LABEL" $NIMOL_CONF 2>$ERR )
    [[ $? != 0 ]] && cmd_err 1 "${AWK}"
    [[ -z $dir ]] && err 1 "Unable to find the directory corresponding to %s in %s\n" "$LABEL" "$NIMOL_CONF"

    # Unexport the directory
    if [[ -s /etc/exports ]] && ${AWK} '{if (!($1 == dir && $2 == "*(rw,insecure,no_root_squash)")) print $0;}' dir="$dir" /etc/exports >${TMPDIR}/exports.tmp 2>$ERR
    then
	${MV} ${TMPDIR}/exports.tmp /etc/exports 2>$ERR || cmd_err 1 "${MV} ${TMPDIR}/exports.tmp /etc/exports"
	msg "Removed \"%s\" from the file \"%s\"\n" "$dir *(rw,insecure,no_root_squash)" "/etc/exports"
    else
	cmd_warn "${AWK}"
    fi

    ${EXPORTFS} -ua 2>$ERR || cmd_warn "${EXPORTFS} -ua"
    msg "Executed %s.\n" "${EXPORTFS} -ua"

    ${EXPORTFS} -a 2>$ERR || cmd_warn "${EXPORTFS} -a"
    msg "Executed %s.\n" "${EXPORTFS} -a"

    # Remove the boot image in /tftpboot
    ${RM} -f /tftpboot/${LABEL}.chrp.mp.ent || cmd_warn "${RM} -fr /tftpboot/${LABEL}.chrp.mp.ent"
    msg "Removed %s.\n" "/tftpboot/${LABEL}.chrp.mp.ent"

    # Remove the directory
    ${RM} -fr $dir 2>$ERR || cmd_warn "${RM} -fr $dir"
    msg "Removed %s.\n" "$dir"

    # Remove the label from /etc/nimol.conf
    if ${AWK} '{if (!($1 == "LABEL" && $2 == label )) print $0;}' label="$LABEL" $NIMOL_CONF >${TMPDIR}/nimol.conf.tmp 2>$ERR
    then
	${MV} ${TMPDIR}/nimol.conf.tmp $NIMOL_CONF 2>$ERR || cmd_err 1 "${MV} ${TMPDIR}/nimol.conf.tmp $NIMOL_CONF"
    else
	cmd_warn "${AWK}" 1
    fi
    msg "Removed \"%s\" from the file \"%s\"\n" "LABEL $LABEL" "$NIMOL_CONF"

    exit 0
fi


####################################################
#
# UNCONFIGURE THE NIMOL SERVER
#
####################################################
if [[ -n $UNCONFIG_NIMOL ]]
then
    # may want to check for existing clients and resources before

    unconfig

    msg "Successfully unconfigured NIMOL.\n"

    exit 0
fi


####################################################
#
# CONFIGURE THE NIMOL SERVER
#
####################################################

#
# Write the NIMOL server's hostname to the /etc/nimol.conf file
#
if [[ ! -f $NIMOL_CONF ]]
then
    if [[ -z $NIMOL_SERVER ]]
    then	
	NIMOL_SERVER=$( ${HOSTNAME} )
	[[ -z $NIMOL_SERVER ]] && err 1 "Unable to determine the hostname of the NIMOL server.\n"

	msg "No NIMOL server hostname specified, using %s as the default.\n" "$NIMOL_SERVER"
    fi
	
    $ECHO "# nimol.conf file" >$NIMOL_CONF
    $ECHO "NIMOL_SERVER $NIMOL_SERVER" >>$NIMOL_CONF
    MK_CONF_SUCCESS=yes

    # make the directory that will store the original config files
    ${MKDIR} -p $PERM_TMP >/dev/null 2>/dev/null
    MK_PERM_TMP_SUCCESS=yes
    $ECHO "ORIGINAL_FILES_DIR $PERM_TMP" >>$NIMOL_CONF
else
    # the nimol.conf file already exists, check if the NIMOL_SERVER
    # is specified (it should be).  Otherwise, was one specified from
    # the command line?
    ns_existing=$( ${AWK} '{if ( $1 == "NIMOL_SERVER" ) print $2;}' $NIMOL_CONF )

    if [[ -n $NIMOL_SERVER ]]
    then
        # The NIMOL_SERVER hostname already exists, exit
	[[ -n $ns_existing ]] && \
	err 1 "The NIMOL_SERVER hostname \"%s\" already exists in %s.\n" "${ns_existing}" "$NIMOL_CONF"
    else
        if [[ -z $ns_existing ]] 
	then
	    NIMOL_SERVER=$( ${HOSTNAME} )
	    if [[ -z $NIMOL_SERVER ]]
	    then
		err 1 "Unable to determine the hostname of the NIMOL server.\n"
	    else
		$ECHO "NIMOL_SERVER $NIMOL_SERVER" >>$NIMOL_CONF
	    fi
	else
	    NIMOL_SERVER=$ns_existing
	fi
    fi
    
    # The nimol.conf file already exists, so don't do any configuration of services
    DONT_CONFIG=yes
fi

get_ipaddr_from_hostname ${NIMOL_SERVER}
[[ -z $IP_OUT ]] && err 1 "Unable to determine IP address for hostname \"%s\".  Please update /etc/hosts or dns.\n" "${NIMOL_SERVER}"
NIMOL_SERVER_IP=${IP_OUT}

#
# Start nfs
#

# first, make sure that portmap is running
${PORTMAP} status >/dev/null
if [[ $? -ne 0 ]]
then
    ${PORTMAP} start 2>$ERR || cmd_err 1 "${PORTMAP} start"
    $ECHO "PORTMAP_STARTED yes" >>$NIMOL_CONF
    PORTMAP_SUCCESS=yes
fi

#
# On the HMC, call nfsserver.  On Redhat, call nfs
#
if [[ -s ${NFSSERVER} ]]
then
    # now start the nfssever
    ${NFSSERVER} status >/dev/null
    if [[ $? -ne 0 ]]
    then
	${NFSSERVER} start 2>$ERR || cmd_err 1 "${NFSSERVER} start"
	$ECHO "NFSSERVER_STARTED yes" >>$NIMOL_CONF
	NFSSERVER_SUCCESS=yes
    fi
else
    # the status for nfs does not return a non-zero value if stopped
    ${NFS} status >${TMPDIR}/nfs.status 2>$ERR || cmd_warn "${NFS} status"
    if [[ -s ${TMPDIR}/nfs.status ]]
    then
	if ${GREP} "stopped" ${TMPDIR}/nfs.status >/dev/null 2>/dev/null
	then
	    $ECHO "NFS_STARTED yes" >>$NIMOL_CONF
	fi
    fi
    ${NFS} start || err 1 "Failed to start %s.\n" "nfs"
    NFS_SUCCESS=yes
fi

#
# Add remote access method variable to nimol.conf if not already there
#
existing_comm_meth=$( ${AWK} '{if ( $1 == "REMOTE_ACCESS_METHOD" ) print $2;}' $NIMOL_CONF )
if [[ -n $COMM_METH ]] 
then
    if [[ -n $existing_comm_meth ]]
    then
	# remove the old remote access method and add the new one
	if ${AWK} '{if (!($1 == "REMOTE_ACCESS_METHOD" )) print $0;}' $NIMOL_CONF >${TMPDIR}/nimol.conf.tmp 2>$ERR
	then
	    ${MV} ${TMPDIR}/nimol.conf.tmp $NIMOL_CONF 2>$ERR || cmd_err 1 "${MV} ${TMPDIR}/nimol.conf.tmp $NIMOL_CONF"
	else
	    cmd_warn "${AWK}" 1
	fi
	msg "Removed \"%s\" from the file \"%s\"\n" "REMOTE_ACCESS_METHOD ${existing_comm_meth}" "$NIMOL_CONF"
    fi

    $ECHO "REMOTE_ACCESS_METHOD ${COMM_METH}" >>$NIMOL_CONF
    msg "Added \"%s\" to the file \"%s\"\n" "REMOTE_ACCESS_METHOD ${COMM_METH}" "$NIMOL_CONF"
else
    if [[ -z $existing_comm_meth ]]
    then
	$ECHO "REMOTE_ACCESS_METHOD ${RSH}" >>$NIMOL_CONF
	msg "Added \"%s\" to the file \"%s\"\n" "REMOTE_ACCESS_METHOD ${RSH}" "$NIMOL_CONF"
    fi
fi

#
# Setup tftpboot
#
if [[ -z $DONT_CONFIG ]]
then

    [[ ! -s /usr/sbin/in.tftpd ]] && \
	err 1 "/usr/sbin/in.tftpd does not exist.  Install the tftp-server rpm.\n"
    [[ ! -s /etc/xinetd.d/tftp ]] && mk_tftp

    [[ ! -s ${PERM_TMP}/tftp ]] && ${CP} /etc/xinetd.d/tftp ${PERM_TMP}/tftp

    # create the /tftpboot directory if it doesn't exists
    if [[ ! -d /tftpboot ]]; then
	${MKDIR} /tftpboot || cmd_err 1 "${MKDIR} /tftpboot"
        msg "Created %s.\n" "/tftpboot"
    fi

    # Determine if tftp is passed "-s /tftpboot".  if yes, set NIMOL_TFTPBOOT
    if ${GREP} -e "^[[:space:]*]*server_args.*-s.*/tftpboot" /etc/xinetd.d/tftp >/dev/null 2>/dev/null
    then
	$ECHO "NIMOL_TFTPBOOT /tftpboot" >>$NIMOL_CONF
    fi


    if ${GREP} -e "^[[:space:]*]*disable.*no" /etc/xinetd.d/tftp >/dev/null 2>/dev/null
    then
	:
    elif ${GREP} -e "^[[:space:]*]*disable.*yes" /etc/xinetd.d/tftp >/dev/null 2>/dev/null
    then
	TFTP_DISABLED=yes

	if ${AWK} '{if ($1 == "disable") print "\tdisable\t\t\t= no"; else print $0}' \
	    /etc/xinetd.d/tftp >${TMPDIR}/tftp.tmp 2>$ERR
	then
	    ${MV} ${TMPDIR}/tftp.tmp /etc/xinetd.d/tftp 2>$ERR || \
		cmd_err 1 "${MV} ${TMPDIR}/tftp.tmp /etc/xinetd.d/tftp"
	    TFTP_ENABLED=yes
	    msg "Removed \"%s\" from the file \"%s\"\n" "disable = yes" "/etc/xinetd.d/tftp"
	    msg "Added \"%s\" to the file \"%s\"\n" "disable = no" "/etc/xinetd.d/tftp"
	else
	    cmd_err "${AWK}" 1
	fi
    else
	if ${AWK} '{if ($1 == "server") print $0 "\n\tdisable\t\t\t= no"; else print $0}' \
	    /etc/xinetd.d/tftp >${TMPDIR}/tftp.tmp 2>$ERR
	then
	    ${MV} ${TMPDIR}/tftp.tmp /etc/xinetd.d/tftp 2>$ERR || \
		cmd_err 1 "${MV} ${TMPDIR}/tftp.tmp /etc/xinetd.d/tftp"
	    TFTP_ENABLED=yes
	    msg "Added \"%s\" to the file \"%s\"\n" "disable = no" "/etc/xinetd.d/tftp"
	else
	    cmd_err "${AWK}" 1
	fi
    fi

    [[ -n $TFTP_ENABLED ]] && $ECHO "TFTP_ENABLED yes" >>$NIMOL_CONF

    $XINETD restart || err 1 "Failed to restart %s.\n" "xinetd"
fi


#
# Setup syslog
#
if [[ $CONFIG_SYSLOG != "no" && -z $DONT_CONFIG ]]
then
    [[ ! -s /etc/sysconfig/syslog ]] && err 1 "The file %s does not exist.\n" "/etc/sysconfig/syslog"
    [[ ! -s /etc/syslog.conf ]] && err 1 "The file %s does not exist.\n" "/etc/syslog.conf"
    
    # Determine whether the variable is SYSLOGD_OPTIONS (Redhat) or SYSLOGD_PARAMS (HMC)
    # This function will set the global variable SYSLOGD_VAR with the correct name
    get_syslogd_param_var

    SYSLOGD_OPTIONS=$( ${AWK} 'BEGIN {FS="="} {if ($1 == var) print $2}' var="${SYSLOGD_VAR}" /etc/sysconfig/syslog )
    if 	[[ ${SYSLOGD_OPTIONS} != *-r* ]]
    then
	[[ ! -s ${PERM_TMP}/syslog ]] && ${CP} /etc/sysconfig/syslog ${PERM_TMP}/syslog

	# remove the quotes
	SYSLOGD_OPTIONS=${SYSLOGD_OPTIONS#\"}
	SYSLOGD_OPTIONS=${SYSLOGD_OPTIONS%\"}

	SYSLOGD_OPTIONS="-r ${SYSLOGD_OPTIONS}"

	if ${AWK} 'BEGIN {FS="="} {if ($1 == var) printf ("%s=\"%s\"\n", var, opts); else print $0; }' var="${SYSLOGD_VAR}" opts="${SYSLOGD_OPTIONS}" /etc/sysconfig/syslog >${TMPDIR}/syslog.tmp 2>$ERR
	then
	    ${MV} /${TMPDIR}/syslog.tmp /etc/sysconfig/syslog 2>$ERR || \
		cmd_err 1 "${MV} ${TMPDIR}/syslog.tmp /etc/sysconfig/syslog"

	    SYSLOG_SUCCESS=yes
	    $ECHO "REMOTE_SYSLOG_ENABLED yes" >>$NIMOL_CONF

	    msg "Removed \"%s\" from the file \"%s\"\n" "${SYSLOGD_VAR}=${SYSLOGD_OPTIONS#-r }" "/etc/sysconfig/syslog"
	    msg "Added \"%s\" to the file \"%s\"\n" "${SYSLOGD_VAR}=$SYSLOGD_OPTIONS" "/etc/sysconfig/syslog"
	else
	    cmd_err 1 "${AWK}"
	fi
    fi

    if ! ${GREP} "nimol.log" /etc/syslog.conf >/dev/null 2>/dev/null
    then
	[[ ! -s ${PERM_TMP}/syslog.conf ]] && ${CP} /etc/syslog.conf ${PERM_TMP}/syslog.conf
	# add the log for nimol

	# if we are on the HMC then we must remove one of the syslog lines to add nimol.log
	if ${GREP} -e "^[[:space:]*]*local2,local3\.\*[[:space:]*]*-/var/log/localmessages" /etc/syslog.conf >/dev/null 2>/dev/null
	then    
	    if ${AWK} '{if ($1 == "local2,local3.*" && $2 == "-/var/log/localmessages") \
		print "local3.*\t\t\t-/var/log/localmessages"; \
		else print $0; }' /etc/syslog.conf >${TMPDIR}/syslog.conf.tmp 2>$ERR
	    then
		${MV} /${TMPDIR}/syslog.conf.tmp /etc/syslog.conf 2>$ERR || \
		    cmd_err 1 "${MV} ${TMPDIR}/syslog.conf.tmp /etc/syslog.conf"

		HMC_SYSLOG_CONF_MODIFIED=yes
		$ECHO "HMC_SYSLOG_CONF_MODIFIED yes" >>$NIMOL_CONF
		NIMOL_SYSLOG_FACILITY=local2

		msg "Removed \"%s\" from the file \"%s\"\n" "local2,local3.*  -/var/log/localmessages" "/etc/syslog.conf"
		msg "Added \"%s\" to the file \"%s\"\n" "local3.*  -/var/log/localmessages" "/etc/syslog.conf"
	    else
		cmd_err 1 "${AWK}"
	    fi
	else
	    # we are not on the HMC, so search for an available log
	    for num in 0 1 2 3 4 5 6 7
	    do
	        if ! ${GREP} -e "^[[:space:]*]*local${num}.*" /etc/syslog.conf >/dev/null 2>/dev/null
		then
		    NIMOL_SYSLOG_FACILITY="local${num}"
		    break
		fi
	    done
	fi

	[[ -z ${NIMOL_SYSLOG_FACILITY} ]] && \
	    err 1 "Unable to find an available syslog.\n"
	$ECHO "${NIMOL_SYSLOG_FACILITY}.*\t\t\t/var/log/nimol.log" >>/etc/syslog.conf

	NIMOL_LOG_SUCCESS=yes
	msg "Added \"%s\" to the file \"%s\"\n" "${NIMOL_SYSLOG_FACILITY}.* /var/log/nimol.log" "/etc/syslog.conf"

    else
	NIMOL_SYSLOG_FACILITY=$( ${GREP} "nimol.log" /etc/syslog.conf 2>/dev/null | ${AWK} '{ print $1;}' )
	[[ $NIMOL_SYSLOG_FACILITY = "#"* ]] && err 1 "Uncomment the nimol.log in the file %s.\n" "/etc/syslog.conf"

	# remove any .* from the syslog facility (e.g. local6.* becomes local6)
	NIMOL_SYSLOG_FACILITY=${NIMOL_SYSLOG_FACILITY%.*}
    fi

    $ECHO "NIMOL_SYSLOG_FACILITY ${NIMOL_SYSLOG_FACILITY}" >>$NIMOL_CONF

    ${SYSLOG} restart || err 1 "Failed to restart %s.\n" "${SYSLOG}"
fi


#
# Setup DHCP or nimol_bootreply
#

# deterime the subnet and netmask for the NIMOL server
NIMOL_NETMASK=$( ${IFCONFIG} | ${GREP} ${NIMOL_SERVER_IP} | ${AWK} '{ print $4; }' )
NIMOL_NETMASK=${NIMOL_NETMASK#Mask:}
[[ -z $NIMOL_NETMASK ]] && err 1 "Unable to determine the \"%s\" for \"%s\".\n" "netmask" "$NIMOL_SERVER_IP"

get_subnet ${NIMOL_SERVER_IP} ${NIMOL_NETMASK}
[[ -z ${SUBNET} ]] && cmd_err 1 "Unable to determine the \"%s\" for \"%s\".\n" "subnet" ${NIMOL_SERVER_IP}
NIMOL_SUBNET=${SUBNET}

$ECHO "NIMOL_SUBNET ${NIMOL_SUBNET}" >>$NIMOL_CONF

if [[ -z $DONT_CONFIG && $NO_DHCP != "yes" ]]
then
  if [[ ! -s /etc/dhcpd.conf ]]
  then

    $ECHO "allow bootp; # nimol" >>/etc/dhcpd.conf
    $ECHO "not authoritative; # nimol" >>/etc/dhcpd.conf
    $ECHO "ddns-update-style none; # nimol\n" >>/etc/dhcpd.conf
    $ECHO "subnet $NIMOL_SUBNET netmask $NIMOL_NETMASK { # nimol" >>/etc/dhcpd.conf
    $ECHO "}\n" >>/etc/dhcpd.conf
    msg "Created %s.\n" "/etc/dhcpd.conf"
    DHCPD_CONF_CREATED=yes

    $ECHO "DHCPD_CONF_CREATED yes" >>$NIMOL_CONF
  else

    [[ ! -s ${PERM_TMP}/dhcpd.conf ]] && ${CP} /etc/dhcpd.conf ${PERM_TMP}/dhcpd.conf

    # copy the dhcpd.conf and modify that
    ${CP} /etc/dhcpd.conf ${TMPDIR}/dhcpd.conf.tmp 2>$ERR || \
	cmd_err 1 "${CP} /etc/dhcpd.conf ${TMPDIR}/dhcpd.conf.tmp"

    # add the NIMOL subnet if it does not exist
    if ! ${GREP} -e "subnet[[:space:]*]*${NIMOL_SUBNET}" ${TMPDIR}/dhcpd.conf.tmp >/dev/null 2>/dev/null
    then
	# add "subnet $NIMOL_SUBNET" to the end of the file
	$ECHO "\nsubnet $NIMOL_SUBNET netmask $NIMOL_NETMASK { # nimol" >>${TMPDIR}/dhcpd.conf.tmp
	$ECHO "}\n" >>${TMPDIR}/dhcpd.conf.tmp

        $ECHO "DHCP_SUBNET_ADDED ${NIMOL_SUBNET}" >>$NIMOL_CONF
	msg "Added \"%s\" to the file \"%s\"\n" "subnet $NIMOL_SUBNET netmask $NIMOL_NETMASK" "/etc/dhcpd.conf"
    fi

    # add "allow bootp" to the dhcpd.conf file
    if ! ${GREP} -e "^[[:space:]*]*allow[[:space:]*]bootp" ${TMPDIR}/dhcpd.conf.tmp >/dev/null 2>/dev/null
    then
	# add "allow bootp" to the beginning of the file
	${MV} ${TMPDIR}/dhcpd.conf.tmp ${TMPDIR}/dhcpd.conf.tmp.1 2>$ERR || \
	    cmd_err 1 "${MV} ${TMPDIR}/dhcpd.conf.tmp ${TMPDIR}/dhcpd.conf.tmp.1"
	$ECHO "allow bootp; # nimol" >${TMPDIR}/dhcpd.conf.tmp
	${CAT} ${TMPDIR}/dhcpd.conf.tmp.1 >>${TMPDIR}/dhcpd.conf.tmp
	${RM} -f ${TMPDIR}/dhcpd.conf.tmp.1

        $ECHO "DHCP_BOOTP_ENABLED yes" >>$NIMOL_CONF
	msg "Added \"%s\" to the file \"%s\"\n" "allow bootp;" "/etc/dhcpd.conf"
    fi

    # remove deny or ignore bootp lines - may want to print a warning
    if ${GREP} -e "^[[:space:]*]*deny[[:space:]*]bootp" ${TMPDIR}/dhcpd.conf.tmp >/dev/null 2>/dev/null
    then
	# remove deny - may want to print a warning
	if ${AWK} '{if (!( $0 ~ /^[ \t\f\n\r\v]*deny[ \t\f\n\r\v]+bootp/ )) print $0; }' \
		${TMPDIR}/dhcpd.conf.tmp >${TMPDIR}/dhcpd.conf.tmp.1 2>$ERR
	then
	    ${MV} ${TMPDIR}/dhcpd.conf.tmp.1 ${TMPDIR}/dhcpd.conf.tmp 2>$ERR || \
		cmd_err 1 "${MV} ${TMPDIR}/dhcpd.conf.tmp.1 ${TMPDIR}/dhcpd.conf.tmp"
	    msg "Removed \"%s\" from the file \"%s\"\n" "deny bootp;" "/etc/dhcpd.conf"
	    $ECHO "DHCP_BOOTP_PREVIOUS deny" >>$NIMOL_CONF
	else
	    cmd_err 1 "${AWK}"
	fi
    elif ${GREP} -e "^[[:space:]*]*ignore[[:space:]*]bootp" ${TMPDIR}/dhcpd.conf.tmp >/dev/null 2>/dev/null
    then
	# remove ignore - may want to print a warning
	if ${AWK} '{if (!( $0 ~ /^[ \t\f\n\r\v]*ignore[ \t\f\n\r\v]+bootp/ )) print $0; }' \
		${TMPDIR}/dhcpd.conf.tmp >${TMPDIR}/dhcpd.conf.tmp.1 2>$ERR
	then
	    ${MV} ${TMPDIR}/dhcpd.conf.tmp.1 ${TMPDIR}/dhcpd.conf.tmp 2>$ERR || \
		cmd_err 1 "${MV} ${TMPDIR}/dhcpd.conf.tmp.1 ${TMPDIR}/dhcpd.conf.tmp"
	    msg "Removed \"%s\" from the file \"%s\"\n" "ignore bootp;" "/etc/dhcpd.conf"
	    $ECHO "DHCP_BOOTP_PREVIOUS ignore" >>$NIMOL_CONF
	else
	    cmd_err 1 "${AWK}"
	fi
    fi

    # add "not authoritative" to the dhcpd.conf file
    if ! ${GREP} -e "^[[:space:]*]*not[[:space:]*]authoritative" ${TMPDIR}/dhcpd.conf.tmp >/dev/null 2>/dev/null
    then
	# add "not authoritative;" to the beginning of the file
	${MV} ${TMPDIR}/dhcpd.conf.tmp ${TMPDIR}/dhcpd.conf.tmp.1 2>$ERR || \
	    cmd_err 1 "${MV} ${TMPDIR}/dhcpd.conf.tmp ${TMPDIR}/dhcpd.conf.tmp.1"
	$ECHO "not authoritative; # nimol" >${TMPDIR}/dhcpd.conf.tmp
	${CAT} ${TMPDIR}/dhcpd.conf.tmp.1 >>${TMPDIR}/dhcpd.conf.tmp
	${RM} -f ${TMPDIR}/dhcpd.conf.tmp.1
	msg "Added \"%s\" to the file \"%s\"\n" "not authoritative;" "/etc/dhcpd.conf"

	$ECHO "DHCP_NOT_AUTHORITATIVE_ADDED yes" >>$NIMOL_CONF
    fi

    ${MV} ${TMPDIR}/dhcpd.conf.tmp /etc/dhcpd.conf 2>$ERR || \
	cmd_err 1 "${MV} ${TMPDIR}/dhcpd.conf.tmp /etc/dhcpd.conf"

    DHCP_SUCCESS=yes

  fi

    # make sure DHCPD_INTERFACE is set in /etc/sysconfig/dhcpd
    if [[ -s /etc/sysconfig/dhcpd ]]
    then
	dhcpd_if=

	[[ ! -s ${PERM_TMP}/dhcpd ]] && ${CP} /etc/sysconfig/dhcpd ${PERM_TMP}/dhcpd

	get_nimol_interface

        # copy the file and modify that one
	${CP} /etc/sysconfig/dhcpd ${TMPDIR}/dhcpd.tmp 2>$ERR || \
	    cmd_err 1 "${CP} /etc/sysconfig/dhcpd ${TMPDIR}/dhcpd.tmp"

	if ! ${GREP} -e "^[[:space:]*]*DHCPD_INTERFACE" ${TMPDIR}/dhcpd.tmp >/dev/null 2>/dev/null
	then
	    # add "DHCPD_INTERFACE" to the end of the file
	    $ECHO "DHCPD_INTERFACE=\"${NIMOL_INTERFACE}\"" >>${TMPDIR}/dhcpd.tmp
	    msg "Added \"%s\" to the file \"%s\"\n" "DHCPD_INTERFACE=\"${NIMOL_INTERFACE}\"" "/etc/sysconfig/dhcpd"

	    $ECHO "DHCPD_INTERFACE_ADDED yes" >>$NIMOL_CONF
	    DHCPD_INTERFACE_ADDED=yes

	else
	    # make sure the interface matches the nimol interface
	    dhcpd_if=$( ${GREP} -e "^[[:space:]*]*DHCPD_INTERFACE" ${TMPDIR}/dhcpd.tmp )
	    dhcpd_if=${dhcpd_if#*=}
	    dhcpd_if=${dhcpd_if#\"}
	    dhcpd_if=${dhcpd_if%\"}

	    if [[ -z ${dhcpd_if} ]]
	    then
		if ${AWK} '{if ( $0 ~ /^[ \t\f\n\r\v]*DHCPD_INTERFACE*/ ) \
		    printf ("DHCPD_INTERFACE=%s\n", nimol_if); else print $0; }' \
		    nimol_if="${NIMOL_INTERFACE}" ${TMPDIR}/dhcpd.tmp >${TMPDIR}/dhcpd.tmp1 2>$ERR
		then
		    ${MV} ${TMPDIR}/dhcpd.tmp1 ${TMPDIR}/dhcpd.tmp 2>$ERR || \
			cmd_err 1 "${MV} ${TMPDIR}/dhcpd.tmp1 ${TMPDIR}/dhcpd.tmp"
		else
		    cmd_err 1 "${AWK} DHCPD_INTERFACE"
		fi

		$ECHO "DHCPD_INTERFACE_ADDED yes" >>$NIMOL_CONF
		DHCPD_INTERFACE_ADDED=yes

	    elif [[ ${dhcpd_if} != ${NIMOL_INTERFACE} ]]
	    then
		cmd_err 1 "%s is set to %s in the file %s.  It should be %s.\n" "DHCPD_INTERFACE" "${dhcpd_if}" "/etc/sysconfig/dhcpd" "$NIMOL_INTERFACE"
	    fi
	fi
	
	if [[ -n $DHCPD_INTERFACE_ADDED ]]
	then
	    ${MV} ${TMPDIR}/dhcpd.tmp /etc/sysconfig/dhcpd 2>$ERR || \
		cmd_err 1 "${MV} ${TMPDIR}/dhcpd.tmp /etc/sysconfig/dhcpd"
	fi
    fi

    ${DHCPD} status >/dev/null 2>/dev/null
    if [[ $? -ne 0 ]]
    then
	${DHCPD} start || err 1 "Failed to start %s.\n" "dhcpd"
	msg "Executed %s.\n" "${DHCPD} start"
	$ECHO "DHCPD_STARTED yes" >>$NIMOL_CONF
	DHCPD_STARTED=yes
    else
	${DHCPD} restart || err 1 "Failed to restart %s.\n" "dhcpd"
	msg "Executed %s.\n" "${DHCPD} restart"
	DHCPD_RESTARTED=yes
    fi

elif [[ -z $DONT_CONFIG && $NO_DHCP = "yes" ]]
then
	if [[ -s $NIMOL_BOOTREPLYD_PID ]]
	then
		file_pid=$( $CAT $NIMOL_BOOTREPLYD_PID )
		running_pid=$( $PS | $SED 1d | $AWK -vpid=$file_pid '$2 == pid {print $2}' )
	fi

	if [[ -n $running_pid ]]
	then
		[[ -s $NIMOL_BOOTREPLYD_PID ]] && pid=$($CAT $NIMOL_BOOTREPLYD_PID) 
		kill $pid || warn "Failed to stop %s.\n" "nimol_bootreplyd"
		msg "Executed %s.\n" "kill $pid"
	fi

	[[ -s $NIMOL_BOOTREPLYD_CONF ]] || >$NIMOL_BOOTREPLYD_CONF
	$NIMOL_BOOTREPLYD_START -s $NIMOL_SERVER || err 1 "Failed to start %s.\n" "nimol_bootreplyd"
	msg "Executed %s.\n" "$NIMOL_BOOTREPLYD_START -s $NIMOL_SERVER"
	STARTED_BOOTREPLY="yes"

	$ECHO 'NIMOL_BOOTREPLYD yes' >>$NIMOL_CONF
fi


#
# Setup is complete
# don't undo the configuration of nimol; only resource copying
#
[[ -z $DONT_CONFIG ]] && msg "Successfully configured NIMOL.\n"
MK_CONF_SUCCESS=
TFTP_DISABLED=
TFTP_ENABLED=
SYSLOG_SUCCESS=
NIMOL_LOG_SUCCESS=
DHCP_SUCCESS=
DHCPD_CONF_CREATED=
DHCPD_STARTED=
DHCPD_RESTARTED=
NFS_SUCCESS=
NFSSERVER_SUCCESS=
PORTMAP_SUCCESS=
MK_PERM_TMP_SUCCESS=

[[ -n $ONLY_CONFIG_NIMOL ]] && exit 0

####################################################
#
# CREATE THE LABEL AND COPY RESOURCES
#
####################################################

#
# Specify the defaults if variables aren't set.
#
[[ -z ${LABEL} ]] && LABEL="default"

# ensure the label doesn't already exist
if [[ -s $NIMOL_CONF ]]
then
	if ! ${AWK} '{if ( $1 == "LABEL" && $2 == label ) exit 1;}' label="$LABEL" $NIMOL_CONF
	then
		# the label already exists, exit
		err 1 "The label \"%s\" already exists.\n" "${LABEL}"
	fi
fi


# Target directory
TARGET_DIR=${TARGET_DIR}/$LABEL
msg "target directory: %s\n" "$TARGET_DIR"

#
# If the target directory exists and no source directory was specified
# then check if there are existing resources
#
if [[ -d ${TARGET_DIR} && -z ${SOURCE_DIR} ]]
then
    check_for_resources ${TARGET_DIR}
fi

#
# Copy the resources
#
if [[ -z $DONT_COPY ]]
then
    [[ -z $SOURCE_DIR ]] && SOURCE_DIR=/dev/cdrom

    if [[ $SOURCE_DIR = /dev/* ]]; then

	export FROM_CD="yes" #installing from some sort of device

	${MKDIR} /mnt/nimol >/dev/null 2>/dev/null

	${MOUNT} $SOURCE_DIR /mnt/nimol 2>$ERR || cmd_err 1 "${MOUNT} $SOURCE_DIR /mnt/nimol"

	SOURCE_DIR=/mnt/nimol
	MNT_SUCCESS=yes
    elif [[ $SOURCE_DIR = *.iso || $SOURCE_DIR = *.ISO ]]; then

	export FROM_CD="yes" #installing from an ISO image

	${MKDIR} /mnt/nimol >/dev/null 2>/dev/null

	${MOUNT} -o loop $SOURCE_DIR /mnt/nimol 2>$ERR || cmd_err 1 "${MOUNT} $SOURCE_DIR /mnt/nimol"

	SOURCE_DIR=/mnt/nimol
	MNT_SUCCESS=yes
    elif [[ $SOURCE_DIR = *:* ]]; then

	SOURCE_SYS=${SOURCE_DIR%:*}

        #
        # Modify the firewall
        #
        if [[ -s ${IPTABLES} ]]
        then
            # accept all packets from the host
            ${IPTABLES} -I INPUT 1 -s $SOURCE_SYS -j ACCEPT || cmd_warn "${IPTABLES} -I INPUT 1 -s $SOURCE_SYS -j ACCEPT"
            IPTABLES_SUCCESS="yes"
            msg "Executed %s.\n" "${IPTABLES} -I INPUT 1 -s $SOURCE_SYS -j ACCEPT"
        fi

	${MKDIR} /mnt/nimol >/dev/null 2>/dev/null

	${MOUNT} $SOURCE_DIR /mnt/nimol 2>$ERR || cmd_err 1 "${MOUNT} $SOURCE_DIR /mnt/nimol"

	SOURCE_DIR=/mnt/nimol
	MNT_SUCCESS=yes
    fi

    msg "source directory: %s\n" "$SOURCE_DIR"

    #before attempting to extract the resources, and if the source is a CD/DVD,
    #see if the source conatins an aix-res RPM and install it if so
    [[ $FROM_CD = "yes" && -z $AIX_VERSION ]] && install_aix_res_rpm $SOURCE_DIR

    # attempt to extract the NIM resources from a tar file first
    extract_nim_res "${SOURCE_DIR}"

    # attempt to copy resources
    [[ -z ${EXTRACT_SUCCESSFUL} ]] && copy_resources
 
    if [[ -n $IPTABLES_SUCCESS ]]; then
        ${IPTABLES} -D INPUT -s $SOURCE_SYS -j ACCEPT
        msg "Executed %s.\n" "${IPTABLES} -D INPUT -s $SOURCE_SYS -j ACCEPT"
    fi

	# Check for additional resources to copy
	if [[ -n $AUX_SOURCE_DIR ]]
	then
		# Make sure we're at 5300-03 or greater
		aix_min_level="5300-03"
        echo $aix_min_level >/tmp/$$.aixcmp
        echo $AIX_VERSION >>/tmp/$$.aixcmp 
		$SORT /tmp/$$.aixcmp >/tmp/$$.aixcmp2 2>/dev/null || cmd_warn $SORT
        $MV /tmp/$$.aixcmp2 /tmp/$$.aixcmp 2>/dev/null || cmd_warn $MV

        # here, we grab the first line of the temp file
        file_line=`$CAT /tmp/$$.aixcmp | while read line; do echo $line;break; done`
        $RM -f /tmp/$$.aixcmp >/dev/null 2>&1

        # The sort will have put the lowest level at the first line of this file
        # (which should only contain two lines).  If the low one = aix_min_level, then
        # we are at least at 5300-03, copying of additional resources is supported
        if [[ $file_line = $aix_min_level ]]
		then
			if [[ ! -d ${AUX_SOURCE_DIR} ]]
			then
				err 1 "The file %s does not exist.\n" "${AUX_SOURCE_DIR}"
			fi

			if [[ ! -d ${TARGET_DIR}/${LPP_SOURCE} ]]
			then
				err 1 "The file %s does not exist.\n" "${TARGET_DIR}/${LPP_SOURCE}"
			fi

		    if [[ -d ${AUX_SOURCE_DIR}/installp/ppc || -d ${AUX_SOURCE_DIR}/RPMS/ppc || \
			      -d ${AUX_SOURCE_DIR}/ISMP/ppc || -d ${AUX_SOURCE_DIR}/efixes ]]
			then
			# If one of these 4 dirs exists, then copy contents of AUX_SOURCE_DIR
			# to TARGET_DIR/LPP_SOURCE
			    msg "Copying %s to %s...\n" "${AUX_SOURCE_DIR}" "${TARGET_DIR}/${LPP_SOURCE}"
				(cd ${AUX_SOURCE_DIR} && $TAR -cf - * 2>$ERR) | \
				(cd ${TARGET_DIR}/${LPP_SOURCE} && $TAR -xf - 2>$ERR) || \
				cmd_warn "$TAR"
			else
			# Copy contents of AUX_SOURCE_DIR to TARGET_DIR/LPP_SOURCE/installp/ppc
				msg "Copying %s to %s...\n" "${AUX_SOURCE_DIR}" "${TARGET_DIR}/${LPP_SOURCE}/installp/ppc"
				(cd ${AUX_SOURCE_DIR} && $TAR -cf - * 2>$ERR) | \
				(cd ${TARGET_DIR}/${LPP_SOURCE}/installp/ppc && $TAR -xf - 2>$ERR) || \
				cmd_warn "$TAR"
			fi
	
			# Remove existing .toc file in label
			$RM -f ${TARGET_DIR}/${LPP_SOURCE}/installp/ppc/.toc >/dev/null 2>&1
		fi
	fi
fi

# Add the label to the nimol.conf file
$ECHO "LABEL $LABEL $TARGET_DIR" >>$NIMOL_CONF

#
# export the target directory
#
if [[ -z ${DONT_GLOBAL_EXPORT} ]]; then

    dir_exists=

    if [[ -s /etc/exports ]]
    then
	dir_exists=$( ${AWK} '{if ($1 == dir && $2 == "*(rw,insecure,no_root_squash)") print $0;}' dir="${TARGET_DIR}" /etc/exports 2>$ERR )

	[[ $? -ne 0 ]] && cmd_warn "${AWK}"
    fi
   
    if [[ -z $dir_exists ]]
    then
	$ECHO "\n${TARGET_DIR} *(rw,insecure,no_root_squash)" >> /etc/exports
	NFS_EXPORT_SUCCESS=yes
	msg "Added \"%s\" to the file \"%s\"\n" "${TARGET_DIR} *(rw,insecure,no_root_squash)" "/etc/exports"
    else 
	msg "Directory \"%s\" already exported.\n" "${TARGET_DIR}"
    fi
fi
${EXPORTFS} -a 2>$ERR || cmd_warn "${EXPORTFS} -a"


msg "Successfully created \"%s\".\n" "$LABEL"
exit 0
