#! /usr/bin/sh
#set environment language
export LANG=C
if [ "x${1:-}" != "xselfrun" ]; then
    /bin/sh $0 selfrun $*
    exit $?
fi
shift

####################
# global variable
####################
program=""
session_dir=""

# 0: success
# 1: Error
SUCCESS=0
ERROR=1

RET_VALUE=$ERROR

####################
# Main
####################
# parse the arguments
while [ $# -gt "0" ]; do
	case $1 in
		-program) program=$2; shift; shift; ;;
		-session_dir) session_dir=$2; shift; shift; ;;
		*) 
			echo "Cannot regonize the input parmeter $1"
			shift;
			;;
	esac
done

echo "argument program: $program"
echo "argument session_dir: $session_dir"

# shutdown HpsumServerhpux service
if [ -n "${program}" ]; then
	echo "shutdown ${program} service"
	env UNIX_STD=2003 ps -opid -C ${program} | awk '{if($1 != "PID") system("kill " $1)}'
	RC=$?
	if [ ${RC} -ne "0" ]; then
		echo "ERROR: Failed to shutdown the service ${program}"
		exit $RET_VALUE
	fi
fi

# remove files and directory
if [ -n "${session_dir}" ]; then
	echo "remove files and directory in ${session_dir}"
	cd ${session_dir}
	RC1=$?
	rm -rf *.so *tools.xml HpsumServer* hpsum_* pciinfo.* hpux_crossos.tar* *.sh
	RC2=$?
	RC=`expr $RC1 + $RC2`
	if  [ ${RC} -ne "0" ]; then
		echo "ERROR: Failed to remove files and directory"
		exit $RET_VALUE
	fi
fi

exit $SUCCESS
