#!/bin/sh
# $Id: tarantella.sh,v 1.53 2005/06/20 19:15:56 peterc Exp $
#  Copyright (c) 2003 Tarantella, Inc.
#  This software code contains proprietary information
#  and should be treated as confidential.


# Main tarantella control script - calls subsidiary scripts to carry out
# the required operations.

#------------------------------------------------------------------
# All this stuff is substituted at build time because it is common
#

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


# be paranoid about the current path
if [ -d /bin ]; then
  PATH=/bin:$PATH
fi
if [ -d /sbin ]; then
  PATH=/sbin:$PATH
fi
if [ -d /usr/bin ]; then
  PATH=/usr/bin:$PATH
fi
if [ -d /usr/sbin ]; then
  PATH=/usr/sbin:$PATH
fi
if [ -d /etc ]; then
  PATH=/etc:$PATH
fi
export PATH

N=""
C=""
if echo -n | grep n > /dev/null
then
  C="\c"
else
  N="-n"
fi
case "$MACHINEARCH" in
i3li*|ppli*) N="$N -e" ;;
esac
_ss_headlines="--------------------------------------------------------------------------"

(unalias) 2>/dev/null 1>&2
if [ "$?" -eq 0 ]; then
  unalias ls df ps cp rm mv sed awk grep echo chmod chown chgrp 2>/dev/null 1>&2
  unalias tr uname mkdir cat sort id 2>/dev/null 1>&2
fi

if [ -z "$0" ]; then
  who_i_am=""
else
  who_i_am=`expr "//$0" : '.*/\(.*\)' `
fi



# export variables to scripts if they want to use them

export PRODUCT_VERSION
export PRODUCT_VERSION_FULL
export PRODUCT_FULL_NAME
export PRODUCT_SHORT_NAME
export PRODUCT_VENDOR
export TSP_FULL_NAME
export TSP_DAEMON_NAME

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

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

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

#Put the pkgtools at the head of the path
if [ -d /usr/package/sadm/install/bin ]
then
  PATH=/var/sadm/install/bin:$PATH
fi

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

# Messages for the top level tarantella 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."

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

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

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

# export the relevant environment variables - TTABINDIR and TTADIR

TTABINDIR=`pwd` export TTABINDIR
TTADIR=`dirname $TTABINDIR` export TTADIR
TTASKID="1" export TTASKID
TTASKIDKEYFILE="$TTADIR/var/info/key.file"

cd $TTADIR
if [ $trace ]; then echo "   trace: TTADIR=\"$TTADIR\"  TTABINDIR=\"$TTABINDIR\""; fi

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

# export the architecture code
SCOARCH=`$TTABINDIR/scoarch` export SCOARCH

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

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

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

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

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

if [ ! -d $TTABINDIR/scripts/$TTALANG -o -z "$TTALANG" ]; then
  if [ ! -z "$TTALANG" ]; then
    posslangs=`GetDirs \`ls -d $TTABINDIR/scripts/$TTALANG* 2>/dev/null\``
  fi
  if [ -z "$posslangs" ]; then
    posslangs=`GetDirs \`ls -d $TTABINDIR/scripts/[a-z][a-z]-[a-z][a-z]* 2>/dev/null\``
  fi
  nposslangs=`echo $posslangs | wc -w`
  if [ "$nposslangs" -gt 1 ]; then
    case "$TTALANG" in
      ??* )
	    echo "$lf_nomessages $LANG" #!!!
	    ;;
      "" )
	   echo "$l_langunset"
	   ;;
      * )
	  TTALANG=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 $TTABINDIR/scripts/$output/common -a "$TTALANG" != "en-us" ]; then
	TTALANG=$output
      fi
    done
    echo "$l_usinglang $TTALANG"
  elif [ "$nposslangs" -eq 1 ]; then
    TTALANG=`echo $posslangs | sed -e 's/.*\///'`
  else
    echo "$lf_reinst"
    exit 5
  fi
fi

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

# export TTAMSG as the language specific directory
TTAMSG=$TTABINDIR/scripts/$TTALANG
export TTAMSG TTALANG
if [ $trace ]; then echo "   trace: Language dir = $TTAMSG"; fi

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


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

# Get information about the location of this installation
# (We may inherit these variables)
pkgres=0;
pkginstfile="$TTADIR/etc/pkg/ttainstance"
if [ -z "$TTABASEINSTDIR" -o -z "$TTABASEDATADIR" ]; then
    if [ -f $pkginstfile ]; then

	# read the package instance from a file
	pkginst=`cat $pkginstfile 2>/dev/null`
	pkgres=$?

	if [ $pkgres = 0 ]; then
	    TTABASEINSTDIR=`pkgparam $pkginst INSTDIR 2>/dev/null`
	    pkgres=$?
	fi
	if [ $pkgres = 0 ]; then
	    TTABASEDATADIR=`pkgparam $pkginst DATADIR 2>/dev/null`
	    pkgres=$?
	fi
    fi
fi

# See if this installation has a var directory.
# If so, then that will be the data directory.
if [ -d "$TTADIR/var" ]; then
    TTAINSTDIR=$TTADIR
    TTADATADIR=$TTADIR/var
fi

# Work out the location of the instance
instflag=f
needpkg=f
if [ "$1" = "--inst" ]; then
    # Use a non-default installation
    TTAINSTDIR=$TTABASEDATADIR/inst.$2
    TTADATADIR=$TTAINSTDIR/var
    instflag=t
    needpkg=t
    shift; shift
elif [ "$TTAINSTDIR" = "" -o "$TTADATADIR" = "" ]; then
    # Set these directories to the
    # base directories if they have not been set.
    TTAINSTDIR=$TTABASEINSTDIR
    TTADATADIR=$TTABASEDATADIR
    needpkg=t
fi

# If we need the package to be installed,
# check the pkgres result from earlier.
if [ $needpkg = t -a $pkgres != 0 ]; then
    echo 1>&2 "The tta installation could not be not found.";
    exit 6;
fi

# check that this is a valid instance
if [ ! -d "$TTAINSTDIR" ]; then
    echo 1>&2 "Installation directory '$TTAINSTDIR' not found. ('$TTADIR')";
    exit 7;
elif [ ! -d "$TTADATADIR" ]; then
    echo 1>&2 "Data directory '$TTADATADIR' not found.";
    exit 8;
fi

# override some of the variables set earlier
TTABINDIR=$TTAINSTDIR/bin
TTAMSG=$TTABINDIR/scripts/$TTALANG
TTADIR=$TTAINSTDIR
export TTABASEINSTDIR TTABASEDATADIR TTAINSTDIR TTADATADIR TTABINDIR TTAMSG TTADIR

# run the tarantella script on the other instance if we need to
if [ $instflag = t ]; then
    exec $TTABINDIR/tarantella "$@"
fi

# Put bin directories at begining of path so binaries can be found
PATH=$TTABINDIR:$TTABINDIR/scripts:$TTABINDIR/bin:$PATH
export PATH

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

# generate dynamic usage strings
# what you get for typing tarantella or tarantella --help etc

print_tta_usage ()
{

    echo "" 1>&2
    cwd=`pwd`
    name="$1"

    if [ -d $TTAMSG ]; then
	cd $TTAMSG
	if [ ! -f tta_"$name" -a ! -h tta_"$name" ]
	then
    #        echo "file not FOUND"
	    name="tta"
	fi

	# load all the lf_usage messages from tta_$name eg tta_admin
	# defaults to those in the tarantella file if they don't exist

	if [ -f tta_"$name" ]
	then
	    usagefile=tta_"$name"
	    . ./$usagefile
	fi

	saved_usage="$lf_usage5"

	echo "$l_usage $who_i_am $lf_usage" 1>&2
	echo "$lf_usage2" 1>&2

	# looking for all files beginning with $name

	FoundMsgFile=""
	for msgfile in "$name"_*
	do
            # Ignore all files ending with '.msg', for they are message
            # files for tcl scripts and make no sense in the world of shell
            # Also ignore all files ending in '.xslt', for they are xslt
            # files for parsing XML

            case $msgfile in
            *.msg ) continue;;
            *.xslt ) continue;;
            esac

	    if [ -z "$FoundMsgFile" ]; then
		echo "" 1>&2
		FoundMsgFile="t"
	    fi
	    . ./$msgfile
	    descvar=\$"$msgfile"_desc
	    eval "descvarvalue=$descvar"
	    [ -n "$descvarvalue" ] && echo "$descvarvalue" 1>&2
	done

	if [ -n "$FoundMsgFile" ]; then
	    echo "" 1>&2
	    echo "$lf_usage4$who_i_am$saved_usage" 1>&2
	fi

	cd $cwd
    fi
    echo "" 1>&2
}

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

# Set port numbers. Start by sourcing info/ports.properties.
if [ -r $TTADIR/var/info/ports.properties ]; then
	. $TTADIR/var/info/ports.properties
fi

# Use these as defaults for config
tarantella_config_array_externalport=${externalport:-3144}
tarantella_config_array_port=${peerport:-5427}
tarantella_config_array_secureport=${secureport:-5307}
#also include proxiedhttpsurl for firewall traversal
tarantella_config_server_proxiedhttpsurl=${proxiedhttpsurl:-""}
tarantella_config_array_privilegeduser=${privilegeduser:-""}
tarantella_config_array_privilegedgroup=${privilegedgroup:-""}
#also include privileged username for web authentication
tarantella_config_array_webserveruser=${webserveruser:-""}
# Include web auth key refresh
tarantella_config_login_webauth_refreshkeyonrestart=${refreshkeyonrestart:-""}

#Include attribute to tell ttawebtop whether to be verbose or not
tarantella_config_server_cgiverbose=${cgiverbose:-""}

#Include attribute used to modify the archive names
tarantella_config_server_archivemodifier=${archivemodifier:-""}

#Attribute for cgi-bin to tell client to bootstrap from
tarantella_config_server_cgibin_bootscript=${bootscript:-""}

#Attribute passed to client so it knows the route to the server.
tarantella_config_array_netservice_proxy_routes=${routes:-""}

#Attribute passed to client so it knows the correct external name of the server
tarantella_config_server_netservice_proxy_externalnames=${externalnames:-""}

#SHould we use ssl on peer connections? Admin tools need to know
tarantella_config_security_peerssl_enabled=${peerssl:-"0"}

# Use the bootstrap.properties file if we can.
if [ -r $TTADIR/var/serverconfig/global/bootstrap.properties ]; then
	eval `sed -e '
		s/^#.*//
		:dotloop
		s/^\([^=]*\)\.\([^=]*=.*\)$/\1_\2/
		tdotloop
		s/ /\\\\ /g' $TTADIR/var/serverconfig/global/bootstrap.properties`
fi

# Set the values in the expected variables.
externalport=$tarantella_config_array_externalport
secureport=$tarantella_config_array_secureport
peerport=$tarantella_config_array_port

# Set DNS names. Use the bootstrap.properties file if we can.
if [ -r $TTADIR/var/serverconfig/local/bootstrap.properties ]; then
	eval `sed -e '
		s/^#.*//
		:dotloop
		s/^\([^=]*\)\.\([^=]*=.*\)$/\1_\2/
		tdotloop
		s/ /\\\\ /g' $TTADIR/var/serverconfig/local/bootstrap.properties`
fi

# We need defaults if the properties file is unreadable or corrupt.
peerhost=${tarantella_config_server_dns_peer:-`$TTADIR/bin/bin/ttahostname`}
externalhost="${tarantella_config_server_dns_external:-$peerhost}"
auxhost="$peerhost"

# Get the value of proxiedhttpsurl/cgiverbose/archivemodifier/bootscript from 
# server.properties
if [ -r $TTADIR/var/serverconfig/local/server.properties ]; then
        eval `sed -e '
                s/^#.*//
                :dotloop
                s/^\([^=]*\)\.\([^=]*=.*\)$/\1_\2/
                tdotloop
                s/ /\\\\ /g' $TTADIR/var/serverconfig/local/server.properties`
fi

# Get the properties from array.properties
if [ -r $TTADIR/var/serverconfig/global/array.properties ]; then
        eval `sed -e '
                s/^#.*//
                :dotloop
                s/^\([^=]*\)\.\([^=]*=.*\)$/\1_\2/
                tdotloop
                s/ /\\\\ /g' $TTADIR/var/serverconfig/global/array.properties`
fi

# Get web auth properties
if [ -r $TTADIR/var/serverconfig/global/webauth.properties ]; then
        eval `sed -e '
                s/^#.*//
                :dotloop
                s/^\([^=]*\)\.\([^=]*=.*\)$/\1_\2/
                tdotloop
                s/ /\\\\ /g' $TTADIR/var/serverconfig/global/webauth.properties`
fi

# Get peerssl properties
if [ -r $TTADIR/var/serverconfig/global/security.properties ]; then
        eval `sed -e '
                s/^#.*//
                :dotloop
                s/^\([^=]*\)\.\([^=]*=.*\)$/\1_\2/
                tdotloop
                s/ /\\\\ /g' $TTADIR/var/serverconfig/global/security.properties`
fi

proxiedhttpsurl=$tarantella_config_server_proxiedhttpsurl
privilegeduser=$tarantella_config_array_privilegeduser
privilegedgroup=$tarantella_config_array_privilegedgroup
webauthrefresh=$tarantella_config_login_webauth_refreshkeyonrestart
cgiverbose=$tarantella_config_server_cgiverbose
archivemodifier=$tarantella_config_server_archivemodifier
bootscript=$tarantella_config_server_cgibin_bootscript
routes=$tarantella_config_array_netservice_proxy_routes
externalnames=$tarantella_config_server_netservice_proxy_externalnames
peerssl=$tarantella_config_security_peerssl_enabled
webserveruser=$tarantella_config_array_webserveruser

INSTALLDIR=$TTADIR
export INSTALLDIR


# temporary variable to hold final command and category (base) of command
cmd="$1"
base="$1"

# Setup exceptions for user and permission checking.
# Who can run what depends on whether the server is running or not.

  serverrunning=false
  if [ -f "$TTADATADIR/info/jserver.pid" ]; then
    # may be more than one entry in the jserver pid file
    for pid in `cat $TTADATADIR/info/jserver.pid`; do
      if ps -p $pid >/dev/null 2>&1 ; then
        serverrunning=true
      fi
    done
  fi

if [ "$serverrunning" = "false" ]; then
  # Do the user check
  chkuser="1"
  chkuserexceptions=""

  # Don't do the permission check. Must be correct user, correct permissions isn't good
  # enough.
  chkpermissions="0"

  # Do change the userid to execute subscripts.
  setuser="1"
  setuserexceptions="start stop uninstall webserver print restart setup objectmanager arraymanager"
else
  # Do the user check
  chkuser="1"
  chkuserexceptions="objectmanager arraymanager array"

  # Do the permissions check. Can run commands if you are in privileged permissions
  chkpermissions="1"
  chkpermissionsexceptions="archive security start stop query restart uninstall"

  # Do change the userid to execute subscripts.
  setuser="1"
  setuserexceptions="start stop uninstall webserver print restart setup objectmanager arraymanager"
fi

for exception in $chkuserexceptions; do
  if [ "$base" = "$exception" ]
  then
    chkuser="0"
  fi
done

for exception in $chkpermissionsexceptions; do
  if [ "$base" = "$exception" ]
  then
    chkpermissions="0"
  fi
done

# Make sure we have at least the default for privilegeduser and privileged group 
if [ "$privilegeduser" = "" ]; then
  privilegeduser="ttasys"
fi

if [ "$privilegedgroup" = "" ]; then
  privilegedgroup="ttaserv"
fi 
if [ "$chkuser" = "1" ]; then
found=0
for user in $privilegeduser root; do
  u="`LANG=C LC_ALL=C id | sed 's/.*uid=[0-9][0-9]*(\([^)]*\)).*/\1/'`"
  if [ "$u" = "$user" ]
  then 
    found=1
  else 
  : 
  fi
done

  if [ "$found" = "0" ]; then
    if [ "$chkpermissions" = "1" ]; then
cat $TTADIR/var/info/server.keys > /dev/null 2>&1
found=$?

      if [ "$found" != "0" ]; then
        echo "You don't have permission to run this script."
        exit 15
      fi
    else
      echo "You don't have permission to run this script."
      exit 15
    fi
  fi 
fi 

setuserexceptions="start stop uninstall webserver print restart setup objectmanager arraymanager start_cdm stop_cdm"

for exception in $setuserexceptions; do
  if [ "$base" = "$exception" ]
  then
    setuser="0"
  fi
done

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

## parse the rest of the arguments
# check if there is an argument, or if the user has typed help

if [ -z "$1" -o "$1" = "help" -o "$1" = "--help" ]; then

    # print usage help for all tarantella commands
    print_tta_usage "tta"
    exit 1
fi

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

thecommand=$TTABINDIR/scripts/$1
if [ $trace ]; then echo "    trace: command=$thecommand $@"; fi
# cmd hold the actual name of the command. This maps either to the .sh or .tcl directly

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

# Grub around for scripts thus:
# Command line was:  tarantella <command> <param1> [<other params>...]
#
# First, look for tcl script called:  <command>_<param1>.tsh
# Second, look for tcl script called:  <command>.tsh
# Third, look for shell script called: <command>_<param1>
# Fourth, look for shell script called: <command>
#
# Unconsumed parameters are passed to the script.

soapscript="$thecommand"_"$2".soap
soapscript2="$thecommand".soap
tclscript="$thecommand"_"$2".tsh
tclscript2="$thecommand".tsh
shscript1="$thecommand"_"$2"
shscript2="$thecommand"

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

## if there is a soap script, try it
if [ -f "$soapscript" ]; then
  soapcmd="$cmd"_"$2"
elif [ -f "$soapscript2" ]; then
  soapscript="$soapscript2"
  soapcmd="$cmd"
else
  soapscript=""
fi

if [ ! -z "$soapscript" ]; then
  if [ "$setuser" = "1" ]; then 
    ttasu "$privilegeduser" "$TTABINDIR/scripts/run_soapscript" $base "$soapcmd" "$soapscript" "$@"
    soapexit="$?"
  else 
    "$TTABINDIR/scripts/run_soapscript" $base "$soapcmd" "$soapscript" "$@"
    soapexit="$?"
  fi
  # an exit code of anything other than 1 means don't carry on
  if [ "$soapexit" -ne 1 ]; then
    exit "$soapexit"
  fi
fi


## call prepare_tclscript if we have a tcl script
#  otherwise execute the shell script
if [ -f "$tclscript" ]; then
# call prepare tcl with (base name of command (e.g. admin), merged command and remaining parameters
  cmd="$cmd"_"$2"
  shift
  #echo "$TTABINDIR/scripts/prepare_tclscript" $base $cmd "$@"
  if [ "$setuser" = "1" ]; then 
    ttasu "$privilegeduser" "$TTABINDIR/scripts/prepare_tclscript" $base $cmd "$@"
    exit $?
  else
    exec "$TTABINDIR/scripts/prepare_tclscript" $base $cmd "$@"
  fi
elif [ -f "$tclscript2" ]; then
  if [ "$setuser" = "1" ]; then
    ttasu "$privilegeduser" "$TTABINDIR/scripts/prepare_tclscript" $base $cmd "$@"
    exit $?
  else
    exec "$TTABINDIR/scripts/prepare_tclscript" $base $cmd "$@"
  fi
elif [ -f "$shscript1" ]; then
  shift
  shift
  if [ "$setuser" = "1" ]; then
    ttasu "$privilegeduser" "$shscript1" "$@"
    exit $?
  else
    exec "$shscript1" "$@"
  fi
elif [ -f "$shscript2" ]; then
  shift
  if [ "$setuser" = "1" ]; then
    ttasu "$privilegeduser" "$shscript2" "$@"
    exit $?
  else
    exec "$shscript2" "$@"
  fi 
else
# script was not located so provide help on category of command
    #echo "$ttacommand: $lf_noexe: $1" 1>&2
    #print usage help for a given category of help
    print_tta_usage "$base"
fi

exit 1

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

