#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos72F src/bos/usr/lib/nim/methods/c_file_transfer.sh 1.1.1.4 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2008,2016 
# 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 
# @(#)65        1.1.1.4  src/bos/usr/lib/nim/methods/c_file_transfer.sh, cmdnim, bos72F, f2016_27A9 6/24/16 09:38:43

#   COMPONENT_NAME: CMDNIM
#
#   FUNCTIONS: ./usr/lib/nim/methods/c_file_transfer.sh
#
#
#   ORIGINS: 27
#
#
#   (C) COPYRIGHT International Business Machines Corp. 2008
#   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.

#
NIMPATH=${0%/*}
NIMPATH=${NIMPATH%/*}

[[ ${NIMPATH} = ${0} ]] && NIMPATH=/usr/lpp/bos.sysmgt/nim
NIM_METHODS="${NIMPATH}/methods"

export NIMPATH NIM_METHODS

#
# "dot" include (source) the shell lib functions
#
. ${NIM_METHODS}/c_sh_lib

# --------------------------- local defines

# --------------------------- module globals
REQUIRED_ATTRS="location dest_dir"
OPTIONAL_ATTRS=""
location=""
dest_dir=""

# --------------------------- c_file_transfer
#
# NAME: c_file_transfer
#
# FUNCTION:
#		Specifies a directory of file(s) for transfer to a NIM client.
#
# EXECUTION ENVIRONMENT:
#
# NOTES:
#
# RECOVERY OPERATION:
#
# DATA STRUCTURES:
#		parameters:
#		global:
#
# RETURNS: (int)
#		0	= no errors
#		>0	= failure
#
# OUTPUT:
# ----------------------------------------------------------------------
#

# signal processing
trap cleanup 0
trap err_signal 1 2 11 15

# NIM initialization
nim_init

# initialize local variables
rc=1

# set parameters from command line
while getopts :a:qv c
do
	case ${c} in

		a)		# validate the attr ass
				parse_attr_ass "${OPTARG}"

				# include the assignment for use in this environment
				eval ${variable}=\"${value}\"
		;;

		q)		# show attr info
				cmd_what
				exit 0
		;;

		v)		# verbose mode (for debugging)
				set -x
				for i in $(typeset +f)
				do
					typeset -ft $i
				done
		;;

		\?)	# unknown option
				error ${ERR_BAD_OPT} ${OPTARG}
		;;
	esac
done

#
# check for missing attrs
#
ck_attrs

# NOTE: As part of the http availability feature -
#       the initial call will include a check for
#       web download service.
#
if [[ ${location} = ?*:/?* ]]
then
	# c_file_transfer should attempt to locate the file
	# using the nimtthp servicing option
	# Preference for ssl ..

	tmp_dest_dir="${dest_dir%/}/_nim_dir_$$"
	download_srvr=`echo ${location} | ${SED} 's/\:.*$//'`
	download_loc=`echo ${location} | ${SED} 's/.*://'`
	${NIMHTTP} -i $download_srvr -f $download_loc -o dest=${tmp_dest_dir} -s >/dev/null 2>&1 ||
	${NIMHTTP} -i $download_srvr -f $download_loc -o dest=${tmp_dest_dir} >/dev/null 2>&1
	rc=$?

	if [[ $rc -eq 0 ]]; then
		mget_http_content $location ${tmp_dest_dir} >/dev/null 2>&1
		rc=$?
	fi

	if [[ $rc -eq 0 ]]; then

		# move any hidden files
		${LS} ${tmp_dest_dir}${download_loc%/}/.[!.]* >/dev/null 2>&1
		if [[ $? -eq 0 ]]; then
			${MV} ${tmp_dest_dir}${download_loc%/}/.[!.]* ${dest_dir%/}/ >/dev/null 2>&1
			rc=$?
		fi

		# move any remaining content
		${LS} ${tmp_dest_dir}${download_loc%/}/?* >/dev/null 2>&1
		if [[ $? -eq 0 ]]; then 
			${MV} ${tmp_dest_dir}${download_loc%/}/?* ${dest_dir%/}/ >/dev/null 2>&1
			rc=$?
		fi

		${RM} -r ${tmp_dest_dir} >/dev/null 2>&1
	fi
	# end of HTTP
fi

if [[ $rc -ne 0 ]]
then
	# NFS Method
	#
	# ensure local access to the file (local path returned via access_pnt)
	#
	nim_mount ${location}

	#
	# Check if destination directory exists yet, if not make it
	# and use same permissions as target directory during the copy
	#
	# Else, copy the file(s) from the NFS server and preserve
	# the permission of the destination directory.
	#
	if [[ ! -d ${dest_dir} ]]
	then
		${MKDIR} -p ${dest_dir} 2>$ERR || \
			err_from_cmd "${MKDIR} ${dest_dir}"

		rc=`${CP} -pr "${access_pnt}/." ${dest_dir} 2>$ERR`
	else
		rc=`${CP} -pr ${access_pnt}/?* ${dest_dir} 2>$ERR`
	fi
	# end of NFS
fi

[[ ${rc} -ne 0 ]] && err_from_cmd "${CP} ${access_pnt} ${dest_dir}"

# complete
exit 0
