#!/bin/ksh
#
# @(#)es-mcp-get	1.14 02/12/18
# Copyright (c) 12/18/02 Sun Microsystems, Inc. All Rights Reserved.
#

#############################################################
# Function: setup_safe_tmp_dir
# Purpose:  Set up a safe temporary directory
# Input:    None
# Return:   exit
#
setup_safe_tmp_dir() {

	# determine architecture

	MACH=`uname -m`
	SYS=`uname -s`
	REL=`uname -r`
	VER=`uname -v`
	case "$MACH:$SYS:$REL:$VER" in
    	i86pc*:SunOS:5.*:*)
        	REL="`echo ${REL} | sed 's/^5/2/' | cut -d\. -f1,2`"
			ARCH="i386-sun-solaris${REL}" ;;
		sun4*:SunOS:5.*:*)
			REL="`echo ${REL} | sed 's/^5/2/' | cut -d\. -f1,2`"
			ARCH="sparc-sun-solaris${REL}" ;;
		sun4*:SunOS:*:*)
			REL="`echo ${REL} | cut -d\. -f1,2`"
			ARCH="sparc-sun-sunos${REL}" ;;
	esac

	if [ ! -x  $ESROOT/util/bin/$ARCH/uclock ]
	then
		/usr/bin/gettext "ERROR: Could not find uclock in $ESROOT/util/bin/$ARCH"
		exit 1
	fi

    timestamp=`$ESROOT/util/bin/$ARCH/uclock`
    safedir="${TMPDIR:-/tmp}/sunmclog-${timestamp}-$$"

    if [ ! -d "$safedir" ]; then
	/usr/bin/mkdir -m 700 $safedir
	if [ $? != 0 ]; then
	    /usr/bin/gettext "ERROR: Unable to make temporary directory $safedir\n"
	    exit 1
	fi
	TMP_LOG_DIR="$safedir"
    else
	/usr/bin/gettext "ERROR: Unable to use $safedir due to possible security issues.\n"
	exit 2
    fi
}

LOCAL_BASE=`expr "$INTERFACE_PATH" : '\([^:]*\)'`
LOCAL_BASE=`dirname $LOCAL_BASE`

setup_safe_tmp_dir
mkdir -m 700 -p $TMP_LOG_DIR"/cfg"

INDEX_FILE="${TMP_LOG_DIR}/index$$.xml"
REQUIRED_URLS="${TMP_LOG_DIR}/requiredUrls$$"
AWK_CMD=nawk
MCP_GET_URL=mcp_http_op
MCP_CKSUM=mcp_cksum
RETCODE=255
BASE_MODULES=base-modules-d.dat

#############################################################
# Function: cleanExit
# Purpose:  clean up and exit
# Input:    None
# Return:   exit
#
cleanExit() {
    cd /tmp; rm -rf $TMP_LOG_DIR
    exit $1
}



#############################################################
# Main entry point
# Purpose:  MCP client script to get module config file for a parcel
# Input:    $1, id
#           $2, URL of index file
#           $3, local base directory
# Return:   0, success
#           others, failure
# Note:     local directory should exists
#

#
# check input parameters
#
if [ $# -ne 5 ]
then
    echo "Usage: $0 id urlOfIndexFile localBaseDir parcel propFlag" >&2
    exit 1
fi

PARAM_PROPAGATE=$5  # 0 = always, 1 = if not loaded

#
# check whether localBaseDir exists
#
if [ ! -d $3 ]
then
    echo "Error, $3 is not a directory, exit." >&2
    exit 2
fi

# trap SIGHUP SIGINT SIGQUIT SIGTERM for clean up
trap cleanExit 1 2 3 15 
ID=$1
URL_INDEX_FILE=$2
LOCAL_BASE_DIR=$3
PARCEL=$4

#
# initialization
#
# 1. clear REQUIRED_URLS file
# 2. clear TMP_LOG_DIR
#
> $REQUIRED_URLS
if [ $? -ne 0 ]
then
    echo "Error, cannot create file for required URLs, exit." >&2
    cleanExit 3
fi

#
# get index file
#
$MCP_GET_URL $URL_INDEX_FILE $ID index.xml $PARCEL> $INDEX_FILE
if [ $? -ne 0 ]
then
    echo "Error, failed to get index file from server, exit." >&2
    cleanExit 5
fi
if [ ! -r $INDEX_FILE ]
then
    echo "$INDEX_FILE is not a readable file, exit." >&2
    cleanExit 6
fi


#
# process index file
#
mcp_process_index $INDEX_FILE $REQUIRED_URLS $LOCAL_BASE_DIR 
if [ $? -ne 0 ]
then
    echo "Error, failed to process index file, exit." >&2
    cleanExit 7
fi
rm -f "/tmp/mcp_process_index.log"

#
# Get module config files, if necessary
#
url_list=`cat $REQUIRED_URLS`
for line in $url_list
do
    RETCODE=0
    FILE=`echo $line | $AWK_CMD -F "," '{print $2}'`

    MODE=`echo $line | $AWK_CMD -F "," '{print $3}'`
    CHECKSUM=`echo $line | $AWK_CMD -F "," '{print $4}'`

    URL=$URL_INDEX_FILE"/"$FILE
    ABSOLUTE_FILE_NAME=$TMP_LOG_DIR"/"$FILE

    # create the directory first.
    DIR_NAME=`dirname $ABSOLUTE_FILE_NAME`
    mkdir -p $DIR_NAME

    NEWURL=`echo $URL | sed 's/+/%2B/g'`
    NEWFILE=`echo $FILE | sed 's/+/%2B/g'`
    $MCP_GET_URL $NEWURL $ID $NEWFILE $PARCEL > $ABSOLUTE_FILE_NAME

    if [ $? -ne 0 ]
    then
	echo "Error, failed to get file from server, exit." >&2
	cleanExit 8
    fi
    if [ ! -f $INDEX_FILE ]
    then
	echo "$ABSOLUTE_FILE_NAME is not created properly, exit." >&2
	cleanExit 9
    fi

    # compare the checksum, exit(-1) if failed.
    CHECKSUM_LOCAL=`mcp_cksum $ABSOLUTE_FILE_NAME`
    if [ $CHECKSUM_LOCAL -ne $CHECKSUM ]
    then
	echo "error for checksum of $ABSOLUTE_FILE_NAME, exit." >&2
	cleanExit 10
    fi
done

#
# If the propagate parameter is "If not loaded on target",
# remove all loaded modules from $MCP_BASE_MODULES
#
MCP_BASE_MODULES=$TMP_LOG_DIR"/cfg/"$BASE_MODULES".MCP"
TMP_BASE_MODULES=$MCP_BASE_MODULES".TMP"
> $TMP_BASE_MODULES # Clear the temp file for module parameters
url_list=`/usr/bin/awk '{ print $1 }' $MCP_BASE_MODULES`
for line in $url_list
do
    # Add \ in module name to +, if any
    MODULE=`echo $line | /usr/bin/sed 's/+/\\\+/'`

    module_info=`/usr/bin/grep "^$MODULE " $MCP_BASE_MODULES`
    if [ "$PARAM_PROPAGATE" = "1" ] ; then # Propagate if not loaded
	if [ "$MODULE" != "base-modules-d" ] ; then
	    #
	    # Do not propagate this module if it is already loaded,
	    # otherwise save the module parameters into a temp file.
	    #
	    module_exists=`/usr/bin/grep "^$MODULE " $LOCAL_BASE"/cfg/"$BASE_MODULES`
	    if [ "$module_exists" != "" ] ; then
		continue
	    else
		echo $module_info >> $TMP_BASE_MODULES
	    fi
	fi
    else
	echo $module_info >> $TMP_BASE_MODULES
    fi
done

#
# Make sure base-modules-d.dat.MCP has only relevant module information
#
mv $TMP_BASE_MODULES $MCP_BASE_MODULES

#
# copy file from tmp location to actual location
#
# if REQUIRED_URLS is not empty (i.e. do need to get some files from server)
# do the copy
#
if [ -s "$REQUIRED_URLS" ]
then
    mkdir -p $LOCAL_BASE_DIR
    cd $TMP_LOG_DIR; cp -r . $LOCAL_BASE_DIR

    if [ -d "$TMP_LOG_DIR/bin" ]; then
	for file in `ls $TMP_LOG_DIR/bin`
	do
	    /usr/bin/chmod a-w $LOCAL_BASE_DIR/bin/$file
	    /usr/bin/chmod a+rx $LOCAL_BASE_DIR/bin/$file
	done
    fi
   
    if [ $? -ne 0 ]
    then
	echo "Error, failed to copy files from temp location to actual location, exit." >&2
	cleanExit 11
    fi
fi

cleanExit $RETCODE
