#!/bin/sh
#
#ident "@(#)sjwcx	1.4 06/09/01 SMI"
# Copyright (c) 2006 Sun Microsystems, Inc.
# All Rights Reserved.

# Console start and stop command called from Solaris SMF service.
# Mirrors smcwebserver command, but called from a console 
# service instance SMF start, stop or restart function.  The instance
# option name matches the console service FMRI instance name.
# Syntax:
#
# sjwcx <subcommand> -i <instance> <options>

# Set umask for creating new files
umask 022

# Fixed constants for all runtime command scripts.
# Required to find the console instance configuration properties file.
CONSOLE_CONF=/etc/webconsole
INSTANCE_NAME=console
CONSOLE_FILE=config.properties
SERVICE_FILE=service.properties

# instance flags
INST_FLAG_SHORT="-i"
INST_FLAG_LONG="--instance"

# the driver
DRIVER_CLASS="com.sun.web.console.config.ConsoleConfigDriver"

# name of system porperty that holds the location
# of the console configuration directory
CONSOLE_CONF_PROP_NAME="com.sun.web.console.conf"

# Name of service property that overrides java_home
SERVICE_JAVA_PROP=java.home

# move to the root of the file system
# this is read access for all users
cd /

########################################
# Function to set the INSTANCE_NAME
# look for instance name in the command
# line argumets if it is not defined on 
# the command line leave INSATNCE_NAME
# set to the default
########################################
getInstance() {
    inst=""
    while [ $# -gt 0 ]; do
	if [ "$1" = "${INST_FLAG_SHORT}" -o "$1" = "${INST_FLAG_LONG}" ]; then
	    shift 1
	    inst="$1"
	fi
	shift 1
    done

    # update INSTANCE_NAME if we have a value
    # for instace other than the empty string
    if [ "${inst}" != "" ]; then
	INSTANCE_NAME=${inst}
    fi
}


##########
## main ##
##########

# user must be root
USERID=`id | sed -e 's/uid=\([0-9]*\).*/\1/'`
if [ $USERID -ne 0 ]; then
    echo "You must be the system's root user to manage the server." 1>&2
    exit 1
fi

# gather the command line arguments
args=$*

# check for instance name in a command line argument
getInstance ${args}

# source config file
conf_file=${CONSOLE_CONF}/${INSTANCE_NAME}/${CONSOLE_FILE}
if [ ! -f ${conf_file} ]; then
    echo "Console instance \"${INSTANCE_NAME}\" does not exist." 1>&2
    exit 1
fi
. ${conf_file}

# java command
java_path=${java_home}
serv_file=${CONSOLE_CONF}/${INSTANCE_NAME}/${SERVICE_FILE}
if [ -f ${serv_file} ]; then
    temp=`grep -i "${SERVICE_JAVA_PROP}=" ${serv_file}`
    if [ -n "${temp}" ]; then
        java_path=`echo $temp | /usr/bin/cut -f2 -d"="`
    fi
fi
JAVA_CMD=${java_path}/bin/java
if [ ! -x ${JAVA_CMD} ]; then
    echo "Java command \"${JAVA_CMD}\" does not exist." 1>&2
    exit 1
fi

# build the jar directory names
CONFIG_JAR="${console_home}/private/lib"
CONSOLE_JAR="${console_home}/lib"

# build the native library path
NTV_PATH=${console_native}

# build the classpath
DRIVER_CLASS_PATH="${CONFIG_JAR}/console_config.jar:${CONSOLE_JAR}/consoleutil.jar:${CONSOLE_JAR}/serviceapi.jar:${CONSOLE_JAR}/consoleimpl.jar:${CONSOLE_JAR}/serviceimpl.jar:${CONSOLE_JAR}/serviceapi.jar:${container_classpath}"


${JAVA_CMD} -cp ${DRIVER_CLASS_PATH} \
    -D${CONSOLE_CONF_PROP_NAME}=${CONSOLE_CONF} \
    -Djava.library.path=${NTV_PATH} \
    -Djava.awt.headless=true \
    ${DRIVER_CLASS} ${args}

exit $?
