#!/sbin/sh
#
# Copyright (c) 2001 by Sun Microsystems, Inc.
# All rights reserved.
#pragma ident  "@(#)smpatch.sh	1.17 02/06/07 Sun Microsystems"
#


##############################################################################
# Set up the full package name to our main application class
VIPER_CONSOLE=/usr/sadm/bin/smc

##############################################################################
# Set up localization text domain
TEXTDOMAIN=SUNW_ADMIN_CLI
export TEXTDOMAIN

##############################################################################
# Set up path to the java jre
if [ "$JAVA_HOME" = "" ]; then
	JAVA_HOME=/usr/java
	export JAVA_HOME
fi
JAVABIN=$JAVA_HOME/bin/java
if [ ! -f $JAVABIN ]; then
	MSG=`/usr/bin/gettext "No Java home directory has been set"`
	/usr/bin/printf "${MSG}\n"
	exit 1
fi
#
##############################################################################
#
#	The tool we want is the PatchMgrCli tool.  This tool performs
#       several tasks: V(Analyze patches), A(add patches) 
#       and D(download patches)
#
TOOL=com.sun.admin.patchmgr.cli.PatchMgrCli

SERVICETYPE="Wbem"

#DEBUGGING=ON
DEBUGGING=OFF


case $1 in
   analyze) TASK=V ;;
   add) TASK=A ;;
   remove) TASK=X ;;
   download) TASK=D ;;
   update) TASK=U ;;

   *) MSG=`/usr/bin/gettext "Please specify one of the following operations:   analyze, download, remove, update or add"`
      /usr/bin/printf "${MSG}\n"
      exit 1 ;;
esac
shift

# load in the common shared code that contains the definition for
# methods used by this shell (ie common_smc_cli_exec)
# ?? Do we need this ??
#COMMON_CLI_LIB=/usr/sadm/lib/smcommon
#. ${COMMON_CLI_LIB}


JVM_SERVICETYPE_ARG=-J-Dpatchmgr.serviceType=${SERVICETYPE}
JVM_DEBUGGING_ARG=-J-Dpatchmgr.Debugging=${DEBUGGING}
SMC_ARGS="$JVM_SERVICETYPE_ARG $JVM_DEBUGGING_ARG"

# Check command line arguments to see if we must insert the
# infamous SMC "--" separator option, which is now optional.
# If we get the separator, just use the arguments as is.
# If there is no separator option, then
#    If there are any remote options, then assume all smc options
#        add the "--" AFTER the arguments
#    Else assume all tool options
#        add the "--" BEFORE the arguments
# These assumptions could cause syntax errors, but we assume
# the smc argument parsing will catch them.  If BOTH remote and
# tool options are required, the "--" separator must be used.
#
# Note that we include "-h" as an smc option so we will not get a
# prompt for a password just to show help.  We will get the
# SMC terminal help however, not the tool help (sigh).

if [ $# -eq 0 ]
then
	${VIPER_CONSOLE} open -t \
		-J-Dpatchmgr.Debugging=${DEBUGGING} \
		-T ${TOOL} "$@" -- -K ${TASK}
else
	HASOPTS=false
	SMCOPTS=false
	SMC_OPTIONS="-B -D -H -h -l -p -r -u"
	export HASOPTS
	for i in $@
	do
		if [ "$i" = "--" ]
		then
			HASOPTS=true
			break
		fi
		j=`echo $i | cut -b1-2 -`
		if [ "$j" = "--" ]
		then
			SMCOPTS=true
		fi
		for k in $SMC_OPTIONS
		do
			if [ "$i" = "$k" ]
			then
				SMCOPTS=true
			fi
		done
	done
	if [ $HASOPTS = "true" ]
	then
		${VIPER_CONSOLE} open -t \
			-J-Dpatchmgr.Debugging=${DEBUGGING} \
			-T ${TOOL} "$@" -K ${TASK}
	elif [ $SMCOPTS = "true" ]
	then
		${VIPER_CONSOLE} open -t \
			-J-Dpatchmgr.Debugging=${DEBUGGING} \
			-T ${TOOL} "$@" -- -K ${TASK}
	else
		${VIPER_CONSOLE} open -t \
			-J-Dpatchmgr.Debugging=${DEBUGGING} \
			-T ${TOOL} -- "$@" -K ${TASK}
	fi
fi

exit 0
