#!/bin/sh
#  $Id: run_tclscript.sh 109677 2015-03-31 12:24:31Z pc161604 $
#  Copyright (c) 1998, 2019, Oracle and/or its affiliates. All rights reserved.
#  This software code contains proprietary information
#  and should be treated as confidential.
#
#  Component: prepare_script.sh
#  Author: barrys
#
#  script sets up all parameters

. "common-env.sh"

case "$trace" in
x) set -x
esac

# cd to the directory containing this script, so that we can use
# relative pathnames
ttacommand=$0
case "$ttacommand" in
/*) cd `dirname $ttacommand`
    ;;
*)  fullpath=`pwd`/$ttacommand
    cd `dirname $fullpath`
esac


# Where are we relative to top of tta2 tree?
top=.
for _i in 0 1 2 3 4 5 6 7 8 9; do
    [ -d $top/bin/java -a -x $top/bin/tarantella ] && break
    [ -f $top/etc/passwd ] && { top=.; break; }
    top=../$top
done


LD_LIBRARY_PATH="${TTAINSTDIR}/lib:${TTAINSTDIR}/lib64"
export LD_LIBRARY_PATH

# Parse and consume the command line arguments destined for us:
script=$TTABINDIR/scripts/$1.tsh
shift

while [ -n "$1" ]; do
    case $1 in
        --peer-port) peerport=$2; shift; shift;;
	--external-port ) externalport=$2; shift; shift;;
	--secure-port ) secureport=$2; shift; shift;;
	--log ) logfile=$2; shift; shift;;
	--help )
	    if [ -n "$script" ]; then break; fi
	    echo "$l_usage $2 $who_i_am"; echo "$lf_usage" ; exit ;;
	# Other args are passed through to the tcl script
	*) break ;;
    esac
done

# Source the Java development environment variables:

# Set port numbers. Start by sourcing info/ports.properties.
if [ -r $top/var/info/ports.properties ]; then
	. $top/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.
_propfile="$top/var/serverconfig/global/bootstrap.properties"
[ -s "${_propfile}" -a -r "${_propfile}" ] && {
eval `tr -d '\\\\' <"${_propfile}" | egrep -v "^[ 	]*#|'|^tarantella\.config\.(name|type)=" | sed "/=/!d
:dotloop
s/^\([^=]*\)\.\([^=]*=.*\)\$/\1_\2/
tdotloop
s/^\([^=]*\)=\(.*\)/\1='\2'/"`
}

# 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.
_propfile="$top/var/serverconfig/local/bootstrap.properties"
[ -s "${_propfile}" -a -r "${_propfile}" ] && {
eval `tr -d '\\\\' <"${_propfile}" | egrep -v "^[ 	]*#|'|^tarantella\.config\.(name|type)=" | sed "/=/!d
:dotloop
s/^\([^=]*\)\.\([^=]*=.*\)\$/\1_\2/
tdotloop
s/^\([^=]*\)=\(.*\)/\1='\2'/"`
}

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

# Get the value of proxiedhttpsurl/cgiverbose/archivemodifier/bootscript from 
# server.properties
_propfile="$top/var/serverconfig/local/server.properties"
[ -s "${_propfile}" -a -r "${_propfile}" ] && {
eval `tr -d '\\\\' <"${_propfile}" | egrep -v "^[ 	]*#|'|^tarantella\.config\.(name|type)=" | sed "/=/!d
:dotloop
s/^\([^=]*\)\.\([^=]*=.*\)\$/\1_\2/
tdotloop
s/^\([^=]*\)=\(.*\)/\1='\2'/"`
}

# Get the properties from array.properties
_propfile="$top/var/serverconfig/global/array.properties"
[ -s "${_propfile}" -a -r "${_propfile}" ] && {
eval `tr -d '\\\\' <"${_propfile}" | egrep -v "^[ 	]*#|'|^tarantella\.config\.(name|type)=" | sed "/=/!d
:dotloop
s/^\([^=]*\)\.\([^=]*=.*\)\$/\1_\2/
tdotloop
s/^\([^=]*\)=\(.*\)/\1='\2'/"`
}

# Get web auth properties
_propfile="$top/var/serverconfig/global/webauth.properties"
[ -s "${_propfile}" -a -r "${_propfile}" ] && {
eval `tr -d '\\\\' <"${_propfile}" | egrep -v "^[ 	]*#|'|^tarantella\.config\.(name|type)=" | sed "/=/!d
:dotloop
s/^\([^=]*\)\.\([^=]*=.*\)\$/\1_\2/
tdotloop
s/^\([^=]*\)=\(.*\)/\1='\2'/"`
}

# Get peerssl properties
_propfile="$top/var/serverconfig/global/security.properties"
[ -s "${_propfile}" -a -r "${_propfile}" ] && {
eval `tr -d '\\\\' <"${_propfile}" | egrep -v "^[ 	]*#|'|^tarantella\.config\.(name|type)=" | sed "/=/!d
:dotloop
s/^\([^=]*\)\.\([^=]*=.*\)\$/\1_\2/
tdotloop
s/^\([^=]*\)=\(.*\)/\1='\2'/"`
}

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
peersslciphers=$tarantella_config_security_peerssl_ciphers
webserveruser=$tarantella_config_array_webserveruser

INSTALLDIR=$top
export INSTALLDIR

[ -f $top/bin/scripts/javaprops ] && . $top/bin/scripts/javaprops
JAVADIR=${INSTALLDIR}/bin/java
CLASSPATH=${JAVADIR}/jserver.jar:${JAVADIR}/lib/*:${JAVADIR}:${JAVADIR}/ext/*:${INSTALLDIR}/var/serverresources/adminconfig

# (This sets the $jrecommand to be an appropriate jre.)

# Make script name an absolute path (TTACWD comes from the main script)
searchpath="$TTACWD $TTABINDIR/scripts"
case "$script" in
    /* ) ;;
    - ) ;;
    "" ) ;;
    *  )
	for d in $searchpath; do
	    if [ -r $d/$script ]; then
		script=$d/$script
		break
	    fi
	done
	;;
esac

# Run the script

JAVA_SYSTEM_PROPERTIES="${JAVA_SYSTEM_PROPERTIES}
-Dcom.sco.tta.admin.authentication.prompt=sometimes
-Djava.naming.tfn.flavour=autoAdmin
-Dtarantella.config.security.peerssl.enabled=$peerssl
-Dtarantella.config.security.peerssl.ciphers=$peersslciphers
"

# Find out if the session token has been set...
if [ -n "$TTA_SESSION_TOKEN" ]; then
    # Check to make sure it's a file and not the actual token...
    if [ ! -r "$TTA_SESSION_TOKEN" ]; then
        # The file doesn't exist so write $TTA_SESSION_TOKEN to a tmp file...
        TOKENFILE=${TMPDIR:-/tmp}/TTA_SESSION_TOKEN.$$

        # Check to see if the file exists by accident...
        if [ -w $TOKENFILE ]; then
            rm -f $TOKENFILE
        fi

        # Write the token to a new file...
        (umask 177; echo "$TTA_SESSION_TOKEN" > $TOKENFILE)

        # Switch the token var to point to the file...
        TTA_SESSION_TOKEN=$TOKENFILE
    fi

    # Do some stuff for the TCL bit...
    TCLTOKENARG="SESSIONTOKENFILE=$TTA_SESSION_TOKEN"
else
    # Do some stuff for the TCL bit...
    TCLTOKENARG="SESSIONTOKENFILE="
fi


if [ -r "$script" ]; then
    cd $TTABINDIR/scripts
    # run the jacl tcl interpreter with correct script file and parameters
#@@@@    [ -d "$JREDIR" ] && JRE=$JREDIR/jre
    exec ${SGD_JAVA_DIR}/java $TTAJIT $RUNTIME_ARGS -Djava.library.path=${TTAINSTDIR}/lib -classpath $CLASSPATH $RUNTIME_ARGS $JAVA_SYSTEM_PROPERTIES $TTA_USER_CMD com.sco.tta.common.util.TarantellaApplication tcl.lang.Shell $script "$@" "--ttamsg" "$TTAMSG" "--ttabindir" "$TTABINDIR" "--datadir" "${TTADATADIR}" "--currentdir" "$TTACWD" "--ttainstdir" "$TTAINSTDIR" "--cmdlineargs" $TCLTOKENARG "CONTEXTSTART=.../_ens" "INSTALLDIR=${TTAINSTDIR}" "PEERHOST=${peerhost}" "EXTERNALHOST=${externalhost}" "EXTERNALPORT=${externalport}" "SECUREPORT=${secureport}" "PEERPORT=${peerport}"

elif [ "$script" = "-" ]; then
    cd $TTABINDIR/scripts
    exec ./adminshell "$@"
elif [ -n "$script" ]; then
    echo 1>&2 "${l_errprefix}${l_filenotfound}${script}"
    # Will try to remove the token file if it is allowed to.
rm -f $TOKENFILE

    exit 7
fi
