#!/bin/sh
#  Copyright (C) 1999-2005 Tarantella, Inc. All Rights Reserved.
#  $Id: sysadmin.sh,v 1.14.8.1 2006/08/31 14:28:47 te201713 Exp $
#
#  Author: Bill Weir
#  Crontab/rc conversion script

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


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

lf_nomessages="No message files installed for LANG="
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"
[ -z "$LANG" ] && LANG="C"

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]-'`
esac

if [ ! -d $TTALANG -o -z "$TTALANG" ]; then
  [ ! -z "$TTALANG" ] && posslangs=`find . -type d -name "$TTALANG*" -print 2>/dev/null | sed 's;.*\/.*\/.*;;'`

  [ -z "$posslangs" ] && posslangs=`find . -type d -name '[a-z][a-z]-[a-z][a-z]*' -print 2>/dev/null | sed 's;.*\/.*\/.*;;'`

  nposslangs=`echo $posslangs | wc -w`
  if [ "$nposslangs" -gt 1 ]; then
    case "$TTALANG" in
      ??* )
            echo "${lf_nomessages}${LANG}" #!!!
	    echo "$lf_langright"
            ;;
      "" )
           echo "$l_langunset"
           ;;
      * )
          TTALANG=en-us
          echo "$l_langunknown $LANG"
          echo "$l_langdefault"
          ;;
    esac
    echo "$l_gottheselangs"
    tmpTTALANG=$TTALANG
    for a_lang in $posslangs; do
      output=`echo $a_lang | sed -e 's/.*\///'`
      echo "  $output"
      if [ -f $output/common -o -f ../$output/common ]; then
	[ "$tmpTTALANG" != "en-us" ] && tmpTTALANG=$output
      fi
    done
    [ "$TTALANG" = "$tmpTTALANG" ] && TTALANG=en-us || TTALANG=$tmpTTALANG	
    echo "$l_usinglang $TTALANG"
  elif [ "$nposslangs" -eq 1 ]; then
    TTALANG=`echo $posslangs | sed -e 's/.*\///'`
  else
    echo "$lf_reinst"
    exit 5
  fi
fi

TTAMSG=`pwd`/$TTALANG
export TTAMSG TTALANG
if [ $trace ]; then echo "   trace: Language dir = $TTAMSG"; fi

if [ -f $TTAMSG/common ]; then
  . $TTAMSG/common
else
  _lang=`basename $TTAMSG`
  _parent=`dirname $TTAMSG`
  _parent=`dirname $_parent`
  if [ -f $_parent/$_lang/common ]; then
    . $_parent/$_lang/common
  else
    echo "Unable to open message file: $TTAMSG/common"
    exit 1
  fi
fi
if [ -f $TTAMSG/setup ]; then
  . $TTAMSG/setup
else
  echo "Unable to open message file: $TTAMSG/setup"
  exit 1
fi

GetPrivUsrGrp()
{
    cat ${instdir}/var/serverconfig/global/array.properties 2>/dev/null | awk -F= \
	'BEGIN { u="ttasys"; wu="ttaserv"; g="ttaserv" }
	$1 == "tarantella.config.array.privilegeduser" { u = $2 }
	$1 == "tarantella.config.array.webserveruser" { wu = $2 }
	$1 == "tarantella.config.array.privilegedgroup" { g = $2 }
	END { print u " " wu " " g }'
}
_privusrgrp=`GetPrivUsrGrp`
privusr=`expr "$_privusrgrp" : '\([^ ]*\) .*'`
webservusr=`expr "$_privusrgrp" : '.* \([^ ]*\) .*'`
privgrp=`expr "$_privusrgrp" : '.* \([^ ]*\)$'`


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

TMP_RC=$INSTDIR/var/tmp/tta$$rc

INST_RCFILE="S90$PRODUCT_SHORT_FILENAME"
INST_RCSTRING="##$PRODUCT_SHORT_FILENAME##"

TWS_INST_RCFILE="S89${PRODUCT_SHORT_FILENAME}WebServer"
TWS_INST_RCSTRING="##${PRODUCT_SHORT_FILENAME}WebServer##"

cleanup()
{
    trap '' 1 2 3 15
    rm -f ${TMP_RC}.full ${TMP_RC}.append >/dev/null 2>&1
}

cleanandexit()
{
    cleanup
    exit $1
}

trap 'cleanup;exit 1' 1 2 3 15

DoCreate()
{
    # Create new installation.
    # add to the crontab if requested
case "$SCOARCH" in
  i3li*|ppli*)
    # Strip out any "DO NOT EDIT THIS FILE" comments to avoid repetition in the crontab file.
    crontab -l 2>/dev/null |
    grep -v "/tarantella archive" |
    awk 'BEGIN {
	DoPrint = 1
    }
    /^#.*DO NOT EDIT/ {
	DoPrint = 0
    }
    /^[^#]/ {
	DoPrint = 1
    }
    DoPrint {
	print
    }
    /^#.*[cC]ron version/ {
	DoPrint = 1
    }' |
    crontab - 2>/dev/null
    ;;
  *)
    crontab -l 2>/dev/null | grep -v "/tarantella archive" | crontab 2>/dev/null
esac
if [ "`crontab -l 2>/dev/null | grep -v \"/tarantella archive\"`" = "" ]; then
  crontab -r
fi

    if [ "$a_Archive" = "yes" ]; then
	if [ -n "${privusr}" ]; then
	    c_croncomm="/bin/su ${privusr} -c \"${instdir}/bin/tarantella archive\" >/dev/null 2>/dev/console"
	else
	    c_croncomm="${instdir}/bin/tarantella archive >/dev/null 2>/dev/console"
	fi
# sunday at xxx o'clock
case "$SCOARCH" in
  i3li*|ppli*)
    # Strip out any "DO NOT EDIT THIS FILE" comments to avoid repetition in the crontab file.
    ( crontab -l 2>/dev/null; echo "0 $a_CronTime * * $a_CronDay $c_croncomm" ) |
    awk 'BEGIN {
	DoPrint = 1
    }
    /^#.*DO NOT EDIT/ {
	DoPrint = 0
    }
    /^[^#]/ {
	DoPrint = 1
    }
    DoPrint {
	print
    }
    /^#.*[cC]ron version/ {
	DoPrint = 1
    }' |
    crontab - 2>/dev/null
    ;;
  *)
    ( crontab -l 2>/dev/null; echo "0 $a_CronTime * * $a_CronDay $c_croncomm" ) | crontab 2>/dev/null
esac

    fi
    
    # add to the rc after removing existing setup
# set the defaults
_delete=false _files="_DeleteMe_"
_filename="$INST_RCFILE"
_fNam=`expr "${_filename}" : 'S[0-9][0-9]*\(.*\)'`	# eg Tarantella
# sanity check
[ -z "${_filename}" -o -z "${_fNam}" ] && _filename="_DeleteMe_" _fNam="_DeleteMe_"

# create the list of files to check
case "$a_RcType" in
svr4*|sbin|rcddir*)
  _delete=true
  case "$a_RcType" in
  rcddir*) _rcbase="/etc/rc.d/rc";;
  svr4*)   _rcbase="/etc/rc";;
  sbin)	   _rcbase="/sbin/rc";;
  esac
  for _i in S 0 1 2 3 4 5 6; do
    _files="${_files} `ls -d ${_rcbase}${_i}.d/${_filename} ${_rcbase}${_i}.d/K[0-9]*${_fNam} 2>/dev/null`"
  done
  case "$a_RcType" in
  *-initd) _files="${_files} `ls -d /etc/init.d/${_fNam} 2>/dev/null`"
  esac
  ;;
"local")
  _files="${_files} /etc/rc.local"
  ;;
"rcd")
  _files="${_files} /etc/rc.d/rc.local"
  ;;
"nfs")
  _files="${_files} /etc/rc.nfs"
  ;;
"basic")
  _files="${_files} /etc/rc"
  ;;
esac

# either delete the files or substitute them depending on rc type
if [ "${_delete}" = "true" ]; then
  for _i in ${_files}; do
    # sanity check: file name must end with ${_fNam}
    awk 'BEGIN {
      sfx = "'"${_fNam}"'"; cand = "'"${_i}"'"; file2del="_DeleteMe_"
      if (length(cand) > length(sfx) && substr(cand,length(cand)-length(sfx)+1,length(sfx)) == sfx) file2del = cand
      print file2del
      exit
    }' | xargs rm -f 
  done
else
  for _i in ${_files}; do
    [ "${_i}" = "_DeleteMe_" ] && continue

    # delete everything between the start and stop markers
    echo "BEGIN {" > $_i.awk
    echo "  doprint=1;" >> $_i.awk
    echo "}" >> $_i.awk
    echo "\$1 == \"$INST_RCSTRING\" {" >> $_i.awk
    echo "  if (\$2 == \"start\") {" >> $_i.awk
    echo "    doprint = 0;" >> $_i.awk
    echo "  } else {" >> $_i.awk
    echo "    doprint = 1;" >> $_i.awk
    echo "    continue;" >> $_i.awk
    echo "  }" >> $_i.awk
    echo "}" >> $_i.awk
    echo "{" >> $_i.awk
    echo "  if (doprint == 1) {" >> $_i.awk
    echo "    print" >> $_i.awk
    echo "  }" >> $_i.awk
    echo "}" >> $_i.awk

    # run the newly created awk script on the file
    if [ -f $_i.awk ]; then
      cp -p $_i $_i.tmp1 #for perms
      awk -f $_i.awk $_i > $_i.tmp1
      rm -f $_i.awk
    fi

    # move the new copy over the old
    if [ -f $_i.tmp1 ]; then
      cp -p $_i.tmp1 $_i
      rm -f $_i.tmp1
    fi
  done
fi

# set the defaults
_delete=false _files="_DeleteMe_"
_filename="$TWS_INST_RCFILE"
_fNam=`expr "${_filename}" : 'S[0-9][0-9]*\(.*\)'`	# eg Tarantella
# sanity check
[ -z "${_filename}" -o -z "${_fNam}" ] && _filename="_DeleteMe_" _fNam="_DeleteMe_"

# create the list of files to check
case "$a_RcType" in
svr4*|sbin|rcddir*)
  _delete=true
  case "$a_RcType" in
  rcddir*) _rcbase="/etc/rc.d/rc";;
  svr4*)   _rcbase="/etc/rc";;
  sbin)	   _rcbase="/sbin/rc";;
  esac
  for _i in S 0 1 2 3 4 5 6; do
    _files="${_files} `ls -d ${_rcbase}${_i}.d/${_filename} ${_rcbase}${_i}.d/K[0-9]*${_fNam} 2>/dev/null`"
  done
  case "$a_RcType" in
  *-initd) _files="${_files} `ls -d /etc/init.d/${_fNam} 2>/dev/null`"
  esac
  ;;
"local")
  _files="${_files} /etc/rc.local"
  ;;
"rcd")
  _files="${_files} /etc/rc.d/rc.local"
  ;;
"nfs")
  _files="${_files} /etc/rc.nfs"
  ;;
"basic")
  _files="${_files} /etc/rc"
  ;;
esac

# either delete the files or substitute them depending on rc type
if [ "${_delete}" = "true" ]; then
  for _i in ${_files}; do
    # sanity check: file name must end with ${_fNam}
    awk 'BEGIN {
      sfx = "'"${_fNam}"'"; cand = "'"${_i}"'"; file2del="_DeleteMe_"
      if (length(cand) > length(sfx) && substr(cand,length(cand)-length(sfx)+1,length(sfx)) == sfx) file2del = cand
      print file2del
      exit
    }' | xargs rm -f 
  done
else
  for _i in ${_files}; do
    [ "${_i}" = "_DeleteMe_" ] && continue

    # delete everything between the start and stop markers
    echo "BEGIN {" > $_i.awk
    echo "  doprint=1;" >> $_i.awk
    echo "}" >> $_i.awk
    echo "\$1 == \"$TWS_INST_RCSTRING\" {" >> $_i.awk
    echo "  if (\$2 == \"start\") {" >> $_i.awk
    echo "    doprint = 0;" >> $_i.awk
    echo "  } else {" >> $_i.awk
    echo "    doprint = 1;" >> $_i.awk
    echo "    continue;" >> $_i.awk
    echo "  }" >> $_i.awk
    echo "}" >> $_i.awk
    echo "{" >> $_i.awk
    echo "  if (doprint == 1) {" >> $_i.awk
    echo "    print" >> $_i.awk
    echo "  }" >> $_i.awk
    echo "}" >> $_i.awk

    # run the newly created awk script on the file
    if [ -f $_i.awk ]; then
      cp -p $_i $_i.tmp1 #for perms
      awk -f $_i.awk $_i > $_i.tmp1
      rm -f $_i.awk
    fi

    # move the new copy over the old
    if [ -f $_i.tmp1 ]; then
      cp -p $_i.tmp1 $_i
      rm -f $_i.tmp1
    fi
  done
fi

    if [ "$a_Restart" = "yes" -a "$a_RcType" != "unknown" ]
    then
    	sed "s?@@TTA_DIR@@?${instdir}?g
    	s?@@TTA_SCRIPT@@?bin/tarantella?g" ${INSTDIR}/etc/data/$INST_RCFILE > ${TMP_RC}.full
    	sed "s?@@TTA_DIR@@?${instdir}?g
    	s?@@TTA_SCRIPT@@?bin/tarantella?g" ${INSTDIR}/etc/data/$INST_RCFILE > ${TMP_RC}.append
_rcfile="$a_RcFile|K10" _KN="" _initd="" _links=""
# K prefix may have been passed in at the end of $a_RcType
case "${_rcfile}" in
*\|K*)  _KN=`expr "${_rcfile}" : '.*|K\([0-9][0-9]*\).*'`
	_rcfile=`expr "${_rcfile}" : '\(.*\)|.*'`
esac
# ... otherwise, default K prefix is 10
case "${_KN}" in [0-9][0-9]*) ;; *) _KN="10" ;; esac
_rcdir=`dirname "${_rcfile}"`
_f=`expr "${_rcfile}" : '.*/\(S[0-9][0-9]*[^/]*\)$'`	# eg S90Tarantella
_SN=`expr "${_f}" : 'S\([0-9][0-9]*\).*'`		# eg 90
_fNam=`expr "${_f}" : 'S[0-9][0-9]*\(.*\)'`		# eg Tarantella
# sanity check
[ -z "${_f}" -o -z "${_fNam}" ] && _f="_DeleteMe_" _fNam="_DeleteMe_"
_chkconfig="" _n=2
# build chkconfig line for Linux
while [ "${_n}" -le $a_RunLevel ]; do
  _chkconfig="${_chkconfig}${_n}"
  _n=`expr "${_n}" + 1`
done
_chkconfig="${_chkconfig} ${_SN} ${_KN}"		# eg 2345 90 10
_abs_rcfile="${_rcfile}"; _rel_rcfile="${_abs_rcfile}"
case "$a_RcType" in
*-initd) # on this platform rc files are symlinks to real file in /etc/init.d
  _initd="t" _abs_rcfile="/etc/init.d/${_fNam}"; _rel_rcfile="${_abs_rcfile}"
  # compute relative pathname for symlinks
  case "${_rcdir}" in
  /etc/*)
    _dir="${_rcdir}" _rel_rcfile="init.d/${_fNam}"
    while [ "${_dir}" != "/etc" -a "${_dir}" != "/" ]; do
      _dir=`dirname "${_dir}"` _rel_rcfile="../${_rel_rcfile}"
    done
  esac
esac
case "$a_RcType" in
svr4*|sbin|rcddir*)
  # check the directory exists, doesn't get here unless they've already been
  # asked if they want it created
  [ ! -d "${_rcdir}" ] && mkdir "${_rcdir}"
  # insert chkconfig line
  sed "s/@@CHKCONFIG@@/${_chkconfig}/" ${TMP_RC}.full > "${_rcfile}.tmp1"
  chmod a+x "${_rcfile}.tmp1"
  for _n in S 0 1 2 3 4 5 6; do
    _rcdirN=`echo "${_rcdir}" | sed "s;$a_RunLevel;${_n};"`
    [ -d "${_rcdirN}" ] || continue
    case "${_n}" in
    S) _links="${_links} ${_rcdirN}/K${_KN}${_fNam}"
      ( cd "${_rcdirN}"; ln -s "${_rel_rcfile}" "K${_KN}${_fNam}" 2>/dev/null )
      ;;
    *) if [ "${_n}" -lt 2 -o "${_n}" -eq 6 ]; then
	_links="${_links} ${_rcdirN}/K${_KN}${_fNam}"
	( cd "${_rcdirN}"; ln -s "${_rel_rcfile}" "K${_KN}${_fNam}" 2>/dev/null )
      elif [ "${_n}" -lt $a_RunLevel -o \( "${_n}" -eq $a_RunLevel -a -n "${_initd}" \) ]; then
	_links="${_links} ${_rcdirN}/${_f}"
	( cd "${_rcdirN}"; ln -s "${_rel_rcfile}" "${_f}" 2>/dev/null )
      fi
    esac
  done
  ;;
"local"|"nfs"|"basic")
  cp -p "${_rcfile}" "${_rcfile}.tmp1"
  echo "$INST_RCSTRING start" >> "${_rcfile}.tmp1"
  cat ${TMP_RC}.append >> "${_rcfile}.tmp1"
  echo "$INST_RCSTRING stop" >> "${_rcfile}.tmp1"
  ;;
esac

if [ -f "${_rcfile}.tmp1" ]; then
  awk 'BEGIN {
    sfx = "'"${_fNam}"'"; cand = "'"${_abs_rcfile}"'"; file2del="_DeleteMe_"
    if (length(cand) > length(sfx) && substr(cand,length(cand)-length(sfx)+1,length(sfx)) == sfx) file2del = cand
    print file2del
    exit
  }' | xargs rm -f 
  [ ! -f "${_abs_rcfile}" ] && mv "${_rcfile}.tmp1" "${_abs_rcfile}"
  [ -f "${_rcfile}.tmp1" ] && rm -f "${_rcfile}.tmp1"
fi

case "$a_RcType" in
svr4*|sbin) # replace symlinks with hard links on these platforms
  for _i in ${_links}; do
    awk 'BEGIN {
      sfx = "'"${_fNam}"'"; cand = "'"${_i}"'"; file2del="_DeleteMe_"
      if (length(cand) > length(sfx) && substr(cand,length(cand)-length(sfx)+1,length(sfx)) == sfx) file2del = cand
      print file2del
      exit
    }' | xargs rm -f 
    [ ! -f "${_i}" ] && ln "${_abs_rcfile}" "${_i}" 2>/dev/null
  done
esac


        # and again for the TWS
        TWS_RcFile=`dirname $a_RcFile`/$TWS_INST_RCFILE
    	sed "s?@@TTA_DIR@@?${instdir}?g
    	s?@@TTA_SCRIPT@@?bin/tarantella?g" ${INSTDIR}/etc/data/$TWS_INST_RCFILE > ${TMP_RC}.full
    	sed "s?@@TTA_DIR@@?${instdir}?g
    	s?@@TTA_SCRIPT@@?bin/tarantella?g" ${INSTDIR}/etc/data/$TWS_INST_RCFILE > ${TMP_RC}.append
_rcfile="$TWS_RcFile|K10" _KN="" _initd="" _links=""
# K prefix may have been passed in at the end of $a_RcType
case "${_rcfile}" in
*\|K*)  _KN=`expr "${_rcfile}" : '.*|K\([0-9][0-9]*\).*'`
	_rcfile=`expr "${_rcfile}" : '\(.*\)|.*'`
esac
# ... otherwise, default K prefix is 10
case "${_KN}" in [0-9][0-9]*) ;; *) _KN="10" ;; esac
_rcdir=`dirname "${_rcfile}"`
_f=`expr "${_rcfile}" : '.*/\(S[0-9][0-9]*[^/]*\)$'`	# eg S90Tarantella
_SN=`expr "${_f}" : 'S\([0-9][0-9]*\).*'`		# eg 90
_fNam=`expr "${_f}" : 'S[0-9][0-9]*\(.*\)'`		# eg Tarantella
# sanity check
[ -z "${_f}" -o -z "${_fNam}" ] && _f="_DeleteMe_" _fNam="_DeleteMe_"
_chkconfig="" _n=2
# build chkconfig line for Linux
while [ "${_n}" -le $a_RunLevel ]; do
  _chkconfig="${_chkconfig}${_n}"
  _n=`expr "${_n}" + 1`
done
_chkconfig="${_chkconfig} ${_SN} ${_KN}"		# eg 2345 90 10
_abs_rcfile="${_rcfile}"; _rel_rcfile="${_abs_rcfile}"
case "$a_RcType" in
*-initd) # on this platform rc files are symlinks to real file in /etc/init.d
  _initd="t" _abs_rcfile="/etc/init.d/${_fNam}"; _rel_rcfile="${_abs_rcfile}"
  # compute relative pathname for symlinks
  case "${_rcdir}" in
  /etc/*)
    _dir="${_rcdir}" _rel_rcfile="init.d/${_fNam}"
    while [ "${_dir}" != "/etc" -a "${_dir}" != "/" ]; do
      _dir=`dirname "${_dir}"` _rel_rcfile="../${_rel_rcfile}"
    done
  esac
esac
case "$a_RcType" in
svr4*|sbin|rcddir*)
  # check the directory exists, doesn't get here unless they've already been
  # asked if they want it created
  [ ! -d "${_rcdir}" ] && mkdir "${_rcdir}"
  # insert chkconfig line
  sed "s/@@CHKCONFIG@@/${_chkconfig}/" ${TMP_RC}.full > "${_rcfile}.tmp1"
  chmod a+x "${_rcfile}.tmp1"
  for _n in S 0 1 2 3 4 5 6; do
    _rcdirN=`echo "${_rcdir}" | sed "s;$a_RunLevel;${_n};"`
    [ -d "${_rcdirN}" ] || continue
    case "${_n}" in
    S) _links="${_links} ${_rcdirN}/K${_KN}${_fNam}"
      ( cd "${_rcdirN}"; ln -s "${_rel_rcfile}" "K${_KN}${_fNam}" 2>/dev/null )
      ;;
    *) if [ "${_n}" -lt 2 -o "${_n}" -eq 6 ]; then
	_links="${_links} ${_rcdirN}/K${_KN}${_fNam}"
	( cd "${_rcdirN}"; ln -s "${_rel_rcfile}" "K${_KN}${_fNam}" 2>/dev/null )
      elif [ "${_n}" -lt $a_RunLevel -o \( "${_n}" -eq $a_RunLevel -a -n "${_initd}" \) ]; then
	_links="${_links} ${_rcdirN}/${_f}"
	( cd "${_rcdirN}"; ln -s "${_rel_rcfile}" "${_f}" 2>/dev/null )
      fi
    esac
  done
  ;;
"local"|"nfs"|"basic")
  cp -p "${_rcfile}" "${_rcfile}.tmp1"
  echo "$TWS_INST_RCSTRING start" >> "${_rcfile}.tmp1"
  cat ${TMP_RC}.append >> "${_rcfile}.tmp1"
  echo "$TWS_INST_RCSTRING stop" >> "${_rcfile}.tmp1"
  ;;
esac

if [ -f "${_rcfile}.tmp1" ]; then
  awk 'BEGIN {
    sfx = "'"${_fNam}"'"; cand = "'"${_abs_rcfile}"'"; file2del="_DeleteMe_"
    if (length(cand) > length(sfx) && substr(cand,length(cand)-length(sfx)+1,length(sfx)) == sfx) file2del = cand
    print file2del
    exit
  }' | xargs rm -f 
  [ ! -f "${_abs_rcfile}" ] && mv "${_rcfile}.tmp1" "${_abs_rcfile}"
  [ -f "${_rcfile}.tmp1" ] && rm -f "${_rcfile}.tmp1"
fi

case "$a_RcType" in
svr4*|sbin) # replace symlinks with hard links on these platforms
  for _i in ${_links}; do
    awk 'BEGIN {
      sfx = "'"${_fNam}"'"; cand = "'"${_i}"'"; file2del="_DeleteMe_"
      if (length(cand) > length(sfx) && substr(cand,length(cand)-length(sfx)+1,length(sfx)) == sfx) file2del = cand
      print file2del
      exit
    }' | xargs rm -f 
    [ ! -f "${_i}" ] && ln "${_abs_rcfile}" "${_i}" 2>/dev/null
  done
esac

    fi

    # PAM configuration ##################################################
    case "${SCOARCH}" in
    i3li*)	# Linux
	PAMDIR=/etc/pam.d
	PAMFILE_SGD=${PAMDIR}/tarantella
	PAMFILE_DEFAULT=${PAMDIR}/passwd
	if [ -f "${PAMFILE_SGD}" ]; then
	    # PAM file already installed
	    :
	else
	    # copy a default file
	    if [ -f "${PAMFILE_DEFAULT}" ]; then
		cp -p "${PAMFILE_DEFAULT}" "${PAMFILE_SGD}" 2>/dev/null
		touch "${PAMFILE_SGD}" 2>/dev/null
	    fi
	fi
	;;
    spso*)	# SPARC Solaris
	;;
    i3so*)	# x86 Solaris
	;;
    esac
    ######################################################################

    return 0
}

DoUpgrade()
{
    # Nothing to upgrade: just create a new installation.
    DoCreate
    return $?
}

if [ -z "$oldinstdir" ]; then
    DoCreate
    exitCode=$?
else
    DoUpgrade
    exitCode=$?
fi

cleanandexit $exitCode
