#!/bin/sh
#
#ident "@(#)wcadmin	1.10 06/07/27 SMI"
# Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.

# 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
DEFAULT_INST=console
CONSOLE_FILE=config.properties
SERVICE_FILE=service.properties

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

# Relocation - indicates running in a bundled package script
# during Solaris installation or diskless client installation.
# Return an error and exit.
if [ -n "$PKG_INSTALL_ROOT" -a "$PKG_INSTALL_ROOT" != "/" ]; then
    echo "Cannot execute command in this package installation" 1>&2
    exit 1
fi

# Get the subcommand name.
SUBCMD=""
if [ $# -gt 0 ]; then
    SUBCMD=$1
    shift 1
fi

# Function to return the instance name
getInstance() {

    inst=""
    while [ $# -gt 0 ]; do
	if [ "$1" = "-i" -o "$1" = "--instance" ]; then
	    shift 1
	    inst="$1"
	fi
	shift 1
    done
    if [ "${inst}" = "" ]; then
	inst="${DEFAULT_INST}"
    fi
    echo "${inst}"

}

# Get the optional instance name from CLI arguments
INSTNAME=`getInstance $*`
conf_file=${CONSOLE_CONF}/${INSTNAME}/${CONSOLE_FILE}

# Build up one big argument with a special value between options.
# This allows option values with white space to be handled.
ARGS=""
if [ $# -gt 0 ]; then
    ARGS=$1
    shift
    while [ $# -gt 0 ]; do
        ARGS="${ARGS}_WxS_$1"
        shift
    done
fi

# source the console properties for the following properties:
#   console_home        - where the console software is installed
#   container_classpath - extra jars for container configuration
. ${conf_file}

# Use the specified java path.
java_path=${java_home}
serv_file=${CONSOLE_CONF}/${INSTNAME}/${SERVICE_FILE}
if [ -f ${serv_file} ]; then
    temp=`grep -i "${SERVICE_JAVA_PROP}=" ${serv_file}`
    if [ -n "${temp}" ]; then
        temp_path=`echo $temp | /usr/bin/cut -f2 -d"="`
	if [ -x ${temp_path}/bin/java ]; then
	    java_path=${temp_path}
	else
	    echo "Invalid Java path set in ${SERVICE_JAVA_PROP}: ${temp_path}." 1>&2
	fi
    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

# Set native library path
NTV_PATH=${console_native}

# Set classpath
LIB_PATH=${console_home}/lib
PVT_PATH=${console_home}/private/lib
CLASSPATH=${PVT_PATH}/console_admin.jar:${PVT_PATH}/console_config.jar:${LIB_PATH}/consoleutil.jar:${LIB_PATH}/serviceapi.jar:${LIB_PATH}/serviceimpl.jar:${container_classpath}

# Execute the java based command
${JAVA_CMD} -classpath ${CLASSPATH} \
    -Dcom.sun.web.console.conf=${CONSOLE_CONF} \
    -Djava.library.path=${NTV_PATH} \
    -Djava.awt.headless=true \
    -Dhttps.protocols="TLSv1" \
    com.sun.web.console.admin.WebAdminDriver ${SUBCMD} "${ARGS}"

exit $?
