#!/bin/sh

#ident "$installgetras: installgetras Build 5.91.0.6 07/03/30 16:00:00 $"
#
#/*----------------------------------------------------------------------------*/
#/* PRODUCT :  HDLM Driver 0591                                                */
#/* NAME :     Installgetras                                                   */
#/* FUNCTION : Installer error information collection Utility                  */
#/* INSTANCE:                                                                  */
#/* ATTR:                                                                      */
#/* MEMO :     Installgetras Execution Shell Script                            */
#/*                                                                            */
#/* DATE :     2007.03.02                                                      */
#/* HISTORY :                                                                  */
#/* AUTHOR :   Umesh Dawra                                                     */
#/*                                                                            */
#/* All Rights Reserved. Copyright (C) 2007, Hitachi, Ltd.               */
#/*----------------------------------------------------------------------------*/


CMD_AWK="/bin/gawk"
CMD_BASENAME="/bin/basename"
CMD_CD="cd"
CMD_CHMOD="/bin/chmod"
CMD_CHOWN="/bin/chown"
CMD_CP="/bin/cp"
CMD_DATE="/bin/date"
CMD_ECHO="/bin/echo"
CMD_GREP="/bin/grep"
CMD_GZIP="/bin/gzip"
CMD_KILL="/bin/kill"
CMD_LS="/bin/ls"
CMD_MKDIR="/bin/mkdir"
CMD_PS="/bin/ps"
CMD_RM="/bin/rm"
CMD_TAR="/bin/tar"
CMD_WHOAMI="/usr/bin/whoami"

GETRAS_TMPDIR=""
HDLMINSTALL_LOGDIR="/var/tmp/hdlminstlog"
RAS_TMP_DIR="/Installgetras_tmpdir.$$"
TARFILE="installgetras.tar"
VAR_LOGDIR="/var/log"

STATUS=1

print_messages()
{
	case $1 in
	'KAPL13401')
	$CMD_ECHO "KAPL13401-E No parameter has been specified."
	;;

	'KAPL13402')
	$CMD_ECHO "KAPL13402-E The first parameter has not been set to a directory. Value = $2"
	;;

	'KAPL13403')
	$CMD_ECHO "KAPL13403-E You lack write permission for the specified directory. Value = $2"
	;;

	'KAPL13404')
	$CMD_ECHO -n "KAPL13404-W The specified directory already exists. Do you want to overwrite it? [y/n]: "
	;;
	
	'KAPL13405')
	$CMD_ECHO "KAPL13405-E The root directory has been specified in the first parameter."
	;;
	
	'KAPL13406')
	$CMD_ECHO "KAPL13406-E You lack privileges for executing the utility for collecting HDLM install error information."
	;;

	'KAPL13407')
	PRESENT_TIME=`$CMD_DATE '+%Y/%m/%d  %H:%M:%S'`"(GMT:"`$CMD_DATE -u '+%Y/%m/%d  %H:%M:%S'`")"
	$CMD_ECHO "KAPL13407-I The file has been obtained successfully. File = $2, Collection time = $PRESENT_TIME" >> $GETRAS_LOGFILE
	;;
	
	'KAPL13408')
	$CMD_ECHO "KAPL13408-E Processing terminated before completion because a signal was received."
	;;
	
	'KAPL13409')
	$CMD_ECHO "KAPL13409-I The utility for collecting HDLM install error information completed normally."
	;;

	'KAPL13410')
	$CMD_ECHO "KAPL13410-I A user terminated the utility for collecting HDLM install error information."
	;;

	'KAPL13411')
	$CMD_ECHO -n "KAPL13411-W The entered value is invalid. Continue operation? [y/n]:"
	;;

	'KAPL13412')
	$CMD_ECHO "KAPL13412-E The entered value is invalid. The utility for collecting HDLM install error information stops."
	;;
	
	'KAPL13413')
	$CMD_ECHO "KAPL13413-W The file does not exist. Filename = $2" >> $GETRAS_LOGFILE
	;;
	
	'KAPL13414')
	$CMD_ECHO "KAPL13414-E The file could not be copied. Filename = $2, Details = $3"
	;;
	
	'KAPL13415')
	$CMD_ECHO "KAPL13415-E An attempt to archive the install error information failed. Details = $2"
	;;

	'KAPL13416')
	$CMD_ECHO "KAPL13416-E An attempt to compress the install error information failed. Details = $2"
	;;
	
	'KAPL13417')
	$CMD_ECHO "KAPL13417-E The install error information does not exist."
	;;
	
	'KAPL13418')
	$CMD_ECHO "KAPL13418-E Too many parameters have been specified."
	;;

	*)
	exit
	;;
	esac
}

######################################################################
# parameter check
# Information output Directory check
dir_check()
{
	CNT=1

	while [ $CNT -le 3 ]; do
		if [ "$CNT" -eq 1 ];then			
			print_messages "KAPL13404"
			read ans
		fi
		case $ans in
		 y|Y)
		 if [ ! -w "$1" ];then
			print_messages "KAPL13403" $1
			exit
		 fi
		return
		;;
		n|N)
		print_messages "KAPL13410"
		exit
		;;
		*)
		if [ "$CNT" -lt 3 ];
		then
			print_messages "KAPL13411"
			read ans
			CNT=`/usr/bin/expr $CNT + 1`
			continue
		fi
		;;
		esac
		
		print_messages "KAPL13412"
		exit
	done
}

# input parameter check
param_check()
{
	if [ $# -eq 0 ]; then
		# parameter error
		print_messages "KAPL13401"
		exit
	fi

	if [ $# -gt 1 ];then
		print_messages "KAPL13418"
		exit
	fi

	OPTCHK=`$CMD_ECHO $1 | $CMD_GREP '^-'`
	if [ ! -z "$OPTCHK" ];then
		print_messages "KAPL13402" $1
		exit
	fi
	
	if [ -e "$1" -a ! -d "$1" ]; then
		print_messages "KAPL13402" $1
		exit
	fi

	# existence directory
	if [ -d "$1" ];	then		
		if [ "$1" == "/" ];then
			print_messages "KAPL13405"
			exit
		fi
		dir_check $1
	fi
}


#Creating directories
make_dir()
{
	$CMD_MKDIR -p $GETRAS_OUTDIR$HDLMINSTALL_LOGDIR
	$CMD_MKDIR -p $GETRAS_OUTDIR$VAR_LOGDIR
}


#File collection function
collect_files()
{
	for GET_FILE in $1
	do
		    if [ ! -f "$GET_FILE" ];
   		    then
     		    	 print_messages "KAPL13413" "$GET_FILE"
     			 continue
   		    fi

  		    ERR_MSG=`$CMD_CP -p $GET_FILE $GETRAS_OUTDIR$GET_FILE 2>&1`
		    RET=$?
   		    if [ $RET -eq 0 ];
     		    then
       		   	 print_messages "KAPL13407" "$GET_FILE"
     		    else
      		         print_messages "KAPL13414" "$GET_FILE" "$ERR_MSG"
			 exit
   		    fi
	done
}

#Exit handler
fn_exit( )
{
	trap "" 1 2 3 9 15
	if [ $STATUS -eq 0 ]; then
		ERR_MSG=`$CMD_CP -p $TARFILE.gz ../ 2>&1`
		RET=$?
		if [ $RET -ne 0 ]; then
			print_messages "KAPL13414" "$TARFILE.gz" "$ERR_MSG"
			STATUS=1			
		fi
		$CMD_CD $WORKING_DIR
		$CMD_RM -rf $GETRAS_TMPDIR
		if [ $STATUS -eq 0 ]; then
			print_messages "KAPL13409"
			exit 0
		else
			exit 1
		fi
	else
		$CMD_CD /
		if [ -d $GETRAS_TMPDIR ]; then
			$CMD_RM -rf $GETRAS_TMPDIR
		fi
		exit 1
	fi
}

#Trap handler
trap_handler( )
{
	trap "" 1 2 3 9 15
	# killing child process
	for PID in `$CMD_PS -f | $CMD_AWK -v PPID=$$ '{if ( "$3" == "PPID" ) print $2}'` ; do
		if [ "$PID" != "PID" -a "$PID" -ne "$$" ] ; then
			$CMD_KILL $PID 2>/dev/null
		fi
	done

	if [ $STATUS -eq 0 ]; then
		ERR_MSG=`$CMD_CP -p $TARFILE.gz ../ 2>&1`
		RET=$?
		if [ $RET -ne 0 ]; then
			print_messages "KAPL13414" "$TARFILE.gz" "$ERR_MSG"
			STATUS=1
		fi
		$CMD_CD $WORKING_DIR
		$CMD_RM -rf $GETRAS_TMPDIR
		if [ $STATUS -eq 0 ]; then
			print_messages "KAPL13409"
			exit 0
		else
			exit 1
		fi
	else
		$CMD_CD /
		if [ -d $GETRAS_TMPDIR ]; then
			$CMD_RM -rf $GETRAS_TMPDIR
		fi
		print_messages "KAPL13408"
		exit 1
	fi
}

#Directory collection function

col_dir()
{
  for GET_DIR in $1
  do
    if [ ! -d "$GET_DIR"  -a ! -f "$GET_DIR" ];
    then
      print_messages "KAPL13413" $GET_DIR
      continue
    fi

    ERR_MSG=`$CMD_CP -R $GET_DIR $GETRAS_OUTDIR$GET_DIR 2>&1`
    if [ $? -eq 0 ];
      then
        print_messages "KAPL13407" $GET_DIR
      else
        print_messages "KAPL13414" $GET_DIR  "$ERR_MSG"
	exit
    fi
  done
}

#Create installgetras.tar.gz
create_tar()
{
	$CMD_CD ../
	ERR_MSG=`$CMD_TAR -cf $TARFILE $OUTDIR 2>&1`
	RET=$?
	if [ $RET -ne 0 ]; then
		print_messages "KAPL13415" "$ERR_MSG"
		exit
	fi
	ERR_MSG=`$CMD_GZIP $TARFILE 2>&1`
	RET=$?
	if [ $RET -ne 0 ]; then
		print_messages "KAPL13416" "$ERR_MSG"
		exit
	fi
	$CMD_CHMOD 400 $TARFILE.gz
	$CMD_CHOWN root:root $TARFILE.gz
	#print_message "KAPL13409"
	STATUS=0
	exit
}



######################################################################
# installgetras main

trap "fn_exit" EXIT
trap "trap_handler" 1 2 3 9 15

CHK_USER=`$CMD_WHOAMI | $CMD_GREP root`

if [ -z "$CHK_USER" ]; then
	print_messages "KAPL13406"
	exit
fi

param_check $*


if [ ! -d "$HDLMINSTALL_LOGDIR" ]; then
	print_messages "KAPL13417"
	exit
else
	TEST=`$CMD_LS "$HDLMINSTALL_LOGDIR"`
	if [ -z "$TEST" ]; then
		print_messages "KAPL13417"
		exit
	fi
fi

MASK=`umask`
umask 077

if [ ! -d "$1" ]; then
	#Display error message if mkdir fails for Linux
	$CMD_MKDIR -p "$1" > /dev/null 2>&1
	RET=$?
	if [ $RET -ne 0 ];then
		print_messages "KAPL13403" $1
		exit
	fi
fi

PRESENT_DIR=`/bin/pwd`
OUTDIR="$1"
$CMD_CD $OUTDIR
WORKING_DIR=`/bin/pwd`

OUTDIR=`$CMD_BASENAME $WORKING_DIR`
GETRAS_TMPDIR="$WORKING_DIR$RAS_TMP_DIR"
GETRAS_OUTDIR="$GETRAS_TMPDIR/$OUTDIR"
GETRAS_LOGFILE="$GETRAS_OUTDIR/installgetras.log"

#Display error message if mkdir fails for Linux
$CMD_MKDIR -p $GETRAS_OUTDIR > /dev/null 2>&1
RET=$?
if [ $RET -ne 0 ];then
	print_messages "KAPL13403" $1
	exit
fi

$CMD_CD $GETRAS_OUTDIR
#Create Directories
make_dir

#Copying the files in the respective directories
col_dir "$HDLMINSTALL_LOGDIR/*"

COL_FILES="$VAR_LOGDIR/messages"

collect_files "$COL_FILES"

$CMD_CHMOD 0400 $GETRAS_LOGFILE
$CMD_CHOWN root:root $GETRAS_LOGFILE

#Create Getras tar
create_tar

umask=$MASK