#!/bin/sh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# nimol_rplx03a src/nimol/nimol_update.sh 1.4 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2004 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 

. /usr/lib/nimol_lib


#------------------------------ local defines ---------------------------------
COMM_METH=
NIMOL_SERVER=
SOURCE_DIR=
CLIENT=
FILESETS=
LABEL=
GENINSTALL_FLAGS=
DEBUG=

NFS_EXPORT_SUCCESS=
IPTABLES_SUCCESS=


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

if [[ -n $NFS_EXPORT_SUCCESS ]]
then
    ${EXPORTFS} -u ${CLIENT}:${SOURCE_DIR} 2>$ERR || \
	cmd_warn "${EXPORTFS} -u ${CLIENT}:${SOURCE_DIR}"
fi
    
if [[ -n $IPTABLES_SUCCESS ]]; then
    ${IPTABLES} -D INPUT -s $CLIENT -j ACCEPT
    msg "Executed %s.\n" "${IPTABLES} -D INPUT -s $CLIENT -j ACCEPT"
fi
   
return
}


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

    trap "" 0

    ${RM} -rf $TMPDIR

    ${RM} -rf $ERR

} # end of cleanup 

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

# set parameters from command line
while getopts :c:Df:L:m:p: c
do
	case ${c} in

        c)		# client hostname
				CLIENT=$OPTARG
				;;

        D)		# debug output
				DEBUG=yes
				set -x
				;;

        f)		# geninstall flags
				GENINSTALL_FLAGS=$OPTARG
				;;

        L)		# label
				LABEL=$OPTARG
				;;

        m)		# remote access methods
				COMM_METH=$OPTARG
				;;

        p)		# list of filesets to install
				FILESETS=$OPTARG
				;;

	\?)		# unknown option
				usage "\nUsage nimol_update: Install software updates onto an AIX client\n      nimol_update       -c <client hostname>\n                         [-L <install resources label>]\n                         [-f <geninstall flags>]\n                         [-m <remote access method>]\n                         [-p <software packages to install>] [-D]\n\nExample:\n  nimol_update -c client1 -L 530  # install all packages in 530 onto client1\n\ndefaults:\n\t-L    default\n\t-p    all\n\t-m    /usr/bin/rsh\n"

				;;
	esac
done

trap err_signal 1 2 11 15
trap cleanup 0

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

#
# Specify the default values.
#
if [[ -z $CLIENT ]]
then
	err 1 "A hostname must be specified.\n"
fi

[[ -z $FILESETS ]] && FILESETS="all"
[[ -z $LABEL ]] && LABEL="default"
[[ -z $GENINSTALL_FLAGS ]] && GENINSTALL_FLAGS="-acgX"

#
# Determine the hostname of the NIMOL server
#
[[ ! -s $NIMOL_CONF ]] && err 1 "The file %s does not exist.\n" "${NIMOL_CONF}"
NIMOL_SERVER=$( awk '{if ( $1 == "NIMOL_SERVER" ) print $2;}' $NIMOL_CONF )
[[ -z $NIMOL_SERVER ]] && err 1 "Unable to determine the hostname of the NIMOL server.\n"

#
# determine the location of the AIX install resources
#
[[ ! -s $NIMOL_CONF ]] && err 1 "The file %s does not exist.\n" $NIMOL_CONF

SOURCE_DIR=$( ${AWK} '{if ( $1 == "LABEL" && $2 == label ) print $3;}' label=$LABEL $NIMOL_CONF )
if [[ -z $SOURCE_DIR ]]
then
	# the label does not exist, print the valid labels and exit
        VALID_LABELS=$( ${AWK} '{if ( $1 == "LABEL" ) print $2;}' $NIMOL_CONF )
	err 1 "The label \"%s\" does not exist. Valid labels are:\n%s\n" "$LABEL" "$VALID_LABELS"
fi

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

#
# export the target directory if the directory is not globally exported
#
if ! ${AWK} '{if ( $1 == dir && $2 == "*(rw,insecure,no_root_squash)" ) exit 1;}' dir=${SOURCE_DIR} /etc/exports
then
    ${EXPORTFS} -i -o rw,insecure,no_root_squash ${CLIENT}:${SOURCE_DIR} 2>$ERR || \
	cmd_err 1 "${EXPORTFS} -i -o rw,insecure,no_root_squash ${CLIENT}:${SOURCE_DIR}"
    NFS_EXPORT_SUCCESS=yes
fi

#
# determine remote access method
#
if [[ -z ${COMM_METH} ]]
then
    COMM_METH=$( ${AWK} '{if ( $1 == "REMOTE_ACCESS_METHOD" ) print $2;}' $NIMOL_CONF 2>$ERR )
    [[ $? -ne 0 ]] && cmd_err 1 "${AWK} REMOTE_ACCESS_METHOD $NIMOL_CONF"
    [[ -z $COMM_METH ]] && COMM_METH=${RSH}
fi

#
# check that the remote access method works
#
$COMM_METH $CLIENT "echo >/dev/null" 2>$ERR
[[ $? -ne 0 ]] && cmd_err 1 "$COMM_METH $CLIENT"

#
# call c_installp
#
if [[ -z $DEBUG ]]
then
    $COMM_METH $CLIENT "/usr/lpp/bos.sysmgt/nim/methods/c_installp -f -afilesets=\"${FILESETS}\" -alpp_source=${NIMOL_SERVER}:${SOURCE_DIR}/${LPP_SOURCE} -a installp_flags=\"${GENINSTALL_FLAGS}\" -aasync=yes -ashow_progress=yes; echo \"nimol_rc=\$?\"" 2>&1 | ${TEE} $ERR
else
    $COMM_METH $CLIENT "/usr/lpp/bos.sysmgt/nim/methods/c_installp -v -f -afilesets=\"${FILESETS}\" -alpp_source=${NIMOL_SERVER}:${SOURCE_DIR}/${LPP_SOURCE} -a installp_flags=\"${GENINSTALL_FLAGS}\" -aasync=yes -ashow_progress=yes; echo \"nimol_rc=\$?\"" 2>&1 | ${TEE} $ERR
fi

#
# Was the command successful
#
nimol_rc=$( ${CAT} $ERR | ${GREP} "nimol_rc=" )

if [[ ${nimol_rc#*=} != 0 ]]
then
    >$ERR  # don't re-print the output
    cmd_err 1 "c_installp on $CLIENT"
fi

if [[ -n $NFS_EXPORT_SUCCESS ]]
then
    ${EXPORTFS} -u ${CLIENT}:${SOURCE_DIR} 2>$ERR || \
	cmd_err 1 "${EXPORTFS} -u ${CLIENT}:${SOURCE_DIR}"
fi
   
if [[ -n $IPTABLES_SUCCESS ]]; then
    ${IPTABLES} -D INPUT -s $CLIENT -j ACCEPT
    msg "Executed %s.\n" "${IPTABLES} -D INPUT -s $CLIENT -j ACCEPT"
fi
 
exit 0
