#! /bin/sh
# $Revision: 1.34 $
#bcpyrght
#***************************************************************************
#* $VRTScprght: Copyright 1993 - 2005 VERITAS Software Corporation, All Rights Reserved $ *
#***************************************************************************
#ecpyrght
#
## Start NetBackup-Java admin console as a standalone Java application.
#

PATH=/bin:/usr/bin:/usr/ucb:$PATH; export PATH
INSTALL_DIR=/usr/openv/java
INSTALL_PATH=$INSTALL_DIR
DOTNBJ_CONF=$INSTALL_PATH/.nbjConf
NBJ_CONF=$INSTALL_PATH/nbj.conf

OS=`uname -s`

if [ "$OS" = "Linux" ]
then
	unset POSIXLY_CORRECT
	ECHO="/bin/echo -e"
else
	ECHO="/usr/bin/echo"
fi

## Read the default configuration variables for current version GUI.
. $DOTNBJ_CONF

DEBUG_PROPERTIES=$INSTALL_PATH/Debug.properties

## Set default values for INITIAL_MEMORY and MAX_MEMORY.
INITIAL_MEMORY=36M
MAX_MEMORY=256M
## Get the values for INITIAL_MEMORY and MAX_MEMORY from conf file.
## Assume the last uncommented one of each in the file is the one to use.
line=`cat $NBJ_CONF | egrep -v "^#" | egrep -i "INITIAL_MEMORY" | tail -1`
if [ "$line" != "" ] ; then
	INITIAL_MEMORY=`echo $line | cut -d= -f2 | awk '{print $1}'`
fi
line=`cat $NBJ_CONF | egrep -v "^#" | egrep -i "MAX_MEMORY" | tail -1`
if [ "$line" != "" ] ; then
	MAX_MEMORY=`echo $line | cut -d= -f2 | awk '{print $1}'`
fi
export INITIAL_MEMORY
export MAX_MEMORY

LOG_CMDLINES=false
LOGIN_HOST=""
HP64_FLAG=""
LOG_FILE=""
RELEASE_VERSION="6.0"
INITMEM=""
MAXMEM=""
CMD_OPTIONS="$@"

while [ "`$ECHO $1 | cut -c1`" = "-" ]
do
  case "$1" in
    -d | -display)
        DISPLAY=$2;export DISPLAY; shift 2;;
    -D) 
        DEBUG_PROPERTIES=$2;export DEBUG_PROPERTIES; shift 2;;
    -h)
        $ECHO "\nUsage: $0 [option]\n"
        $ECHO "  option:"
        $ECHO "  -d  Hostname to display to, e.g., 'eagle:0.0'."
        $ECHO "  -D  Debug properties file."
        $ECHO "  -h  Help."
        $ECHO "  -H  Hostname to login to, e.g., 'eagle'."
        $ECHO "  -l  Full path of log file."
        $ECHO "  -lc Log cmdlines used by the application to the log file."
        $ECHO "  -ms Initial Java VM memory."
        $ECHO "  -mx Maximum Java VM memory."
        $ECHO "  -r  Version of the console to use, either '5.0' or '5.1' or '6.0'"
        exit 2;;
    -H)
        LOGIN_HOST=$2;export LOGIN_HOST; shift 2;;
    -l)
        LOG_FILE=$2;export LOG_FILE; shift 2;;
    -lc)
        LOG_CMDLINES=true;export LOG_CMDLINES; shift 1;;
    -ms)
        INITMEM=$2;export INITMEM; shift 2;;
    -mx)
        MAXMEM=$2;export MAXMEM; shift 2;;
    -r)
        RELEASE_VERSION=$2;export RELEASE_VERSION; shift 2;;
    *)
        $ECHO "\nUsage: $0 [option]\n"
        $ECHO "  option:"
        $ECHO "  -d  Hostname to display to, e.g., 'eagle:0.0'."
        $ECHO "  -D  Debug properties file."
        $ECHO "  -h  Help."
        $ECHO "  -H  Hostname to login to, e.g., 'eagle'."
        $ECHO "  -l  Full path of log file."
        $ECHO "  -lc Log cmdlines used by the application to the log file."
        $ECHO "  -ms Initial Java VM memory."
        $ECHO "  -mx Maximum Java VM memory."
        $ECHO "  -r  Version of the console to use, either '5.0' or '5.1' or '6.0'"
        exit 2;;
  esac
done

## Validate -r data.
if [ "$RELEASE_VERSION" != "5.0" ] && [ "$RELEASE_VERSION" != "5.1" ] && \
	[ "$RELEASE_VERSION" != "6.0" ] ; then
	$ECHO "\n\n\tInvalid value, $RELEASE_VERSION, for the -r option - try again.\n"
	exit 1
fi

DOTNBJ_CONF=""
ERROR_LINE1=""

## Determine version of the GUI to startup by first checking the version
## of the host specified.  If errors or an unsupported version, do so based
## on the user specifying a release version (-r option).  Default to the
## current version if not specified.
if [ "$LOGIN_HOST" != "" ] ; then
	OUTPUT=`$INSTALL_PATH/../netbackup/bin/bpclntcmd -get_remote_host_version $LOGIN_HOST 2>&1`
	EXIT_STATUS=$?
	if [ "$EXIT_STATUS" != "0" ] ; then
		ERROR_LINE1="\nCould not get version of host $LOGIN_HOST - ERROR:"
		ERROR_LINE2="\t$OUTPUT (exit status = $EXIT_STATUS)."
		$ECHO "\n"$ERROR_LINE1
		$ECHO $ERROR_LINE2
	else
		VERSION=`$ECHO $OUTPUT | cut -d";" -f5`
		if [ "$VERSION" = "5.0GA" ] ; then
			DOTNBJ_CONF=$INSTALL_PATH/.nbjConf_50
			RELEASE_VERSION=5.0
		elif [ "$VERSION" = "5.1" ] ; then
			DOTNBJ_CONF=$INSTALL_PATH/.nbjConf_51
			RELEASE_VERSION=5.1
		elif [ "$VERSION" = "6.0" ] ; then
			DOTNBJ_CONF=$INSTALL_PATH/.nbjConf
			RELEASE_VERSION=6.0
		else
			ERROR_LINE1="\n\tAdministration of server, $LOGIN_HOST, is not" 
			ERROR_LINE2="\tpossible due to its unsupported version, $VERSION."
			$ECHO $ERROR_LINE1
			$ECHO $ERROR_LINE2
		fi
	fi
fi
if [ "$DOTNBJ_CONF" = "" ] ; then
	# Start the version user possibly specified.
	if [ "$RELEASE_VERSION" = "5.0" ] ; then
		DOTNBJ_CONF=$INSTALL_PATH/.nbjConf_50
	elif [ "$RELEASE_VERSION" = "5.1" ] ; then
		DOTNBJ_CONF=$INSTALL_PATH/.nbjConf_51
	elif [ "$RELEASE_VERSION" = "6.0" ] ; then
		DOTNBJ_CONF=$INSTALL_PATH/.nbjConf
	fi
fi
if [ "$DOTNBJ_CONF" != "" ] && [ "$DOTNBJ_CONF" != "$INSTALL_PATH/.nbjConf" ]
then
	## Read the configuration variables for the backlevel version.
	## Current version was defaulted above.
	. $DOTNBJ_CONF
fi

## Set log file path if user specified on the cmdline or had JNB_LOG env var.
if [ "$LOG_FILE" = "" ] ; then
	if [ "$JNB_LOG" = "" ] ; then
		LOG_FILE=$NBJAVA_LOGS_PATH/jbp.`whoami`.jnbSA.$$.log
	else 
		LOG_FILE=$JNB_LOG
	fi
fi

## Set memory options if user specified on the cmdline.
if [ "$INITMEM" != "" ] ; then
	INITIAL_MEMORY=$INITMEM; export INITIAL_MEMORY
fi
if [ "$MAXMEM" != "" ] ; then
	MAX_MEMORY=$MAXMEM; export MAX_MEMORY
fi

# Set default location of the VxSS at component lib directory.
VXSSAT_LIBRARY_PATH=/opt/VRTSat/lib
VXSSAT_LOC_FILE=/etc/vx/vss/VRTSat.loc
if [ -f $VXSSAT_LOC_FILE ] ; then
	locLine=`cat $VXSSAT_LOC_FILE | egrep -i "ProductInstallDir"`
	VXSSAT_LIBRARY_PATH=`echo $locLine | cut -d= -f2 | awk '{print $1}'`/lib
fi

# Append VxSS at library path to VRTS_LIBRARY_PATH.
VRTS_LIBRARY_PATH=$VRTS_LIBRARY_PATH:$VXSSAT_LIBRARY_PATH

CLASSPATH=$PJAR:.:$INSTALL_PATH:$COMMONJAR:$DBEXTJAR:$JBPJAR:$CHART_JAR:$FSAJAR:$HSMJAR:$NBJAR:$PV_JAR:$KL_JAR:$JDOM_JAR:$HELPVIEWER_JAR:$SCHED_JAR:$CORBA_JAR:$JACORB_JAR:$JACORB_PBX_JAR:$JACORB_LOG_JAR:$JACORB_AV_JAR:$JACORB_CON_JAR:$JACORB_ANT_JAR:$VXSSIOP_JAR:$VXSSAT_LIBRARY_PATH/$ATWRAPPER_JAR; export CLASSPATH

# The following is needed to prevent Java from exceeding the default open
# file limit because it does not close image files fast enough.
ulimit -n 1024

case "$OS" in
	OSF1)
		LD_LIBRARY_PATH=$VRTS_LIBRARY_PATH:$LD_LIBRARY_PATH; export LD_LIBRARY_PATH;;
	AIX)
		LIBPATH=$VRTS_LIBRARY_PATH:$LIBPATH; export LIBPATH;;
	HP-UX) 
		SHLIB_PATH=$VRTS_LIBRARY_PATH:$SHLIB_PATH; export SHLIB_PATH;
		ARCH=`uname -m`
		if [ "$ARCH" = "ia64" ] ; then
			HP64FLAG="-d64"
		fi;;
	Linux) 
		LD_LIBRARY_PATH=$VRTS_LIBRARY_PATH:$LD_LIBRARY_PATH; export LD_LIBRARY_PATH;;
	SunOS)
		# On Solaris, due to a bug fix made by Sun for release levels after 
		# 5.6, if the locale is currently C/POSIX it needs to be set to 
		# ISO 8859-1 in order to display extended characters.
		RELEASE_LVL=`uname -r`
		if [ $RELEASE_LVL -ge 5.7 ] ; then
			if [ "$LC_ALL" = "" ] ; then
				eval LC_ALL="$LANG" >> /dev/null 2>&1
				export LC_ALL
			fi
			if [ "$LC_ALL" = "C" ] || [ "$LC_ALL" = "" ] ; then
				eval LC_ALL=iso_8859_1 >> /dev/null 2>&1
				export LC_ALL
				eval LANG=iso_8859_1 >> /dev/null 2>&1
				export LANG
			fi
		fi

		# On Solaris, LD_LIBRARY_PATH must be set for access to libX11 - default
		# it to /usr/openwin/lib.  If it doesn't exist, inform the user and quit.
		if [ -d /usr/openwin/lib ]
		then
			LD_LIBRARY_PATH=/usr/openwin/lib:$VRTS_LIBRARY_PATH:$LD_LIBRARY_PATH; export LD_LIBRARY_PATH;
		else   
			$ECHO "\n\tDirectory /usr/openwin/lib doesn't exist.  It is the default"
			$ECHO "\tlocation for libX11.so.4 which is required by NB-Java."
			$ECHO "\tPlease create the /usr/openwin/lib directory and a symbolic"
			$ECHO "\tlink to libX11.so.4 or modify this script to properly set the"
			$ECHO "\tLD_LIBRARY_PATH environment variable.\n"
			exit 1
		fi;;
esac

XENVIRONMENT=$INSTALL_PATH/Xenv;export XENVIRONMENT

if [ "$LOG_FILE" != "" ]
then
	$ECHO "\nDISPLAY environment variable is set to: $DISPLAY" > $LOG_FILE
	$ECHO "Console was started on:" >> $LOG_FILE
	$ECHO "\t`uname -a`\n" >> $LOG_FILE
	$ECHO "Command line options were:" >> $LOG_FILE
	$ECHO "\t"$0 $CMD_OPTIONS >> $LOG_FILE
	if [ "$ERROR_LINE1" != "" ] ; then
		$ECHO $ERROR_LINE1 >> $LOG_FILE
		$ECHO $ERROR_LINE2 >> $LOG_FILE
	fi
	$ECHO "\nStarting administration console version $RELEASE_VERSION.\n" >> $LOG_FILE
	$ECHO "\n\tStarting administration console version $RELEASE_VERSION." 
	$ECHO "\tThe log file for this execution instance is\n\t$LOG_FILE\n"
fi

locale >> $LOG_FILE 2>&1
$ECHO "" >> $LOG_FILE
lcCtypeLine=`locale | grep LC_CTYPE`
LCCTYPE=`echo $lcCtypeLine | cut -d= -f2`

# The ordering of parameters here is important.  In particular, the 
# -Dvrts.NBJAVA_CONF parameter must be first.  bpps and bp.kill_all are 
# using this to distinguish NBJava processes from NBAR and VSM java processes.

$JAVA_PATH -Dvrts.NBJAVA_CONF=$NBJ_CONF -showversion $HP64FLAG -Dvrts.common.server.LCCTYPE=${LCCTYPE} -Dvrts.common.server.LOG_CMDLINES=${LOG_CMDLINES} -Djava.library.path=${VRTS_LIBRARY_PATH} -Dvrts.common.utilities.DEBUG_PROPERTIES=$DEBUG_PROPERTIES -Dvrts.nbe.HOSTNAME=$LOGIN_HOST -Dvrts.nbe.INST_REL=$INSTALLED_RELEASE -Dvrts.nbe.BUILDNO=$BUILDNUMBER -Xms$INITIAL_MEMORY -Xmx$MAX_MEMORY vrts.nbe.AdminConsole >> $LOG_FILE 2>&1

if [ $? != "0" ] && [ -s $LOG_FILE ]
then
	cat $LOG_FILE
fi

## The end ...

