#!/bin/sh
#  $Id: tem.sh 146648 2018-01-18 16:42:44Z kevin.charles@oracle.com $
#  Copyright (c) 2002, 2018, Oracle and/or its affiliates. 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 Enhancement Module components.
#

TEMCFG=/etc/sgdtem.conf

# 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: util.shlib,v 10.22 2015-03-31 12:25:49 pc161604 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="5.40.966"
PRODUCT_VERSION_FULL="5.40.966"
PRODUCT_VERSION_MAJOR=5
PRODUCT_VERSION_FEATURE=4
PRODUCT_VERSION_BUG=0
PRODUCT_VERSION_BUILD=966
PRODUCT_VERSION_BUILD_DECIMAL=966
PRODUCT_VERSION_AS_NUMBER=540966
PRODUCT_VERSION_CAB=5,4,0,966
PRODUCT_VERSION_RPM=5.40-966

# Product names
PRODUCT_FULL_NAME="Oracle Secure Global Desktop"
PRODUCT_SHORT_NAME="Secure Global Desktop"
PRODUCT_IDENT="SSGD"
PRODUCT_SHORT_FILENAME="Tarantella"
PRODUCT_LSBSCRIPTNAME="sun.com-sgd-base"
PRODUCT_LSBSCRIPT_RUN_LEVEL="90"
TSP_FULL_NAME="Oracle Secure Global Desktop Security Pack"
TSP_SHORT_NAME="Security Pack"
WCP_FULL_NAME="Oracle Secure Global Desktop Windows Connectivity Pack"
WCP_SHORT_NAME="Windows Connectivity Pack"
TN3270_FULL_NAME="Oracle Secure Global Desktop Mainframe Connectivity Pack"
TN3270_SHORT_NAME="Mainframe Connectivity Pack"
AS400_FULL_NAME="Oracle Secure Global Desktop AS/400 Connectivity Pack"
AS400_SHORT_NAME="AS/400 Connectivity Pack"
S1PSTK_FULL_NAME="Oracle Secure Global Desktop Channel for Sun ONE Portal Server"
S1PSIP_FULL_NAME="Oracle 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="Oracle Secure Global Desktop Native Client for Windows 3.1"
NATIVE_WIN16_SHORT_NAME="Native Client for Windows 3.1"
NATIVE_WIN32_FULL_NAME="Oracle 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="Oracle Secure Global Desktop Native Client for Wyse"
NATIVE_WYSE_SHORT_NAME="Native Client for Wyse"
NATIVE_FULL_NAME="Oracle Secure Global Desktop Native Client"
NATIVE_SHORT_NAME="Native Client"
NATIVE_REGISTRY_NAME="Tarantella Native Client"
INTCLI_FULL_NAME="Oracle Secure Global Desktop Client"
INTCLI_SHORT_NAME="Client"
TCC_FULL_NAME="Oracle Secure Global Desktop Client"
TCC_HELPER_NAME="Oracle Secure Global Desktop Client Helper"
TCC_ACTIVEX_NAME="Oracle Secure Global Desktop ActiveX Control"
TCC_PLUGIN_NAME="Oracle Secure Global Desktop Netscape Plug-in"
T3COMPONENT_WIN_NAME="Oracle Secure Global Desktop Enhancement Module for Windows"
T3COMPONENT_UNIX_NAME="Oracle Secure Global Desktop Enhancement Module for UNIX(TM) and Linux Platforms"
T3COMPONENT_SHORT_UNIX_NAME="tem"
T3COMPONENT_NAME="Oracle Secure Global Desktop Enhancement Module"
T3COMPONENT_LSBSCRIPTNAME="sun.com-sgd-em"
T3COMPONENT_LSBSCRIPT_RUN_LEVEL="90"
GWCOMPONENT_UNIX_NAME="Oracle Secure Global Desktop Gateway for UNIX(TM) and Linux Platforms"
GWCOMPONENT_NAME="Oracle Secure Global Desktop Gateway"
GWCOMPONENT_SHORTNAME="SGD Gateway"
GWCOMPONENT_LSBSCRIPTNAME="sun.com-sgd-gw"
GWCOMPONENT_LSBSCRIPT_RUN_LEVEL="91"

# 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"
WEBSTART_JAR_NAME="sgdwscs"
ARCHIVE_SUFFIX="K"

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

PRODUCT_VENDOR="Oracle Corporation, Inc."
PRODUCT_SHORT_VENDOR="Oracle"

# 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"

# Top-level item names in ENS datastore
DEFAULT_ROOT_ENS="organization"
SYSTEM_ROOT_ENS="Tarantella System Objects"
APP_ROOT_ENS="applications"
APPSERVER_ROOT_ENS="appservers"

WEB_SERVICES_VERSION=1.5


machinearch ()
{
#!/bin/sh
#  $Id: ixiarch.sh 23561 2012-05-15 08:22:43Z ww161634 $
#  Copyright (c) 1996, 2012, Oracle and/or its affiliates. All rights reserved.
#  This software code contains proprietary information
#  and should be treated as confidential.
#
#  Print canonical syscode for platform

# There are three valid types of command line:
#
# 1.     visionarch
# 2.     visionarch <available codes>...
# 3.	 visionarch [-c|-o]
#
# 1. The architecture is echoed
# 2. The best architecture from the list is returned
# 3. The -c and -o options return just the system Code or the OS type.

# 0) INITIALIZE VARIABLES

# Some of these are historical, but dont cause any harm
PATH=/bin:/usr/bin:/sbin:/usr/sbin:$XDTHOME/bin:$PMWMHOME/bin:$IXIHOME/bin:$PATH


# 1) Arguments
numcodes=0
for i
do
  case $i in
    -c )
      codeonly=1
    ;;
  
    -o )
      osonly=1
    ;;

    -n )
      if [ -z "$codeonly" -a -z "$osonly" ]
      then
	  displayname=1	
      fi
    ;;
 
    * )
      checkcodes="$checkcodes $i"
      numcodes=`expr $numcodes + 1`
    ;;

   esac
done

# fast exit
if [ $numcodes = 1 ]
then
  echo $checkcodes
  exit 0
fi


# 2) MESSAGES
myname=`basename $0`

#  First define some messages.
#  They get overwritten by sourcing from a file, scripts.ll_TT,
#  so there's no need to translate them here.  

#  Messages for the visionarch script.
#  These follow text like "visionarch: !4! ", and may extend to several lines.
msg_xdtarch_no_uname=\
'No uname command found.'
msg_xdtarch_os_cpu=\
'Cannot recognize the Operating System or processor type.'
msg_xdtarch_ver_rel=\
'Cannot calculate version or release.'
msg_xdtarch_no_match=\
'Cannot find matching installed system.'

#  End of messages
# Try to find a language script
posslangfiles=""
for lang in C $IXILANG $PANLANG $LANG
do
  for pathname in $XDTHOME/bin/scripts. $PMWMHOME/bin/scripts. $IXIHOME/bin/scripts. ./scripts.
  do
    posslangfiles="$posslangfiles $pathname.$lang"
  done
done

# see if we can find one of those language files
for f in $posslangfiles
do
  if [ -f $f ]
  then
    . $scriptslangfile			# source it
    break
  fi						
done
  
# 3) TRY UNAME AND SET UP VARIABLES

unamev=`uname -v 2>/dev/null`
if [ $? -ne 0 ]
then
  # This isn't really a UNIX system is it?
  echo >&2  "$myname: !2! $msg_xdtarch_no_uname"
  exit 2
fi

U_SYSTEM=`uname -s 2>/dev/null`  || U_SYSTEM=UNKNOWN
U_RELEASE=`uname -r 2>/dev/null` || U_RELEASE=UNKNOWN
U_VERSION=`uname -v 2>/dev/null` || U_VERSION=UNKNOWN

# Some machines use -m arg to return 'model' rather
# than architecture.  For example, NCR SVR4 returns
# 3433,3434.  On such machines need to use -p which
# returns processor.

for MACHARG in m p
do
    U_MACHINE=`uname -"$MACHARG" 2>/dev/null` || U_MACHINE=UNKNOWN

    # 4)  FIND THE MACHINE
    # Note that we only look for cpu/os combinations our products currently
    # support - if you want better matching, most GNU software comes with
    # a script called 'config.guess' which does a more thorough job (and
    # has totally different output!)

    CPU= 			# Characters 1-2 of syscode
    OS= 			# Characters 3-4 of syscode
    VER=			# Digits 5-6 of syscode
    REL= 			# Digits 7-8 of syscode


    case ${U_MACHINE}::${U_SYSTEM}::${U_RELEASE}::${U_VERSION} in
      # Intel FreeBSD
      i*::FreeBSD::*:* )
        CPU=i3
        OS=fb
        NAME="FreeBSD (Intel)"
        VER=`expr $U_RELEASE : '[^0-9]*\([0-9][0-9]*\)[^0-9]'`
        REL=`expr $U_RELEASE : '[^0-9]*[0-9][0-9]*[^0-9]\([0-9][0-9]*\)'`
       ;;
      # MacOS X
      powerpc::Darwin::*::*)
        CPU=pp
        OS=dw
        NAME="Darwin (PowerPC)"
        VER=`expr $U_RELEASE : '[^0-9]*\([0-9][0-9]*\)[^0-9]'`
        REL=`expr $U_RELEASE : '[^0-9]*[0-9][0-9]*[^0-9]\([0-9][0-9]*\)'`
       ;;
      # MacOS X (Intel)
      i386::Darwin::*::*)
        CPU=i3
        OS=dw
        NAME="Darwin (Intel)"
        VER=`expr $U_RELEASE : '[^0-9]*\([0-9][0-9]*\)[^0-9]'`
        REL=`expr $U_RELEASE : '[^0-9]*[0-9][0-9]*[^0-9]\([0-9][0-9]*\)'`
       ;;
      # PowerPC Linux
      ppc*::Linux::*::*)
        CPU=pp
        OS=li
        NAME="Linux (PowerPC)"
        VER=`expr $U_RELEASE : '[^0-9]*\([0-9][0-9]*\)[^0-9]'`
        REL=`expr $U_RELEASE : '[^0-9]*[0-9][0-9]*[^0-9]\([0-9][0-9]*\)'`
       ;;
      # Intel Linux
      i*::Linux::*::* )
	CPU=i3
	OS=li
	NAME="Linux (Intel)"
	VER=`expr $U_RELEASE : '[^0-9]*\([0-9][0-9]*\)[^0-9]'`
	REL=`expr $U_RELEASE : '[^0-9]*[0-9][0-9]*[^0-9]\([0-9][0-9]*\)'`
      ;;
      # Athlon 64 Linux
      x86_64::Linux::*::* )
	CPU=i3
	OS=li
	NAME="Linux (AMD 64)"
	VER=`expr $U_RELEASE : '[^0-9]*\([0-9][0-9]*\)[^0-9]'`
	REL=`expr $U_RELEASE : '[^0-9]*[0-9][0-9]*[^0-9]\([0-9][0-9]*\)'`
      ;;
      # PowerPC Linux
      ppc*::Linux::*::* )
        CPU=pp
        OS=li
        NAME="Linux (PowerPC)"
        VER=`expr $U_RELEASE : '[^0-9]*\([0-9][0-9]*\)[^0-9]'`
        REL=`expr $U_RELEASE : '[^0-9]*[0-9][0-9]*[^0-9]\([0-9][0-9]*\)'`
       ;;
      # Sequent (Intel)
      i*::DYNIX*::*::* )
	CPU=i3
	OS=dy
	NAME="Sequent DYNIX/ptx (Intel)"
	VER=`expr $U_VERSION : 'V\([0-9]\).*'`
	REL=`expr $U_VERSION : 'V[0-9].\([0-9].[0-9]\)' | sed 's/\.//g'`
      ;;

      # The Sun family
      *::SunOS::4.*::* )
	CPU=sp
	OS=su
	VER=4
	NAME="Sun SunOS (Sparc)"
	REL=`expr $U_RELEASE : '4.\([^_]*\)' | sed 's/[^0-9]//g'`
      ;;
      sun*::SunOS::5.*::* )
	CPU=sp
	OS=so
	VER=5
	NAME="Sun Solaris (Sparc)"
	REL=`expr $U_RELEASE : '5.\([0-9][0-9]*\).*' | sed 's/[^0-9]//g'`
      ;;
      *86*::SunOS::5.*::* )
	CPU=i3
	OS=so
	VER=5
	NAME="Sun Solaris (Intel)"
	REL=`expr $U_RELEASE : '5.\(.*\)' | sed 's/[^0-9]//g'`
      ;;
      *::SunOS::5.*::* )
	CPU=sp
	OS=so
	VER=5
	NAME="Sun Solaris (Sparc)"
	REL=`expr $U_RELEASE : '5.\([0-9][0-9]*\).*' | sed 's/[^0-9]//g'`
      ;;

      # HP
      *::HP-UX::*::* )
	CPU=pa
	OS=hp
	NAME="Hewlett-Packard HP-UX (PA-RISC)"
	VER=`expr $U_RELEASE : '[^.]*.\([0-9]*\).*'`
	REL=`expr $U_RELEASE : '[^.]*.[^.]*.\([0-9]*\)'`
      ;;

      # Data General
      *::dgux::5.4*::* )
	CPU=m8
	OS=dg
	VER=54
	NAME="Data General DG/UX (Motorola)"
	REL=`expr $U_RELEASE : '5.4\(.*\)' | sed 's/[^0-9]//g' | sed 's/\(.\).*\(.\)$/\1\2/'`
      ;;

      # Data General on Intel
      *::dgux::R4*::* )
	case `uname -p`  in
	  m* )
	    CPU=m8 ;;
	  * )
	    CPU=i3 ;;
	esac
	OS=dg
	VER=04
	NAME="Data General DG/UX (Intel)"
	REL=`expr $U_RELEASE : 'R4.\(.*\)' | sed 's/[^0-9]//g'`
        REL=`echo $REL | sed 's/\(..\).*/\1/'`
      ;;

      # OSF/1
      alpha::OSF1::*::* )
	CPU=ax
	OS=o1
	NAME="DEC OSF/1 (Alpha)"
	VER=`echo $U_RELEASE | sed 's/\([^.]*\).*/\1/' | sed 's/[^0-9]//g'`
	REL=`echo $U_RELEASE | sed 's/[^.]*.\(.*\)/\1/' | sed 's/[^0-9]//g'`
      ;;

      # ULTRIX
      *::ULTRIX::*::* )
	CPU=mp
	OS=ul
	NAME="DEC Ultrix (MIPS)"
	VER=`echo $U_RELEASE | sed 's/\([^.]*\).*/\1/' | sed 's/[^0-9]//g'`
	REL=`echo $U_RELEASE | sed 's/[^.]*.\(.*\)/\1/' | sed 's/[^0-9]//g'`
      ;;

      # IRIX
      *::IRIX::4.*::* )
	CPU=mp
	OS=ir
	VER=`echo $U_RELEASE | sed 's/\([^.]*\).*/\1/' | sed 's/[^0-9]//g'`
	REL=`echo $U_RELEASE | sed 's/[^.]*.\(.*\)/\1/' | sed 's/[^0-9]//g'`
      ;;
      
      *::IRIX::5.1::* )
	CPU=mp
	OS=ir
	VER=`echo $U_RELEASE | sed 's/\([^.]*.[^.]*\).*/\1/' | sed 's/[^0-9]//g'`
	REL=`echo $U_RELEASE | sed 's/[^.]*.[^.]*.\(.*\)/\1/' | sed 's/[^0-9]//g'`
      ;;

      # Note
      # If we ever get minor release numbers of 5.2 we'll have to do
      # something different
      *::IRIX::5.2::* )
	CPU=mp
	OS=ir
	VER=`echo $U_RELEASE | sed 's/\([^.]*\).*/\1/' | sed 's/[^0-9]//g'`
	REL=20
      ;;
      # Note
      # This system does not exist yet
      *::IRIX::5.2.*::* )
	CPU=mp
	OS=ir
	VER=`echo $U_RELEASE | sed 's/\([^.]*\).*/\1/' | sed 's/[^0-9]//g'`
	REL=`echo $U_RELEASE | sed 's/[^.]*.\(.*\)/\1/' | sed 's/[^0-9]//g'`
      ;;

      # catchall for other irix versions
      # 6.4 and later change from IRIX to IRIX64
      *::IRIX*::*::* )
	CPU=mp
	OS=ir
	VER=`echo $U_RELEASE | sed 's/\([^.]*\).*/\1/' | sed 's/[^0-9]//g'`
	REL=`echo $U_RELEASE | sed 's/[^.]*.\(.*\)/\1/' | sed 's/[^0-9]//g'`
      ;;

      # AIX
      # note that we can't figure out the patch level of a 3.2.x (x<4)
      # system (even IBM can't tell you immediately!)
      *::AIX::*::* )
	CPU=rs
	OS=ai
	NAME="IBM AIX (Risc)"
	VER=$U_VERSION
	REL=$U_RELEASE
      ;;

      # RISC OS (the mips variety, not acorn)
      # version 5 like output
      mips::*::*::RISCos )
	CPU=mp
	OS=ro
	VER=`echo $U_RELEASE | sed 's/\([^.]*\).*/\1/' | sed 's/[^0-9]//g'`
	REL=`echo $U_RELEASE | sed 's/[^.]*.\(.*\)/\1/' | sed 's/[^0-9]//g'`
      ;;
      # version 4 like output
      mips::*::*::UMIPS )
	CPU=mp
	OS=ro
	VER=`echo $U_RELEASE | sed 's/\([0-9]*\).*/\1/'`
	REL=`echo $U_RELEASE | sed 's/[0-9]*\(.*\)/\1/' | sed 's/[^0-9]//g'`
      ;;

      # Siemens Nixdorf mips R4000 (at least)
      R4000::*::*::* )
	CPU=mp
	OS=sx
	NAME="Siemens Nixdorf SINIX (MIPS)"
	VER=`echo $U_RELEASE | sed 's/\([0-9]*\).*/\1/'`
	REL=`echo $U_RELEASE | sed 's/[0-9]*\(.*\)/\1/' | sed 's/[^0-9]//g'`
      ;;

      # Motorola SVR4
      *88*::*::4*::* )
	CPU=m8
	OS=mo
	VER=`expr $U_VERSION : '[^V]*.\([^.]*\).*' | sed 's/[^0-9]//g'`
	REL=`expr $U_VERSION : '[^V]*.[^.]*.\(.*\)' | sed 's/[^0-9]//g'`
      ;;

      # windows nt with nutcracker
      *::Windows_NT::*::* )
	CPU=i3
	OS=wi
	VER="$U_RELEASE"
	REL="$U_VERSION"
      ;;

      DRS*::*::*::* )
	  PROC=`uname -p 2>/dev/null` || PROC=UNKNOWN
	  case "$PROC" in
	    sparc | SPARC )
	      CPU=sp
	      OS=nx
	      NAME="ICL DRS/NX (Sparc)"
	      ;;
	    i386 )
	      CPU=i386
	      OS=nx
	      NAME="ICL DRS/NX (Intel)"
	      ;;
	  esac

	  VER=`expr $U_VERSION : '\([0-9]\).*'`
	  REL=`expr $U_VERSION : '[0-9]..*\([0-9].[0-9]\)' | sed 's/\.//g'`
      ;;

      
      *::UnixWare::*::* )
	CPU=i3
	OS=un
	NAME="SCO UnixWare (Intel)"
	VER=`expr $U_VERSION : '\([0-9]\)'`
	REL=`expr $U_VERSION : '[0-9].\(.*\)' | sed -e 's/\.//'`
	if [ -z "$REL" ]; then
		REL=00
        elif [ "x`expr $REL : '[0-9]\([0-9]\)'`" = "x" ] ; then
		REL="$REL"0
	fi
	;;

      *::OpenUNIX::*::* )
        CPU=i3
        OS=un
        NAME="SCO UnixWare (Intel)"
        VER=`expr $U_VERSION : '\([0-9]\)'`
        REL=`expr $U_VERSION : '[0-9].\(.*\)' | sed -e 's/\.//'`
        if [ -z "$REL" ]; then
                REL=00
        elif [ "x`expr $REL : '[0-9]\([0-9]\)'`" = "x" ] ; then
                REL="$REL"0
        fi
        ;;

      # Various Intel based Unixen.  These seem particularly reluctant
      # to divulge their OS name, so we have to grep through
      # various system files to find the OS string

      *86*::*::*::* )
	if grep -s -i UnixWare /stand/boot 2>/dev/null 1>&2 || strings /stand/1/unix_mp 2>/dev/null | grep -s -i Unixware 2>/dev/null 1>&2
	then
          # It's some version of Unixware obviously
	  CPU=i3
	  OS=un
	  NAME="SCO UnixWare (Intel)"
          if expr $U_VERSION : 'D\([0-9]*.*\)' 2>/dev/null 1>&2
	  then
            # It's a date stamped version ; we dont yet know how to figure out
            # which so just pretend that it is unixware 212
            VER=02
            REL=12
          else
	    VER=`expr $U_VERSION : '\([0-9]\)'`
	    REL=`expr $U_VERSION : '[0-9].\(.*\)' | sed -e 's/\.//'`
	    if [ "x`expr $REL : '[0-9]\([0-9]\)'`" = "x" ] 
	    then
		REL="$REL"0
	    fi
          fi
	else
	  if [ "$U_SYSTEM" = "SCO_SV" ]
	  then
	    CPU=i3
	    OS=sc
	    VER=`expr $U_RELEASE : '^\([0-9]*\)'`
	    NAME="SCO OpenServer (Intel)"
	    # just the release string ie: 3.2v5.0.0
	    relstr=`uname -X | grep Release | awk '{ print $3 }'`
	    # just the major SCO version after the SVR? (3.2v) bit
	    U_VERSION=`expr ${relstr} : '[0-9.]*v\([0-9]\)[0-9.]*'`
	    REL=`expr ${U_RELEASE}::${U_VERSION} : '[0-9]*\(.*\)' | sed 's/[^0-9]//g'`
	  else
	    if grep -s 'The Santa Cruz Operation' /etc/gettydefs 2>/dev/null 1>&2
	    then
	      CPU=i3
	      OS=sc
	      NAME="SCO Unix (Intel)"
	      VER=`expr $U_RELEASE : '^\([0-9]*\)'`
	      REL=`expr ${U_RELEASE}::${U_VERSION} : '[0-9]*\(.*\)' | sed 's/[^0-9]//g'`
	    else
	      # DYNIX/ptx V2 doesn't return 'DYNIX' or 'ptx' in uname.
	      # This kludge may work, may not, it's either this
	      # or flip a coin!   
	      if grep -s 'ptx' /etc/versionlog 2>/dev/null 1>&2
	      then
		CPU=i3
		OS=dy
		NAME="Sequent DYNIX/ptx (Intel)"
		VER=`expr $U_VERSION : 'V\([0-9]\).*'`
		REL=`expr $U_VERSION : 'V[0-9].\([0-9].[0-9]\)' | sed 's/\.//g'`
	      else
	        # Assume OS must be System V Release 4.
	        CPU=i3
	        OS=sv
		NAME="System V Release 4 (Intel)"
	        VER=`expr $U_RELEASE : '^\([0-9]*\)'`
	        REL=`expr ${U_RELEASE}::${U_VERSION} : '[0-9]*\(.*\)' | sed 's/[^0-9]//g'`
	      fi
	    fi
	  fi
	fi
	;;	
    esac

    if [ -n "$CPU" -a -n "$OS" -a -n "$VER" -a -n "$REL" ]
    then
	break
    fi
done

# 5) Do sanity checking and cleanup
VER=`echo $VER | sed 's/[^0-9]//g'`
REL=`echo $REL | sed 's/[^0-9]//g'`

if [ -z "$CPU" -o -z "$OS" ]
then
  echo "$myname: !10! $msg_xdtarch_os_cpu"
  exit 10
fi

if [ -z "$VER" -o -z "$REL" ]
then
  echo "$myname: !11! $msg_xdtarch_ver_rel"
  exit 11
fi

# Add leading zeroes
REL=`echo 0$REL | sed 's/.*\(..\)/\1/'`
VER=`echo 0$VER | sed 's/.*\(..\)/\1/'`

# 6) So we now know it all

syscode=$CPU$OS$VER$REL

if  test -n "$codeonly"
then
    echo $CPU$OS
    exit 0
fi

if  test -n "$osonly"
then
    echo $VER$REL
    exit 0
fi

# 7) Check for codes given on the command line

if [ ! -z "$checkcodes" ]
then
    for i in $checkcodes
    do
	# Filter the input $checkcodes by matching CPU and OS, and strip
	# them off leaving just the VER and REL.  The tr command replaces
	# spaces by line-feeds, so that the following sort can do its stuff.
	posscodes=`echo $checkcodes | tr ' ' '\012' \
				| sed -ne "s/$CPU$OS//p" | sort -n`
	if [ -z "$posscodes" ]
	then				# None supplied for correct CPU+OS
	    echo >&2 "$myname: !5! $msg_xdtarch_no_match"
	    exit 5
	fi 

	# Some of them are the right platform.
	# The following algorithm returns the greatest possible code which
	# is older than this syscode, or, failing that, the least possible
	# code which is more recent than this one.

	for ver_rel in $posscodes
	do
	    if [ $VER$REL -eq $ver_rel ]
	    then 			# bingo we have this exact release
		echo $syscode
		exit 0
	    elif [ $VER$REL -gt $ver_rel ]
	    then 			# store this previous version
		priorcode=$CPU$OS$ver_rel
	    else 			# $VER$REL is < $ver_rel
		if [ -n "$priorcode" ]
		then
		    echo $priorcode
		    exit 0
		else
		    # Oh dear, we have no previous version so we should use
		    # the oldest newer version which is this current one ...
		    echo $CPU$OS$ver_rel
		    exit 0
		fi
	    fi
	done
    done
    if [ -n "$priorcode" ]
    then
        echo $priorcode
        exit 0
    fi
else
    if [ -n "$displayname" ]
    then
	echo "$NAME"
    else
	echo $syscode
    fi
fi

}
MACHINEARCH=`machinearch`
MACHINEFLAVOR=""
LSBFLAVOR=""
if [  `expr $MACHINEARCH : ..li....` -ne 0 ] ; then
	distrib=`ls /etc/*-release 2>/dev/null`
	n_distrib=`echo $distrib | LANG=C LC_ALL=C tr '[A-Z]' '[a-z]'`
        
        for f_distrib in $n_distrib ; do 
	  case "$f_distrib" in
	    *turbolinux* )
	        MACHINEFLAVOR="turbolinux"
		;;
	    *suse* )
	        MACHINEFLAVOR="suse"
	        ;;
	    *caldera* )
                MACHINEFLAVOR="caldera"
                ;;
	    *redhat*|*fedora* )
	        MACHINEFLAVOR="redhat"
	        ;;
            *lsb* )
                eval `grep ^DISTRIB_ID /etc/lsb-release`
                case "$DISTRIB_ID" in
                 *SuSE* )
                        LSBFLAVOR="suse"
                        ;;
                 "SCO Linux"* ) 
                        LSBFLAVOR="scolinux"
                        ;;
                 *UnitedLinux* ) 
                        LSBFLAVOR="unitedlinux"
                        ;;
                esac
                ;;
	    * )
		# Caldera and Debian don't have a -release.
		# But Debian has a debian_version
		if [ -f /etc/debian_version ] ; then
			MACHINEFLAVOR="debian"
		else
			distrib=`rpm -q -a 2>/dev/null | grep OpenLinux 2>/dev/null`
			r_distrib=`echo $distrib | LANG=C LC_ALL=C tr '[A-Z]' '[a-z]'`
			case "$r_distrib" in
				*openlinux* )
					MACHINEFLAVOR="caldera"
					;;
			esac
		fi
     		;;
	  esac
        done
        # use the LSB flavor iff we haven't determined a flavor from files
        # and packages.
        if [ -z "$MACHINEFLAVOR" -a  ! -z "$LSBFLAVOR" ] ; then
                MACHINEFLAVOR=$LSBFLAVOR
        fi
fi



privusr="temsys"
privgrp="temserv"


AUDIO_OS=OSS
case "$MACHINEARCH" in
    spso*|i3so*)
        AUDIO_OS=SADA
esac

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"

a_ttatdmfile="ttatdm"
a_ttatdmclfile="ttatdmcl"
a_ttatdmconfigdir="etc"
a_ttatdmconfigfile="client.prf"

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=`machinearch -c` export SCOARCH

case "$SCOARCH" in
    i3li*|ppli*)
        # LINUX
        NUM_PROCESSORS=`cat /proc/cpuinfo|grep '^processor'|wc -l`
        PLATFORM_MOUNT_ARGS="-t nfs"
        PLATFORM_MOUNT_OPTS="udp,nfsvers=2,lookupcache=pos"
    ;;
    spso*|i3so*)
        # Solaris
        do_psrinfo
        PLATFORM_MOUNT_ARGS="-F nfs -O"
        PLATFORM_MOUNT_OPTS="proto=udp,vers=2"
    ;;
    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
#------------------------------------------------------------------

#------------------------------------------------------------------
# CDM variables.
SetupCDM()
{
    TTATDMCONFIG=${TEMDIR}/$a_ttatdmconfigdir/$a_ttatdmconfigfile 
    if [ -f $TTATDMCONFIG ]; then
        # Get the NFS exported directory.
        MOUNTPOINT=`grep mountpoint $TTATDMCONFIG | sed -e 's/\[nfsserver\/mount\/mountpoint={(\(.*\))}\]/\1/'`
        [ $? -ne 0 ] && return 1
       
        # Get the daemon's preferred listening port.
        PORT=`grep mount/port $TTATDMCONFIG | sed -e 's/\[nfsserver\/mount\/port={(\(.*\))}\]/\1/'`
        [ $? -ne 0 ] && return 1
    else
        return 1
    fi
    return 0
}

do_startaudio()
{
    # set the start type for the request script
    SGD_TEM_START_TYPE="startaudio" export SGD_TEM_START_TYPE

    # check to see if the server has been configured before running anything
    do_config

    # PulseAudio
    PULSE_INSTALLED=`grep PULSE_SINK $TEMCFG`
    if [ "${PULSE_INSTALLED}" != "" ] ; then
        PULSE_ENABLED=`grep PULSE_ENABLED $TEMCFG`
        if [ "${PULSE_ENABLED}" == "" ] ; then
            echo "PULSE_ENABLED=1; export PULSE_ENABLED" >> $TEMCFG
        fi
        echo "$l_unixaudiostartok_pulse"
    else
        echo "$l_unixaudionotinstalled_pulse"
    fi

# OSS/SADA audio
case "$SCOARCH" in
    i3li*|ppli*)
        # LINUX
        if [ -f ${TEMDIR}/audio/sgdadem/src/sgdadem.ko ] || [ -f ${TEMDIR}/audio/sgdadem/src/sgdadem.o ]; then
            mkdir /tmp/SGD 2>/dev/null
            PATH=/bin:/sbin:/usr/bin
            export PATH

            SGDAUDIOPID=`ps -e|grep sgdaudio|grep -v grep|awk '{print $1}'`
            if [ "$SGDAUDIOPID" != "" ]; then
               echo "$l_unixaudioalreadyrunning"    
               return 2
            fi

            SGDMODULE=`lsmod |grep sgdadem|awk '{print $1}'`
            if [ "$SGDMODULE" != "" ]; then
                echo "$l_unixaudioalreadyrunning"   
                return 2
            fi

            echo "$l_unixaudiostart"
            rm -f /dev/sgdadem 2>/dev/null
            depmod -a 
            if [ $? != "0" ]; then
                echo "$l_unixaudiodepmodfail"
                echo "$l_unixaudiostartfail"
                return 1
            fi
            #remove previously installed driver
            modprobe -r sgdadem
            if [ $? != "0" ]; then
                echo "$l_unixaudioloaded"
                return 2
            fi
	    ALLOW_UNSUPPORTED=''
	    [ -s /etc/SuSE-release ] && ALLOW_UNSUPPORTED='--allow-unsupported'
            modprobe ${ALLOW_UNSUPPORTED} sgdadem
            if [ $? != "0" ]; then
                echo "$l_unixaudiomodprobefail"
                echo "$l_unixaudiostartfail"
                return 1
            fi
            MINOR_NUM=`grep dsp /proc/driver/sgdadem | awk '{print $2}'`
            if [ -z $MINOR_NUM ]; then
                echo "$l_unixaudiominnumfail"
                echo "$l_unixaudiostartfail"
                return 1
            fi
            mknod -m 0777 /dev/sgdadem c 14 $MINOR_NUM 
            if [ $? != "0" ]; then
                echo "$l_unixaudiomknodfail"
                echo "$l_unixaudiostartfail"
                return 1
            fi
            echo "$l_unixaudiostartok"
        else
            echo "$l_unixaudionotinstalled"
            return 1
        fi
    ;;
    spso*|i3so*)
        # Solaris
        if [ -z "$PKG_INSTALL_ROOT" ]; then
            PKG_INSTALL_ROOT=""
            REM_DRV="/usr/sbin/rem_drv"
            ADD_DRV="/usr/sbin/add_drv"
        else
            REM_DRV="/usr/sbin/rem_drv -b ${PKG_INSTALL_ROOT}"
            ADD_DRV="/usr/sbin/add_drv -b ${PKG_INSTALL_ROOT}"
        fi
        #set -eu
        SGDADEM="sgdadem"
        TARGDIR="$PKG_INSTALL_ROOT/usr/kernel/drv"

        if [ -f "${TARGDIR}/${SGDADEM}" -a -f "${TARGDIR}/${SGDADEM}.conf" ]; then
            SGDMODULE=`/usr/sbin/modinfo -c|grep $SGDADEM|grep 'LOADED/INSTALLED'`
            if [ "$SGDMODULE" != "" ]; then
                echo "$l_unixaudioalreadyrunning"   
                return 2
            fi

            SGDAUDIOPID=`ps -e|grep sgdaudio|grep -v grep|awk '{print $1}'`
            if [ "$SGDAUDIOPID" != "" ]; then
               echo "$l_unixaudioalreadyrunning"    
               return 2
            fi

            echo "$l_unixaudiostart"
            #set +e
            $REM_DRV $SGDADEM 2>/dev/null
            #set -e

            $ADD_DRV -m '* 0600 bin bin' $SGDADEM 

            if [ $? != "0" ]; then
                echo "$l_unixaudioadddrvfail"
                echo "$l_unixaudiostartfail"
                return 1
            else
                echo "$l_unixaudiostartok"
            fi
            #set +eu
        else
            echo "$l_unixaudionotinstalled"
            return 1
        fi
        
    ;;
    *)
        echo "$l_badarch"
        exit 1
    ;;
esac
}

do_stopaudio()
{
    # PulseAudio
    PULSE_INSTALLED=`grep PULSE_SINK $TEMCFG`
    if [ "${PULSE_INSTALLED}" != "" ] ; then
        PULSE_ENABLED=`grep PULSE_ENABLED $TEMCFG`
        if [ "${PULSE_ENABLED}" != "" ] ; then
            grep -v 'PULSE_ENABLED' $TEMCFG > $TEMCFG.tmp
            mv $TEMCFG.tmp $TEMCFG
            chmod 644 $TEMCFG
        fi
        echo "$l_unixaudiostopok_pulse"
    else
        echo "$l_unixaudionotinstalled_pulse"
    fi

case "$SCOARCH" in
    i3li*|ppli*)
        #LINUX
        if [ -f ${TEMDIR}/audio/sgdadem/src/sgdadem.ko ] || [ -f ${TEMDIR}/audio/sgdadem/src/sgdadem.o ]; then
            PATH=/bin:/sbin:/usr/bin
            export PATH

            SGDAUDIOPID=`ps -e|grep sgdaudio|grep -v grep|awk '{print $1}'`
            if [ "$SGDAUDIOPID" != "" ]; then
               echo "$l_unixaudioinuse"
               echo "$l_unixaudiostopfail"
               return 2
            fi

            SGDMODULE=`lsmod |grep sgdadem|awk '{print $1}'`
            if [ "$SGDMODULE" = "" ]; then
                echo "$l_unixaudionotrunning"   
                return 2
            fi
            echo "$l_unixaudiostop"

            modprobe -r sgdadem 
            if [ $? = "0" ]; then
                echo "$l_unixaudiostopok"
                return 0
            else
                echo "$l_unixaudiostopfail"
                return 2
            fi
        else
            echo "$l_unixaudionotinstalled"
            return 1;
        fi
    ;;
    spso*|i3so*)
        #Solaris
        # if PKG_INSTALL_ROOT is not assigned from the env then
        # set it to an empty string, set -u in effect below
        #
        if [ -z "$PKG_INSTALL_ROOT" ]; then
            PKG_INSTALL_ROOT=""
            REM_DRV="/usr/sbin/rem_drv"
        else
            REM_DRV="/usr/sbin/rem_drv -b ${PKG_INSTALL_ROOT}"
        fi
        SGDADEM="sgdadem"
        TARGDIR="$PKG_INSTALL_ROOT/usr/kernel/drv"

        if [ -f "${TARGDIR}/${SGDADEM}" -a -f "${TARGDIR}/${SGDADEM}.conf" ]; then
            SGDAUDIOPID=`ps -e|grep sgdaudio|grep -v grep|awk '{print $1}'`
            if [ "$SGDAUDIOPID" != "" ]; then
                echo "$l_unixaudioinuse"
                echo "$l_unixaudiostopfail"
                return 2
            fi

            SGDMODULE=`/usr/sbin/modinfo -c|grep $SGDADEM|grep 'LOADED/INSTALLED'`
            if [ "$SGDMODULE" = "" ]; then
                echo "$l_unixaudionotrunning"   
                return 2
            fi
            #set +e
            echo "$l_unixaudiostop"
            $REM_DRV $SGDADEM
            #set -e

            if [ $? = "0" ]; then
                echo "$l_unixaudiostopok"
                return 0
            else
                echo "$l_unixaudiostopfailornotinstalled"
                return 1
            fi
        else
            echo "$l_unixaudionotinstalled"
            return 1;
        fi
    ;;
    *)
        echo "$l_badarch"
        exit 1
    ;;
esac
}

# check if a tier3loadprobe binary is already running - if not launch one
do_startloadprobe()
{
  # set the start type for the request script
  SGD_TEM_START_TYPE="startloadprobe" export SGD_TEM_START_TYPE

  # check to see if the server has been configured before running anything
  do_config
  echo "$l_starting_loadprobe"

  PID=`ps -e|grep $a_binaryfile|grep -v grep|awk '{print $1}'`
  if [ "$PID" != "" ]
  then
      echo "$l_notstarting_loadprobe"
  else
      su "${privusr}" -c "$TEMBINDIR/$a_binaryfile $TEMDIR/$a_propsdir $NUM_PROCESSORS &"
      echo "${l_started_pid1}$!${l_started_pid2}"
  fi
}

# check if a tier3loadprobe binary is already running - if so kill it
do_stoploadprobe()
{
  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_loadprobe"
      fi
  else
      PID=`cat $PIDFILE`
      if [ "$PID" != "" ]
      then
	  echo "$l_stopping_loadprobe"
          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_config()
{
    # Check for the configure file, if it exists the server needs
    # to be configured before starting up
    TTA_CONFIGURE=`cat ${TEMDIR}/var/configure 2>/dev/null`
    if [ "${TTA_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}`
        [ "${QUIT_TEMINSTALL}" = "t" ] && exit 1
        sh ${INST_DATADIR}/${PKG}/install/postinstall
        rVal=$?

        # If postinstall returns an error exit now
        [ $rVal -ne 0 ] && exit 1
        rm -rf $REQDIR 2>/dev/null
        echo "" > ${TEMDIR}/var/configure
    fi
}

do_start()
{
    # set the start type for the request script
    SGD_TEM_START_TYPE="start" export SGD_TEM_START_TYPE

    # check to see if the server has been configured before running anything
    do_config

    echo "${l_starting1}${NUM_PROCESSORS}${l_starting2}"

    # start two default modules - load probe & audio
    do_startloadprobe
    do_startaudio
}

do_stop()
{
    # stop two default modules - load probe & audio
    echo "$l_stopping_tem"
    do_stoploadprobe
    do_stopaudio
}

do_startcdm()
{
    # set the start type for the request script
    SGD_TEM_START_TYPE="startcdm" export SGD_TEM_START_TYPE

    # check to see if the server has been configured before running anything
    do_config

    SetupCDM
    [ $? -ne 0 ] && exit 1

    # Make sure the log directory exists.
    if [ ! -d ${TEMDIR}/$a_logdir ]; then
        mkdir ${TEMDIR}/$a_logdir
    fi

    # Check for an existing ttatdm.
    PID=`ps -e|grep $a_ttatdmfile|grep -v grep|grep -v ${a_ttatdmfile}cl |awk '{print $1}'`
    if [ "$PID" != "" ]; then
        echo "${l_tdmalreadyrunning1}$PID${l_tdmalreadyrunning2}"
        exit 1
    fi

    # Start the ttatdm.
    echo "${l_startingtdm}"
    su "${privusr}" -c "$TEMBINDIR/$a_ttatdmfile $TTATDMCONFIG ${TEMDIR}/$a_logdir"
    PID=`ps -e|grep $a_ttatdmfile|grep -v grep|grep -v ${a_ttatdmfile}cl |awk '{print $1}'`
    
    if [ "$PID" != "" ]; then
        echo "${l_started_pid1}$PID${l_started_pid2}"
    else
        echo "${l_starttdm_failed}"
        exit 1
    fi
        
    # General args to mount.
    MOUNT_ARGS=""
    MOUNT_OPTS="port=$PORT"
        
    # Mount the NFS exported directory.
    mount $PLATFORM_MOUNT_ARGS $MOUNT_ARGS -o $PLATFORM_MOUNT_OPTS,$MOUNT_OPTS localhost:$MOUNTPOINT $MOUNTPOINT
    RVAL=$?

    if [ $RVAL -ne 0 ]; then
        echo "${l_mount_failed}$MOUNTPOINT"
        kill $PID
        exit 1
    else
        echo "${l_mount_succeeded}$MOUNTPOINT"
    fi
}

do_stopcdm()
{
    SetupCDM
    [ $? -ne 0 ] && exit 1

    mount | grep localhost:$MOUNTPOINT 2>&1 > /dev/null
    if [ $? -ne 0 ]; then
        echo "$MOUNTPOINT${l_notmounted}"
        exit 1
    fi
    
    umount $MOUNTPOINT
    if [ $? -ne 0 ]; then
        echo "${l_umount_failed}$MOUNTPOINT"
        exit 1
    else
        echo "${l_umount_succeeded}$MOUNTPOINT"
    fi

    PID=`ps -e|grep $a_ttatdmfile|grep -v grep|awk '{print $1}'`
    if [ "$PID" != "" ]; then
        echo "${l_stoppingtdm1}$PID${l_stoppingtdm2}"
        kill $PID
    else
        echo "${l_tdmnotrunning}"
    fi
}

do_version()
{
    if [ -f $VERSIONFILE ]
    then
        cat $VERSIONFILE
    else
        echo "Version unknown"
    fi
}



do_loadprobestatus()
{
    echo "${l_tier3loadprobe}"
    if [ -f ${TEMBINDIR}/$a_binaryfile ] ; then
        echo "${l_installed}"

        case "$SCOARCH" in
            i3li*|ppli*)
                PIDS=`ps -e|grep $a_binaryfile|grep -v grep|wc -l|awk '{print $1}'`
            ;;
            spso*|i3so*)
                PIDS=`ps -ef|grep $a_binaryfile|grep -v grep|wc -l|awk '{print $1}'`
            ;;
            *)
                PIDS=`ps -ef|grep $a_binaryfile|grep -v grep|wc -l|awk '{print $1}'`
            ;;
        esac

        if [ "$PIDS" = "0" ] ; then
            echo "${l_notrunning}"
            return 1
        else
            echo "${l_running}"
            echo "${l_processes}$PIDS"
            return 0
        fi 
    else
        echo "${l_notinstalled}"
        return 1
    fi
}

do_cdmstatus()
{
    echo "${l_cdm}"
    if [ -f ${TEMBINDIR}/$a_ttatdmfile ] && [ -f ${TEMBINDIR}/${a_ttatdmfile}cl ]; then
        echo "${l_installed}"
        PIDS=`ps -e|grep $a_ttatdmfile|grep -v grep|grep -v ${a_ttatdmfile}cl|wc -l|awk '{print $1}'`
        if [ "$PIDS" = "0" ] ; then
            echo "${l_notrunning}"
            return 1
        else
            echo "${l_running}"
            PIDS=`ps -e|grep $a_ttatdmfile|grep -v grep|wc -l|awk '{print $1}'`
            echo "${l_processes}$PIDS"
            return 0
        fi 
    else
        echo "${l_notinstalled}"
    fi
}

do_audiostatus()
{
    # PulseAudio
    echo "${l_unixaudio_pulse}"
    PULSE_INSTALLED=`grep PULSE_SINK $TEMCFG`
    if [ "${PULSE_INSTALLED}" != "" ] ; then
        echo "${l_installed}"
        PULSE_ENABLED=`grep PULSE_ENABLED $TEMCFG`
        if [ "${PULSE_ENABLED}" == "" ] ; then
            echo "${l_notrunning}"
        else
            echo "${l_running}"
            SGDAUDIOPS=`ps -e|grep sgdaudio|grep '\-\-pa'|grep -v grep|wc -l|awk '{print $1}'`
            echo "${l_processes}$SGDAUDIOPS"
        fi
    else
        echo "$l_notinstalled"
    fi

    echo "${l_unixaudio}"
    case "$SCOARCH" in
        i3li*|ppli*)
            # LINUX
            if [ -f ${TEMDIR}/audio/sgdadem/src/sgdadem.ko ] || [ -f ${TEMDIR}/audio/sgdadem/src/sgdadem.o ]; then
                PATH=/bin:/sbin:/usr/bin
                export PATH

                echo "${l_installed}"

                SGDADEMMOD=`lsmod | grep sgdadem`

                if [ "$SGDADEMMOD" = "" ]; then
                    echo "${l_notrunning}"
                    return 1;
                else 
                    echo "${l_running}"
                    SGDAUDIOPS=`ps -ef|grep sgdaudio|grep -v '\-\-pa'|grep -v grep|wc -l|awk '{print $1}'`
                    echo "${l_processes}$SGDAUDIOPS"
                fi
                return 0
            else
                echo "$l_notinstalled"
                return 1
            fi
        ;;
        spso*|i3so*)
            # Solaris
            if [ -z "$PKG_INSTALL_ROOT" ]; then
                PKG_INSTALL_ROOT=""
            fi
            SGDADEM="sgdadem"
            TARGDIR="$PKG_INSTALL_ROOT/usr/kernel/drv"

            if [ -f "${TARGDIR}/${SGDADEM}" -a -f "${TARGDIR}/${SGDADEM}.conf" ]; then
                PATH=/bin:/sbin:/usr/bin
                export PATH

                echo "${l_installed}"

                SGDMODULE=`/usr/sbin/modinfo -c|grep $SGDADEM|grep 'LOADED/INSTALLED'`
                if [ "$SGDMODULE" = "" ]; then
                    echo "${l_notrunning}"
                    return 1;
                else 
                    echo "${l_running}"
                    SGDAUDIOPS=`ps -ef|grep sgdaudio|grep -v '\-\-pa'|grep -v grep|wc -l|awk '{print $1}'`
                    echo "${l_processes}$SGDAUDIOPS"
                fi
                return 0
            else
                echo "$l_notinstalled"
                return 1
            fi
        ;;
        *)
            echo "$l_badarch"
            exit 1
        ;;
    esac

}

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

case $1 in
    start)
        do_start
        exit 0
    ;;

    stop)
        do_stop
        exit 0
    ;;

    startcdm)
        do_startcdm
        exit 0
    ;;

    stopcdm)
        do_stopcdm
        exit 0
    ;;

    startaudio)
        do_startaudio
        exit $?
    ;;

    stopaudio)
        do_stopaudio
        exit $?
    ;;

    startloadprobe)
        do_startloadprobe
        exit 0
    ;;

    stoploadprobe)
        do_stoploadprobe
        exit 0
    ;;

    restart)
        do_stop
        do_start
        exit 0
    ;;

    version)
        do_version
        exit 0
    ;;

    status)
        do_loadprobestatus
        do_cdmstatus
        do_audiostatus
        exit 0
    ;;
    *)
    echo "$0 [start|stop|startcdm|stopcdm|startaudio|stopaudio|startloadprobe|stoploadprobe|restart|version|status]"
    ;;
esac

