#!/bin/sh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# nimol_rplx03a src/nimol/nimol_lslpp.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=
CLIENT=
LSLPP_FLAGS=

IPTABLES_SUCCESS=

################################################################################
#
#  undo - undo changes made
#
#  parameters:
#
################################################################################
function undo {
    
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:f:m:D c
do
	case ${c} in

        c)		# client hostname
				CLIENT=$OPTARG
				;;

        f)		# lslpp flags
				LSLPP_FLAGS=$OPTARG
				;;

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

        D)		# debug output
				set -x
				;;

	\?)		# unknown option
				usage "\nUsage nimol_lslpp: List software installed on an AIX client\n      nimol_lslpp        -c <client hostname>\n                         [-m <remote access method>]\n                         [-f <lslpp flags>] [-D]\n\nExample:\n  nimol_lslpp -c client1 -f \"-L\"  # Pass the -L flag to lslpp on client1\n\ndefaults:\n\t-f    -Lc\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

#
# set default lslpp flags
#
[[ -z $LSLPP_FLAGS ]] && LSLPP_FLAGS="-Lc"

#
# determine remote access method if not passed as a parameter
#
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

#
# 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

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

#
# Execute the command
#
$COMM_METH $CLIENT "${LSLPP} $LSLPP_FLAGS; echo \"nimol_rc=\$?\"" 2>&1 | ${TEE} $ERR

#
# 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 "${LSLPP} on $CLIENT"
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