#!/bin/sh
#  $Id: tem.sh,v 1.9.6.1 2006/03/30 14:19:00 billwe Exp $
#  Copyright 2006 Sun Microsystems, Inc. All rights reserved.
#  This software code contains proprietary information 
#  and should be treated as confidential.
#
#  Component: Tier3 Enhancement Module for UNIX
#  Author:    stuarth
#
#  Start script which starts the Tier3 Loadprobe
#


# Remember the current working directory
if [ ! -d "$TEMCWD" ]; then
    TEMCWD=`pwd`
    export TEMCWD
fi

# This is a generic product script used to establish an environment
# before starting/stopping a product or component.
# This script was built from shell library
#   $Id: startup.shlib,v 10.165.6.1 2006/04/03 09:51:35 billwe Exp $
# This script has to run on every platform we support.  Any suggestions
# for improvement are welcome (but must be portable!)

# Version numbers
PRODUCT_VERSION="4.2"
PRODUCT_VERSION_FULL="4.20.983"
PRODUCT_VERSION_MAJOR=4
PRODUCT_VERSION_FEATURE=2
PRODUCT_VERSION_BUG=0
PRODUCT_VERSION_BUILD=983
PRODUCT_VERSION_CAB=4,2,0,983

# Product names
PRODUCT_FULL_NAME="Sun Secure Global Desktop Software"
PRODUCT_SHORT_NAME="Secure Global Desktop"
PRODUCT_SHORT_FILENAME="Tarantella"
TSP_FULL_NAME="Sun Secure Global Desktop Security Pack"
TSP_SHORT_NAME="Security Pack"
WCP_FULL_NAME="Sun Secure Global Desktop Windows Connectivity Pack"
WCP_SHORT_NAME="Windows Connectivity Pack"
TN3270_FULL_NAME="Sun Secure Global Desktop Mainframe Connectivity Pack"
TN3270_SHORT_NAME="Mainframe Connectivity Pack"
AS400_FULL_NAME="Sun Secure Global Desktop AS/400 Connectivity Pack"
AS400_SHORT_NAME="AS/400 Connectivity Pack"
S1PSTK_FULL_NAME="Sun Secure Global Desktop Channel for Sun ONE Portal Server"
S1PSIP_FULL_NAME="Sun Secure Global Desktop Integration Pack for Sun ONE Portal Server"
S1PSTK_SHORT_NAME="Secure Global Desktop Channel for Sun ONE Portal Server"
S1PSIP_SHORT_NAME="Secure Global Desktop Integration Pack for Sun ONE Portal Server"

OBJECT_MGR_NAME="Object Manager"
ARRAY_MGR_NAME="Array Manager"
NATIVE_WIN16_FULL_NAME="Sun Secure Global Desktop Native Client for Windows 3.1"
NATIVE_WIN16_SHORT_NAME="Native Client for Windows 3.1"
NATIVE_WIN32_FULL_NAME="Sun Secure Global Desktop Native Client for Windows 95, 98 and NT"
NATIVE_WIN32_SHORT_NAME="Native Client for Windows 95, 98 and NT"
NATIVE_WYSE_FULL_NAME="Sun Secure Global Desktop Native Client for Wyse"
NATIVE_WYSE_SHORT_NAME="Native Client for Wyse"
NATIVE_FULL_NAME="Sun Secure Global Desktop Native Client"
NATIVE_SHORT_NAME="Native Client"
NATIVE_REGISTRY_NAME="Tarantella Native Client"
TCC_FULL_NAME="Sun Secure Global Desktop Client"
TCC_HELPER_NAME="Sun Secure Global Desktop Client Helper"
TCC_ACTIVEX_NAME="Sun Secure Global Desktop ActiveX Control"
TCC_PLUGIN_NAME="Sun Secure Global Desktop Netscape Plug-in"
T3COMPONENT_WIN_NAME="Sun Secure Global Desktop Enhancement Module for Windows"
T3COMPONENT_UNIX_NAME="Sun Secure Global Desktop Enhancement Module for UNIX/Linux"
T3COMPONENT_SHORT_UNIX_NAME="tem"
T3COMPONENT_NAME="Sun Secure Global Desktop Enhancement Module"

# Watch the length of these names: IE refuses to remove packages if
# the name is too long.
ARCHIVE_NAME_ttalw="TCC Helper, version"
ARCHIVE_NAME_asad="3.x Framework Java Archive"
#ARCHIVE_NAME_tta="3.x Java Archive"
#ARCHIVE_NAME_tta="3.x Combined Java Archive"
ARCHIVE_NAME_tta="Client, "
ARCHIVE_NAME_tsp="3.x Security Java Archive"
ARCHIVE_NAME_proxy="3.x Proxy Java Archive"

ARCHIVE_SUFFIX="F"

NS_REGKEY="java/download/com/tarantella/tta"

PRODUCT_VENDOR="Sun Microsystems, Inc."
PRODUCT_SHORT_VENDOR="Sun"

# Capabilities:
# The format for the capabilities required by the client end of a
# connection is: CAPABILITIES_protocol_type_REQUIRED="expression"

# The format for the capabilities provided by the server end of a
# connection is: CAPABILITIES_protocol_type_PROVIDED="c1 c2 ... c3"

# Asad between a webtop client and server:
CAPABILITIES_ASAD_WEBTOPCLIENT_REQUIRED="(1.1 || 1.2)"
CAPABILITIES_ASAD_WEBTOPCLIENT_PROVIDED="1.1 1.2"

# Asad between two peer servers, or an admin tool and a server:
# 1.1 is core E3 array functionality.
# 1.2 is 3.01 array interoperability functionality.
# 2.1 is core Pisa client device mapping functionality (not a requirement)
CAPABILITIES_ASAD_PEER_REQUIRED="(1.1 || 1.2)"
CAPABILITIES_ASAD_PEER_PROVIDED="1.1 1.2 2.1"

# Client versions of server strings; same format, name prefixed by CLIENT
# Asad between a webtop client and server:
CLIENTCAPABILITIES_ASAD_WEBTOPCLIENT_REQUIRED="(1.1 || 1.2)"
CLIENTCAPABILITIES_ASAD_WEBTOPCLIENT_PROVIDED="1.1 1.2"


a_base_definstdir="/opt"
a_definstdir="/opt/tta_tem"
a_definstdir2="/usr/tta_tem"
a_propsdir="var/serverconfig/local"
a_propsdir_save="var/serverconfig/local/save"
a_propsfile="tier3loadbalancing.properties"
a_temppropsfile="/tmp/tier3loadbalancing.properties"
a_piddir="var/info"
a_pidfile="tier3loadprobe.pid"
a_logdir="var/log"
a_binaryfile="tier3loadprobe"

portKey="tarantella.config.tier3loadbalancing.port"
mVmemKey="tarantella.config.tier3loadbalancing.maxvirtualmemory"
mPmemKey="tarantella.config.tier3loadbalancing.maxphysicalmemory"
mLogDir="tarantella.config.tier3loadbalancing.logdirectory"
mPidDir="tarantella.config.tier3loadbalancing.piddirectory"
mProbePath="tarantella.config.tier3loadbalancing.probepath"

#------------------------------------------------------------------

# Messages for the top level tem script.
# All the ones pertaining to getting messages.
l_langdefault="Attempting to use default English (en-us) messages"
l_langunknown="Unrecognized \$LANG: "
l_langunset="\$LANG is not set."
lf_langright="Please check the \$LANG variable is correct."
l_langsetto="\$LANG is currently set to: "
l_gottheselangs="Message files are installed for the following languages:"
l_usinglang="Using messages in"

# Other fatal messages that are displayed before lang sorted.
lf_reinst="The installation is not in a valid state.
    You need to reinstall it."

#------------------------------------------------------------------
#
# This function is called for Solaris and UnixWare 7 to get the number
# of active processors using "psrinfo"
#
do_psrinfo()
{
    NUM_PROCESSORS=`psrinfo -v|grep "on-line"|wc -l`
}
#------------------------------------------------------------------
# GetDirs: gets a list of filenames, and echoes to stdout the ones which
# are directories, and which are not in an exclusion list.  (The exclusion
# list comprises subdirectories of INSTDIR/bin/scripts which are known
# not to be language-specific message directories.)
GetDirs()
{
  for x do
    if [ -d "$x" ]; then
      case "$x" in
      */install_scripts) ;;
      */vendor) ;;
      *) echo $x
      esac
    fi
  done
}
#------------------------------------------------------------------

u="`LANG=C LC_ALL=C id | sed 's/.*uid=\([0-9][0-9]*\).*/\1/'`"
if [ "$u" = "0" ]
then
  : # all is okay
else
  echo "You must be root to run this program"
  exit 15
fi


#------------------------------------------------------------------
# export the directory containing this script, so that we can find message
# information by relative pathnames
temcommand=$0
if [ $trace ]; then echo "   trace: \$0=\"$temcommand\""; fi
case "$temcommand" in
/*) cd `dirname $temcommand`
    ;;
*)  fullpath=`pwd`/$temcommand
    cd `dirname $fullpath`
esac

# Set up environment variables - TEMBINDIR and TEMDIR

TEMBINDIR=`pwd`;export TEMBINDIR
TEMDIR=`dirname $TEMBINDIR`; export TEMDIR
cd $TEMDIR
if [ $trace ]; then echo "   trace: TEMDIR=\"$TEMDIR\"  TEMBINDIR=\"$TEMBINDIR\""; fi

VERSIONFILE=$TEMDIR/etc/pkg/temversion
#------------------------------------------------------------------

## messages directory
# if $LANG doesn't exist it is set to C
[ -z "$LANG" ] && LANG="C"

# set up TEMLANG variable - default en-us
TEMLANG=$LANG
case "$TEMLANG" in
C)  TEMLANG="en-us"
    ;;
[a-z][a-z]_[A-Z][A-Z])
    # convert LANG format from ab_CD to ab-cd
    TEMLANG=`echo "$TEMLANG" | LANG=C LC_ALL=C tr '[A-Z]_' '[a-z]-'`
    ;;
esac

#------------------------------------------------------------------
# attempt to find the language specific directory
# error message if nothing can be found

if [ ! -d $TEMBINDIR/scripts/$TEMLANG -o -z "$TEMLANG" ]; then
  if [ ! -z "$TEMLANG" ]; then
    posslangs=`GetDirs \`ls -d $TEMBINDIR/scripts/$TEMLANG* 2>/dev/null\``
  fi
  if [ -z "$posslangs" ]; then
    posslangs=`GetDirs \`ls -d $TEMBINDIR/scripts/[a-z][a-z]-[a-z][a-z]* 2>/dev/null\``
  fi
  nposslangs=`echo $posslangs | wc -w`
  if [ "$nposslangs" -gt 1 ]; then
    case "$TEMLANG" in
      ??* )
	    echo "$lf_nomessages $LANG" #!!!
	    ;;
      "" )
	   echo "$l_langunset"
	   ;;
      * )
	  TEMLANG=en-us
	  echo "$l_langunknown $LANG"
	  echo "$l_langdefault"
	  ;;
    esac
    echo "$l_gottheselangs"
    for a_lang in $posslangs; do
      output=`echo $a_lang | sed -e 's/.*\///'`
      echo "  $output"
      if [ -f $TEMBINDIR/scripts/$output/common -a "$TEMLANG" != "en-us" ]; then
	TEMLANG=$output
      fi
    done
    echo "$l_usinglang $TEMLANG"
  elif [ "$nposslangs" -eq 1 ]; then
    TEMLANG=`echo $posslangs | sed -e 's/.*\///'`
  else
    echo "$lf_reinst"
    exit 5
  fi
fi

#------------------------------------------------------------------

# export TEMMSG as the language specific directory
TEMMSG=$TEMBINDIR/scripts/$TEMLANG
export TEMMSG TEMLANG
if [ $trace ]; then echo "   trace: Language dir = $TEMMSG"; fi

# check the language specific tem message file exists
if [ -f $TEMMSG/common -a -f $TEMMSG/tem.msg ]; then
  . $TEMMSG/common
  . $TEMMSG/tem.msg
else
  echo "Unable to open message file: $TEMMSG/tem.msg"
  exit 1
fi


#------------------------------------------------------------------
PIDDIR=`grep piddirectory $TEMDIR/$a_propsdir/$a_propsfile|cut -d"=" -f2` >/dev/null 2>&1
if [ -n "$PIDDIR" -a -d "$PIDDIR" ]
then
    PIDFILE=$PIDDIR/$a_pidfile
else
    PIDDIR=${TEMDIR}/var/info
    PIDFILE=$PIDDIR/$a_pidfile
fi
if [ $trace ]; then echo "   trace: PIDFILE=\"$PIDFILE\""; fi
#------------------------------------------------------------------
# export the architecture code
SCOARCH=`$TEMBINDIR/scoarch -c` export SCOARCH

case "$SCOARCH" in
    i3li*|ppli*)
        # LINUX
        NUM_PROCESSORS=`cat /proc/cpuinfo|grep '^processor'|wc -l`
    ;;
    spso*|i3so*)
        # Solaris
        do_psrinfo
    ;;
    pahp*)
        # On HP-UX the number of processors is found programmatically by 
        # the tier3loadprobe binary"
        NUM_PROCESSORS=1
    ;;
    rsai*)
        # AIX
        NUM_PROCESSORS=`lsdev -C -c processor -S Available|wc -l`
    ;;
    i3un*)
        # UnixWare 7
        do_psrinfo
    ;;
    *)
        echo "$l_badarch"
        exit 1
    ;;
esac
#------------------------------------------------------------------

do_start()
{
    # Check for the configure file, if it exists the server needs
    # to be configured before starting up
    if [ -f ${TEMDIR}/var/configure ]; then
	PKG=tem export PKG
	INSTDIR=${TEMDIR} export INSTDIR
	INST_DATADIR=${TEMDIR}/etc/pkg export INST_DATADIR
	LOGDIR=${TEMDIR}/var/log export LOGDIR
	# Developers' note: the trailing slash in the REQDIR setting is important
	REQDIR=/tmp/teminst_$$/ export REQDIR
	rm -rf $REQDIR; mkdir -p $REQDIR 2>/dev/null; chmod 777 $REQDIR
	cp $INST_DATADIR/$PKG/install/??-??_setup $REQDIR
	LOGFILE=$REQDIR/tem.log$$ export LOGFILE
	RESPFILE=$REQDIR/tem.resp.$$ export RESPFILE
	TTA_TMPDIR=$REQDIR export TTA_TMPDIR
	TTA_RPM=t export TTA_RPM
	sh ${INST_DATADIR}/${PKG}/install/request ${RESPFILE}
	# get variables set by request script, and export them for use by postinstall
	. ${RESPFILE}
	export `sed '/=/!d;s/=.*//' ${RESPFILE}`
	sh ${INST_DATADIR}/${PKG}/install/postinstall
	rVal=$?
	# If postinstall returns an error exit now
	[ $rVal -ne 0 ] && exit 1
	rm -rf ${TEMDIR}/var/configure $REQDIR 2>/dev/null
    fi
    echo "${l_starting1}${NUM_PROCESSORS}${l_starting2}"
    PID=`ps -e|grep $a_binaryfile|grep -v grep|awk '{print $1}'`
    if [ "$PID" != "" ]
    then
        echo "$l_notstarting"
    else
        $TEMBINDIR/$a_binaryfile $TEMDIR/$a_propsdir $NUM_PROCESSORS &
        echo "${l_started_pid1}$!${l_started_pid2}"
    fi
}

do_stop()
{
    RUNPID=`ps -e|grep $a_binaryfile|grep -v grep|awk '{print $1}'`
    if [ $trace ]; then echo "   trace:RUNPID = $RUNPID"; fi
    if [ ! -f $PIDFILE ]
    then
        if [ "$RUNPID" != "" ]
        then
            echo "${l_notstopping1}${PIDFILE}${l_notstopping2}"
            if [ $trace ]; then echo "   trace:PIDFILE \"$PIDFILE\" not present"; fi
        else
            echo "$l_notrunning"
        fi
    else
        PID=`cat $PIDFILE`
        if [ "$PID" != "" ]
        then
            echo "${l_stopping1}${PID}${l_stopping2}"
            kill -15 $PID
            rm -rf $PIDFILE >/dev/null 2>&1
        else
            echo "${l_notstopping1}${PIDFILE}${l_notstopping2}"
            if [ $trace ]; then echo "   trace:PIDFILE \"$PIDFILE\" empty"; fi
        fi
    fi
}
do_version()
{
    if [ -f $VERSIONFILE ]
    then
        cat $VERSIONFILE
    else
        echo "Version unknown"
    fi
}
#------------------------------------------------------------------

case $1 in
    start)
        do_start
        exit 0
    ;;
    stop)
        do_stop
        exit 0
    ;;
    restart)
        do_stop
        do_start
        exit 0
    ;;
    version)
        do_version
        exit 0
    ;;
    *)
	    echo "$0 [start|stop|restart|version]"
    ;;
esac
