#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos72L src/bos/usr/lib/nim/methods/libcosi.sh 1.9.1.1 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2006,2017 
# 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 
# @(#)84 1.9.1.1 src/bos/usr/lib/nim/methods/libcosi.sh, cmdnim, bos72L, l2017_30A5 7/25/17 14:37:18
#
#   COMPONENT_NAME: CMDNIM
#
#   FUNCTIONS: ./usr/lib/nim/methods/libcosi.sh
#
#   ORIGINS: 27
#
#
#   (C) COPYRIGHT International Business Machines Corp. 2006
#   All Rights Reserved
#   Licensed Materials - Property of IBM
#   US Government Users Restricted Rights - Use, duplication or
#   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
#

# --------------------------------------------------------------------------- #
# NAME:         ck_nim_env
#
# FUNCTION:     Checks for existing nim environment
#
# PARAMETERS:   None.
#
# RETURNS:      0 - nim environment exist
#               1 - unable to determine environment
# --------------------------------------------------------------------------- #
function ck_nim_env {

        /usr/bin/lslpp -l bos.sysmgt.nim.master >/dev/null 2>&1

        if [[ $? -ne 0 ]]; then
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_CMD_EXEC_MASTER} \
			'%1$s: This command can only be executed on a NIM master.\n' "${PROGNAME}"
                return 1
        else
                #
                # Fileset exist, so determine if NIM is configure
                #
                ck_init || return 1
        fi

        return 0
}

# --------------------------------------------------------------------------- #
# NAME:         ck_init
#
# FUNCTION:     Checks if the NIM master is configured.
#
# PARAMETERS:   None.
#
# RETURNS:      0 - if the NIM master is configured.
#               1 - if the NIM master is not configured.
# --------------------------------------------------------------------------- #
function ck_init {

        ODMDIR=/etc/objrepos /usr/bin/odmget -q "name=master" nim_object 2>/dev/null |
                /usr/bin/grep master > /dev/null

        if [[ $? -ne 0 ]]; then
                /usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_MASTER_NOT_CONFIGURED} \
                        'NIM master not configured.\n'
                return 1
        fi

        return 0
}

# --------------------------------------------------------------------------- #
# NAME:         ck_nim_env_ts
#
# FUNCTION:     Checks for existing nim environment
#
# PARAMETERS:   None.
#
# RETURNS:      0 - nim environment exist
#               1 - unable to determine environment
# --------------------------------------------------------------------------- #
function ck_nim_env_ts {

        /usr/bin/lslpp -l bos.sysmgt.nim.master >/dev/null 2>&1

        if [[ $? -eq 0 ]]; then
                ODMDIR=/etc/objrepos /usr/bin/odmget -q "name=master" nim_object 2>/dev/null | \
                        /usr/bin/grep master > /dev/null

                if [[ $? -eq 0 ]]; then
                        IS_MASTER=yes
                else
			/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_CMD_NOT_MASTER} \
				'%1$s: Machine is not configured as a NIM master.\n' "${PROGNAME}"
                        return 1
                fi
        else
                if [[ $NIM_CONFIGURATION = "diskless" ]] || [[ $NIM_CONFIGURATION = "dataless" ]]; then
                        IS_CLIENT=yes
                else
			/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_THINSERVER_EXECUTE} \
				'%1$s: Only thinserver can execute this command.\n' "${PROGNAME}"
                        return 1
                fi
        fi

        return 0
}

# --------------------------------------------------------------------------- #
# NAME:         rm_nim_obj
#
# FUNCTION:     Remove NIM object by calling nim -o remove
#
# PARAMETERS:   None.
#
# RETURNS:      0 - object removed successfully
#               1 - unable to remove object
# --------------------------------------------------------------------------- #
function rm_nim_obj {

        typeset object=$1

	if [[ -z $object ]]; then
                /usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_CMD_NOT_EXIST} \
                        '%1$s: \"%2$s\" does not exist.\n' "${PROGNAME}" "${object}"
		return 1
	else
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_REMOVING_OBJECT} \
			'Removing %1$s object definition...' "${object}"
	        /usr/sbin/nim -Fo remove ${object} >/dev/null 2>&1
        	if [[ $? -ne 0 ]]; then
			/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_FAILED} 'failed\n' 
			/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_UNABLE_TO_REMOVE} \
				'%1$s: Unable to remove %2$s.\n' "${PROGNAME}" "${object}"
                	return 1
		fi
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DONE} 'done\n'
        fi

        return 0
}

# --------------------------------------------------------------------------- #
# NAME:         rm_file
#
# FUNCTION:     Remove file
#
# PARAMETERS:   None.
#
# RETURNS:      0 - file removed successfully
#               1 - unable to remove file
# --------------------------------------------------------------------------- #
function rm_file {

        typeset file=$1
	typeset command=""

	if [[ -z $file ]]; then
                /usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_CMD_NOT_EXIST} \
                	'%1$s: \"%2$s\" does not exist.\n' "${PROGNAME}" "${file}"
		return 1
	else
        	if [[ -d $file ]]; then
	                command="/usr/bin/rm -r ${file}"
		elif [[ -f $file ]]; then
			command="/usr/bin/rm ${file}"
		else
			return 1
		fi
	fi

        /usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_REMOVING_FILE} \
		'Removing file %1$s...' "${file}" 

	$command >/dev/null 2>&1
	if [[ $? -ne 0 ]]; then
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_FAILED} 'failed\n'
		/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_CANT_REMOVE_FILE} \
			'%1$s: Unable to remove file %2$s.\n' "${PROGNAME}" "${file}"
		return 1
	fi

	/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DONE} 'done\n'

        return 0
}

# --------------------------------------------------------------------------- #
# NAME:         cp_file
#
# FUNCTION:     Copy file
#
# PARAMETERS:   None.
#
# RETURNS:      0 - file copy successfully
#               1 - unable to copy file
# --------------------------------------------------------------------------- #
function cp_file {

        typeset file=$1
	typeset location=$2
        typeset command=""

	if [[ -z $file || -z $location ]]; then
                /usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_INVALID_SOURCE} \
                	'%1$s: Invalid source.\n' "${PROGNAME}"
		return 1
	else
	        if [[ -d $file ]]; then
        	        command="/usr/bin/cp -r ${file} $location"
	        elif [[ -f $file ]]; then
        	        command="/usr/bin/cp ${file} $location"
		else 
			return 1
	        fi
	fi

        /usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_COPY_FROM_TO} \
		'Copying from %1$s to %2$s...' "${file}" "${location}" 

        $command >/dev/null 2>&1
        if [[ $? -ne 0 ]]; then
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_FAILED} 'failed\n'
		/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_UNABLE_COPY_FROM_TO} \
			'%1$s: Unable to copy from %2$s to %3$s.\n' "${PROGNAME}" "${file}" "${location}"
                return 1
        fi

        /usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DONE} 'done\n'

        return 0
}

# --------------------------------------------------------------------------- #
# NAME:         mkpath
#
# FUNCTION:     Make path location
#
# PARAMETERS:   None.
#
# RETURNS:      0 - path created successfully
#               1 - unable to create path
# --------------------------------------------------------------------------- #
function mkpath {

        path=$1

        if [[ -z $path ]]; then
                /usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_CMD_NOT_EXIST} \
                	'%1$s: \"%2$s\" does not exist.\n' "${PROGNAME}" "${path}"
                return 1
        else
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_C_CREATING_DIR} \
                        'Creating the %s directory.\n' "${path}" 

                /usr/bin/mkdir -p $path
                if [[ $? -ne 0 ]]; then
                        /usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_CREATE_DIR} \
                                '%1$s: Unable to create %2$s directory.\n' "${PROGNAME}" "${path}"
                        return 1
                fi
        fi

        return 0
}

# --------------------------------------------------------------------------- #
# NAME:         get_cosi_thinserver
#
# FUNCTION:     Get thinserver using the common image
#
# PARAMETERS:   None.
#
# RETURNS:      0 - get thinserver using the common image.
#               1 - command failure.
# --------------------------------------------------------------------------- #
function get_cosi_thinserver {

	typeset server=$1
	typeset cosi=$2
	typeset thinservers=""
	typeset rc=0

        if [[ -z $cosi || -z $server ]]; then
		rc=1
	else
		thinservers=`get_cosi_client $server $cosi`
		thinservers=`echo $thinservers | /usr/bin/xargs lsnim \
			| /usr/bin/awk '{if(match($3, "diskless") || match($3, "dataless")) print $1}'`
		rc=$?
	fi

        echo $thinservers
	return $rc
}

# --------------------------------------------------------------------------- #
# NAME:         get_cosi_client
#
# FUNCTION:     Get NIM client using the common image.
#
# PARAMETERS:   None.
#
# RETURNS:      0 - get NIM client using the common image.
#               1 - command failure
# --------------------------------------------------------------------------- #
function get_cosi_client {

        typeset server=$1
        typeset cosi=$2
        typeset clients=""
        typeset rc=0

        if [[ -z $cosi || -z $server ]]; then
                rc=1
        else
                if [[ $server = "master" ]]; then
                        command="/usr/sbin/lsnim"
                elif [[ $server = "client" ]]; then
                        command="/usr/sbin/nimclient"
                fi

                clients=`$command -a spot | /usr/bin/awk -v cosi=$cosi '{if(match($3, cosi )) print prev; prev=$0}' \
                        | /usr/bin/cut -d: -f1`
                rc=$?
        fi

        echo $clients
        return $rc
}

# --------------------------------------------------------------------------- #
# NAME:         obj_exist
#
# FUNCTION:     Determine if object exist.
#
# PARAMETERS:   None.
#
# RETURNS:      0 - object exist
#               1 - object does not exist
# --------------------------------------------------------------------------- #
function obj_exist {

	typeset server=$1
        typeset object=$2
	typeset command=""

        if [[ -z $object ]] || [[ $object = " " ]] || [[ -z $server ]] || [[ $server = " " ]]; then
                return 1
        else
		if [[ $server = "master" ]]; then
			command="/usr/sbin/lsnim"
		elif [[ $server = "client" ]]; then
			command="/usr/sbin/nimclient -l"
		else
			return 1
		fi

                $command $object >/dev/null 2>&1
                if [[ $? -ne 0 ]]; then
                        return 1
                fi
        fi

        return 0
}

# --------------------------------------------------------------------------- #
# NAME:         get_attr_value
#
# FUNCTION:     Get attribute value
#
# PARAMETERS:   None.
#
# RETURNS:      0 - Obtain attribute value successfully
#               1 - unable to obtain attribute value
# --------------------------------------------------------------------------- #
function get_attr_value {

	typeset server=$1
        typeset object=$2
        typeset attribute=$3
        typeset value=""

        if [[ -z $object || -z $attribute || -z $server ]]; then
                return 1
        else
		if [[ $server = "master" ]]; then
			command="/usr/sbin/lsnim -l"
		elif [[ $server = "client" ]]; then
			command="/usr/sbin/nimclient -l -l"
		else
			/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_MISSING_ARGUMENT} \
				'%1$s: Missing argument.\n' "${PROGNAME}"
			return 1
		fi
	
                value=`$command $object | /usr/bin/grep -v "$object:" | \
			/usr/bin/awk -v attr=$attribute \
				'BEGIN { FS="= "} { \
					 count=length(attr); \
					 if(match(substr($0, 4, count), attr)) \
						print $2;\
				 }'`
        fi

        echo $value
        return 0
}

# --------------------------------------------------------------------------- #
# NAME:         ck_nim_server
#
# FUNCTION:     Get the object name given a hostname.
#
# PARAMETERS:   None.
#
# RETURNS:      0 - valid 
#               1 - invalid 
# --------------------------------------------------------------------------- #
function ck_nim_server {

	typeset server=$1
	typeset nim_name=""
	typeset attribute=""
	
	if [[ -z $server ]]; then
		return 1
	else
		nim_name=`/usr/sbin/lsnim -c machines | /usr/bin/grep -v ^master | /usr/bin/awk '{print $1}' \
			| /usr/bin/xargs /usr/sbin/lsnim -a if1 | /usr/bin/grep -v ":" \
			| /usr/bin/awk -v server=$server '{if(match($4, server )) print prev; prev=$0}' \
			| /usr/bin/cut -d: -f1`
		if [[ $? -ne 0 ]]; then
			return 1
		fi
	fi

	echo $nim_name
	return 0
}

# --------------------------------------------------------------------------- #
# NAME:         get_devicetype
#
# FUNCTION:     Get the device type of the source.
#
# PARAMETERS:   None.
#
# RETURNS:      devicetype - return the device type of the value being passed in
# --------------------------------------------------------------------------- #
function get_devicetype {

        typeset value=$1
        typeset devicetype=""
        typeset rc=0

        if [[ -z $value ]]; then
                rc=1
        else
                if [[ "${value}" = cd[0-9]* || "${value}" = /dev/cd[0-9]* ]] || [[ "${value}" = usbms[0-9]* || "${value}" = /dev/usbms[0-9]* ]]; then
                        devicetype="cdrom"
                elif [[ "${value}" = ?*:?* ]]; then
                        devicetype="remote"
                elif [[ -d "${value}" ]]; then
                        if [[ `/usr/bin/df | /usr/bin/awk '{print $7}' | \
                                /usr/bin/grep $( /usr/bin/dirname $source ) > /dev/null 2>&1; \
                                echo $?` -eq 0 ]]; then
                                devicetype="mnt"
                        else
                                devicetype="dir"
                        fi
                elif [[ $(obj_exist "master" $source; echo $?) -eq 0 ]]; then
                        devicetype="nim"
		else
	                /usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_INVALID_SOURCE} \
        	                '%1$s: Invalid source.\n' "${PROGNAME}"
                	return 1
                fi
        fi

        echo $devicetype
        return $rc
}

# --------------------------------------------------------------------------- #
# NAME:         ck_source
#
# FUNCTION:     Checks for valid device and resources, mount the device if
#               necessary.  Also, set SOURCE variables for define functions.
#
# PARAMETERS:   None.
#
# RETURNS:      source - return the value of source
#               0 - valid device with valid resources
#               1 - invalid device or invalid resources
# --------------------------------------------------------------------------- #
function ck_source {

        typeset value=$1
        typeset source=$value
       
	if [[ -z $value  ]]; then
		return 1
	else	
	        if [[ "${value}" = cd[0-9]* || "${value}" = /dev/cd[0-9]* ]] || [[ "${value}" = usbms[0-9]* || "${value}" = /dev/usbms[0-9]* ]]; then
        	        [[ "${value}" = /dev/cd[0-9]* ]] && value=`/usr/bin/basename "${value}"`
        	        [[ "${value}" = /dev/usbms[0-9]* ]] && value=`/usr/bin/basename "${value}"`
                	if [[ -n $( /usr/sbin/lsdev -Cl ${value} 2> /dev/null ) ]]; then
	                        source="/dev/${value}"
                	else
				/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_NOT_VALID_DEVICE} \
					'%1$s: %2$s is not a valid device.\n' "${PROGNAME}" "${value}"
	                        return 1
        	        fi
	        fi
	fi

        echo $source
        return 0
}

# --------------------------------------------------------------------------- #
# NAME:         ck_cron
#
# FUNCTION:     Check and start the cron daemon
#
# PARAMETERS:   None.
#
# RETURNS:      0 - successful
#               1 - unsuccessful
# --------------------------------------------------------------------------- #
function ck_cron {

	typeset cron_up=""

	# lets verify that the cron daemon is running
	# - start it
	cron_up=`/usr/bin/ps -u root | /usr/bin/grep -q cron; echo $?`
	if [[ -z "$cron_up" ]] || [[ "$cron_up" != "0" ]]; then
	        /usr/sbin/cron >/dev/null 2>&1 
		if [[ $? -ne 0 ]]; then
			return 1
		fi
	fi

	return 0
}

# --------------------------------------------------------------------------- #
# NAME:         ck_user
#
# FUNCTION:     Check if user is root.
#
# PARAMETERS:   None.
#
# RETURNS:      0 - successful
#               1 - unsuccessful
# --------------------------------------------------------------------------- #
function ck_user {
	
	if [[ `/usr/bin/whoami` != root ]]; then
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_MUST_BE_ROOT} \
			'%1$s: You must be a root user to run this command.\n' "${PROGNAME}"
		return 1
	fi

	return 0
}

# --------------------------------------------------------------------------- #
# NAME:         ck_speed
#
# FUNCTION:     Checks for valid speed value or set default
#
# PARAMETERS:   None.
#
# RETURNS:      0 - correct speed
#               1 - incorrect speed
# --------------------------------------------------------------------------- #
function ck_speed {

        typeset speed=$1

        if [[ $speed != "10" ]] &&
           [[ $speed != "100" ]] &&
           [[ $speed != "1000" ]] &&
           [[ $speed != "auto" ]]; then
                /usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_WRONG_SPEED} \
                        '%1$s: Wrong speed setting.\n' "${PROGNAME}"
                return 1
        fi

        return 0
}

# --------------------------------------------------------------------------- #
# NAME:         ck_duplex
#
# FUNCTION:     Checks for valid duplex value or set default
#
# PARAMETERS:   None.
#
# RETURNS:      0 - correct duplex value
#               1 - incorrect duplex value
# --------------------------------------------------------------------------- #
function ck_duplex {

        typeset duplex=$1

        if [[ "${duplex}" != "half" ]] &&
           [[ "${duplex}" != "full" ]] &&
           [[ "${duplex}" != "auto" ]]; then
                /usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_WRONG_DUPLEX} \
                        '%1$s: Wrong duplex setting.\n' "${PROGNAME}"
                return 1
        fi

        return 0
}

# --------------------------------------------------------------------------- #
# NAME:         ck_ipaddr
#
# FUNCTION:     Checks for valid attributes
#
# PARAMETERS:   name    - type of ip address
#               ip_addr - ip address
#
# RETURNS:      0 - correct ip value
#               1 - incorrect ip value
# --------------------------------------------------------------------------- #
function ck_ipaddr {

        typeset name=$1
        typeset ip_addr=$2

        # Convert hostname to ip address
        if [[ "${name}" = "client" && "${ip_addr}" != *.*.*.* ]]; then
		client=`/usr/bin/host ${ip_addr} | /usr/bin/cut -f3 -d' ' | /usr/bin/sed 's/,//g'`

                if [[ $? -ne 0 ]]; then
			/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_CANT_RESOLVE_IP} \
				'%1$s: Unable to resolve ip address.\n' "${PROGNAME}"
                        return 1
                fi
                ip_addr=$client
        fi

        # determine the format
        if [[ "${ip_addr}" != *.*.*.* ]]; then
		/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_WRONG_IP_FORMAT} \
			'%1$s: %2$s is in the wrong format: %3$s\n' "${PROGNAME}" "${name}" "${ip_addr}"
                return 1
        fi

        # Check for valid IP address
        for i in `/usr/bin/echo ${ip_addr} | /usr/bin/sed 's/\./ /g'`
        do
                if [[ $i -gt 255 ]] || [[ $i -lt 0 ]]; then
			/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_WRONG_IP_FORMAT} \
				'%1$s: %2$s is in the wrong format: %3$s\n' "${PROGNAME}" "${name}" "${ip_addr}"
                        return 1
                fi
        done

        # Resolve the ip address to a hostname
        if [[ "${name}" = "client" ]] || [[ "${name}" = "gateway" ]]; then

                if [[ "${name}" = "client" ]]; then
                        /usr/bin/host ${ip_addr} > /dev/null 2>&1
                elif [[ "${name}" = "gateway" ]]; then
                        /usr/sbin/ping -c 1 ${ip_addr} >/dev/null 2>&1
                fi

                if [[ $? -eq 1 ]]; then
			/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_CANT_RESOLVE_IP} \
				'%1$s: Unable to resolve ip address.\n' "${PROGNAME}"
                        return 1
                fi
        fi

        return 0
}

# --------------------------------------------------------------------------- #
# NAME:         define_res
#
# FUNCTION:     Define resource.
#
# PARAMETERS:   None.
#
# RETURNS:      0 - resource define successfully
#               1 - unable to define resource
# --------------------------------------------------------------------------- #
function define_res {

        typeset type=$1
        typeset name=$2
        typeset server=$3
        typeset location=$4
        typeset source=$5
        typeset all_packages=$6

        /usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DEFINING_OBJECT} 'Defining %1$s object...' "${name}" 
        /usr/sbin/nim -o define -t $type -a server=$server -a location=$location ${all_packages:+-a packages=all} ${source:+-a source=$source} $name

        if [[ $(obj_exist "master" $name; echo $?) -ne 0 ]]; then
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_FAILED} 'failed\n'
                return 1
        fi

        /usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DONE} 'done\n' 

        return 0
}

# --------------------------------------------------------------------------- #
# NAME:         uninitialize_ts
#
# FUNCTION:     Uninitialize the thinserver
#
# PARAMETERS:   None.
#
# RETURNS:      0 - successful
#               1 - unsuccessful
# --------------------------------------------------------------------------- #
function uninitialize_ts {

        typeset ts=$1

	if [[ -z $ts ]] || [[ $ts = " " ]]; then
		return 1
	else
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_UNINITIALIZING} \
			'Uninitializing %1$s.\n' "${ts}"
		/usr/sbin/nim -Fo reset $ts
		/usr/sbin/nim -Fo deallocate -a subclass=all $ts
	fi

        return 0
}

# --------------------------------------------------------------------------- #
# NAME:         get_apar_fileset
#
# FUNCTION:     Given an apar, get the fileset associated with the apar.
#
# PARAMETERS:   1 = apar
#
# RETURNS:      fileset(s) - return fileset associated with the apar
#               0 - successful
#               1 - unsuccessful
# --------------------------------------------------------------------------- #
function get_apar_fileset {

        typeset apar=$1

        if [[ -z $apar ]]; then
                return 1
        fi

        fileset=`/usr/sbin/instfix -c -i -k $apar -v | /usr/bin/grep -v "^#" | /usr/bin/cut -d: -f2`

        echo  $fileset
        return 0
}

# --------------------------------------------------------------------------- #
# NAME:         get_fixbundle_fileset
#
# FUNCTION:     Given a bundle, get the fileset associated with each apar in
#               the bundle.
#
# PARAMETERS:   1 = fix_bundle object
#
# RETURNS:      fileset(s) - return fileset associated with the apar in the bundle
#               0 - successful
#               1 - unsuccessful
# --------------------------------------------------------------------------- #
function get_fixbundle_fileset {

        typeset bundle=$1

        if [[ -z $bundle ]]; then
                return 1
        fi

        for fix in `/usr/sbin/nim -o showres $bundle`
        do
                list=`get_apar_fileset $fix`
                fileset="$fileset $list"
        done

	fileset=`echo $fileset | /usr/bin/tr ' ' '\012' | /usr/bin/sort | /usr/bin/uniq | /usr/bin/tr '\012' ' '`
        echo $fileset
        return 0
}

# --------------------------------------------------------------------------- #
# NAME:         backup_client
#
# FUNCTION:     Backup the client information.
#
# PARAMETERS:   client - Client to be backup.
#               res1   - resource to be backup (home, root, or tmp)
#               res2   - resource to be backup (home, root, or tmp)
#               res3   - resource to be backup (home, root, or tmp)
#
# RETURNS:      0 - successful
#               1 - unsuccessful
#               r_file - name of the backup file
# --------------------------------------------------------------------------- #
function backup_client {

        typeset client=$1
        typeset res1=$2
        typeset res2=$3
        typeset res3=$4
        typeset rc=0


        typeset backup=""
        typeset backup_exclude=/tmp/swts.$$.exclude
        typeset backup_list="/tmp/swts.$$.backup"

        if [[ -s /var/adm/ras/eznim.cfg ]]; then
                . /var/adm/ras/eznim.cfg
                backup=${EZNIM_FS}/backup.$client
        else
		if [[ ! -d /export/nim ]]; then
			mkpath /export/nim || return 1
		fi
                backup=/export/nim/backup.$client
        fi

        ROOT_INCLUDE="etc/csh.cshrc etc/csh.login etc/group etc/hosts etc/motd etc/passwd etc/profile \
                      etc/resolv.conf etc/security/group etc/security/passwd etc/security/user etc/tftpaccess.ctl"
        TMP_EXCLUDE="dpi_socket .nfs* .strload.mutex rc.net.serial.out rc.net.out"
        cd /
        for resource in $res1 $res2 $res3; do
                location=`get_attr_value "master" $resource "location"`
                type=`get_attr_value "master" $resource "type"`
                if [[ $type = "root" ]]; then
                        for entry in ${ROOT_INCLUDE}; do
                                echo ".${location}/${client}/${entry}" >> $backup_list 2>/dev/null
                        done
                elif [[ $type = "tmp" ]]; then
                        for entry in ${TMP_EXCLUDE}; do
                                echo ".${location}/${client}/${entry}" >> $backup_exclude
                        done
                        find .$location/$client -print | grep -v -f $backup_exclude >> $backup_list 2>/dev/null
			rm_file $backup_exclude
                else
                        find .$location/$client -print >> $backup_list 2>/dev/null
                fi
        done
        tar -cvf $backup -L $backup_list 2>/dev/null
        rm_file $backup_list
        r_file=$backup

        return $rc
}

# --------------------------------------------------------------------------- #
# NAME:         restore_client
#
# FUNCTION:     restore the client information.
#
# PARAMETERS:   client - Client to be restore.
#               file   - File to be restore.
#
# RETURNS:      0 - successful
#               1 - unsuccessful
# --------------------------------------------------------------------------- #
function restore_client {

        typeset file=$1
        typeset rc=0

	if [[ -z $file ]]; then
		rc=1
	fi

        cd /
        tar -xvf $file

        if [[ $? -ne 0 ]]; then
                rc=1
        fi

        return $rc
}
