#!/bin/sh

. /usr/lib/nimol_lib


#------------------------------ local defines ---------------------------------
COMM_METH=
NIMOL_SERVER=
SOURCE_DIR=
CLIENT=
TARGET_DIR=/export/aix
LABEL=

NFS_EXPORT_SUCCESS=
IPTABLES_SUCCESS=
MK_TDIR_SUCCESS=

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

if [[ -n $NFS_EXPORT_SUCCESS ]]
then
    ${EXPORTFS} -u ${CLIENT}:${TARGET_DIR} 2>$ERR || \
	cmd_warn "${EXPORTFS} -u ${CLIENT}:${TARGET_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
   
if [[ -n $MK_TDIR_SUCCESS ]]; then
    ${RM} -fr $TARGET_DIR
    msg "Removed %s.\n" "$TARGET_DIR"
fi

return
}


################################################################################
#
#  rename_resources - rename resources to correct names
#
#  parameters:
#
################################################################################
function rename_resources {

    # Move to the target directory
    cd ${TARGET_DIR}

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

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

    return 0
}


################################################################################
#
#  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:t:L:m:D c
do
	case ${c} in

        c)		# client hostname
				CLIENT=$OPTARG
				;;

        D)		# debug output
				set -x
				;;

        L)		# label
				LABEL=$OPTARG
				;;

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

        t)		# target directory on NIMOL server - copy resources here
				TARGET_DIR=$OPTARG
				;;

	\?)		# unknown option
				usage "\nUsage nimol_backup: Create an installable backup of an AIX client\n      nimol_backup       -c <client hostname>\n                         [-t <target directory to copy resources>]\n                         [-m <remote access method>]\n                         [-L <install resources label>] [-D]\n\nExample:\n  nimol_backup -c client1 -L 530  # create a backup of client1 named 530\n\ndefaults:\n\t-t    /export/aix\n\t-L    default\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 $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

# set the target directory
TARGET_DIR=${TARGET_DIR}/${LABEL}

if [[ ! -d ${TARGET_DIR} ]]; then
    ${MKDIR} -p $TARGET_DIR 2>$ERR || cmd_err 1 "${MKDIR}"
    MK_TDIR_SUCCESS=yes
fi 

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


#
# 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
#
output=$( ${AWK} '{if ( $1 == dir && $2 == "*(rw,insecure,no_root_squash)" ) print $1;}' dir="${TARGET_DIR}" /etc/exports )
if [[ ${output} != ${TARGET_DIR} ]]
then
    ${EXPORTFS} -i -o rw,insecure,no_root_squash ${CLIENT}:${TARGET_DIR} 2>$ERR || \
	cmd_err 1 "${EXPORTFS} -i -o rw,insecure,no_root_squash ${CLIENT}:${TARGET_DIR}"
    NFS_EXPORT_SUCCESS=yes
fi


#
# determine the 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 bootpkg to create the tar file of NIM resources
$COMM_METH $CLIENT "/usr/sbin/bootpkg -l ${NIMOL_SERVER}:${TARGET_DIR} -d ent -N \
        -b /var/adm/ras/bosinst.data -r /etc/resolv.conf -T -B; 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 "/usr/sbin/bootpkg on $CLIENT"
fi

# Rename the resources
rename_resources

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

if [[ -n $NFS_EXPORT_SUCCESS ]]
then
    ${EXPORTFS} -u ${CLIENT}:${TARGET_DIR} 2>$ERR || \
	cmd_warn "${EXPORTFS} -u ${CLIENT}:${TARGET_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





