#!/bin/sh
#  $Id: preremove.sh 144933 2017-02-08 09:50:25Z justin.carveley@oracle.com $
#  Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.
#
#  Component: Secure Global Desktop Gateway
#  Author: Bill Weir

if [ -f /tmp/pkgadd.debug ]; then
    echo "Debug Output On"
    echo "---------------"
    echo " "
    set
    set -x	
    trace=x export trace
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$
# 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.60.514"
PRODUCT_VERSION_FULL="5.60.514"
PRODUCT_VERSION_MAJOR=5
PRODUCT_VERSION_FEATURE=6
PRODUCT_VERSION_BUG=0
PRODUCT_VERSION_BUILD=514
PRODUCT_VERSION_BUILD_DECIMAL=514
PRODUCT_VERSION_AS_NUMBER=560514
PRODUCT_VERSION_CAB=5,6,0,514
PRODUCT_VERSION_RPM=5.60-514

# 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.9



[ -z "${INSTDIR}" ] && exit 1

msgfile="${INSTDIR}/etc/pkg/gateway/install/en-us_setup"
[ -s "${msgfile}" ] && . "${msgfile}"

${INSTDIR}/bin/gateway stop --force >/dev/null 2>&1

LSB_INST_RCFILE="S91${GWCOMPONENT_LSBSCRIPTNAME}"
_dummy_="" a_RcCurRun="" a_RcInstRun="" a_RcKillRun="" a_RcType="unknown" a_RcFile=""
if [ -d /etc/rc.d/rc2.d -o -d /etc/rc.d/rc3.d ]; then
  a_RcType="rcddir" a_RcFile="/etc/rc.d" _dummy_="/etc/rc.d" _rcbase="/etc/rc.d/rc"
  [ -d /etc/init.d ] && a_RcType="$a_RcType-initd"
elif [ -d /etc/rc2.d -o -d /etc/rc3.d ]; then
  a_RcType="svr4" a_RcFile="/etc" _dummy_="/etc" _rcbase="/etc/rc"
  [ -d /etc/init.d ] && a_RcType="$a_RcType-initd"
elif [ -d /sbin/rc2.d -o -d /sbin/rc3.d ]; then
  a_RcType="sbin" a_RcFile="/sbin" _dummy_="/sbin" _rcbase="/sbin/rc"
elif [ -f /etc/rc.local ]; then
  a_RcType="local" a_RcFile="/etc/rc.local"
elif [ -f /etc/rc.d/rc.local ]; then
  a_RcType="rcd" a_RcFile="/etc/rc.d/rc.local"
elif [ -f /etc/rc.nfs ]; then
  a_RcType="nfs" a_RcFile="/etc/rc.nfs"
elif [ -f /etc/rc ]; then
  a_RcType="basic" a_RcFile="/etc/rc"
fi
case "$a_RcType" in
svr4*|sbin|rcddir*)
  a_RcCurRun=`who -r 2>/dev/null | awk '{print $3;}'`
  [ ! -z "$a_RcCurRun" ] && {
    case $a_RcCurRun in [0-9]);; *) a_RcCurRun=`who -r 2>/dev/null | awk '{print $2;}'`;; esac;
  } || a_RcCurRun=`/sbin/runlevel 2>/dev/null | awk '{print $2}'`;
  [ -z "$a_RcCurRun" ] && a_RcCurRun=2 # guess
  a_RcKillRun=1
  for _i in 5 4 3 2 1 S; do
    [ -r ${_rcbase}${_i}.d/$LSB_INST_RCFILE ] && { a_RcInstRun=${_i}; break; }
  done
  _dummy_=$a_RcFile/rc$a_RcKillRun.d/_dummy_
  a_RcFile=$a_RcFile/rc$a_RcCurRun.d/$LSB_INST_RCFILE
esac

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

[ -s "/etc/init.d/${_fNam}" ] && {
    _rclinks='t'
    [ -x "/usr/lib/lsb/remove_initd" ] && {
	/usr/lib/lsb/remove_initd "/etc/init.d/${_fNam}"
	[ -z "`find /etc/rc*.d -name \"[KS][0-9][0-9]${_fNam}\" -type l 2>/dev/null`" ] && _rclinks=''
    }
    [ -n "${_rclinks}" -a -x "/sbin/chkconfig" ] && {
	/sbin/chkconfig --del "${_fNam}"
    }
}

# 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
  # Remove lockfiles
  ls "/var/lock/subsys/${_fNam}" "/var/lock/subsys/${_fNam}"-* 2>/dev/null | while read _fDel; do
      case "${_fDel}" in
      /var/lock/subsys/${_fNam}*) rm -f "${_fDel}" 2>/dev/null
      esac
  done
  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
  [ -n "`ls -l /sbin/rc* 2>/dev/null | grep ' -> .*/init\.d/'`" ] && {
    [ -L "/sbin/rc${_fNam}" ] && {
      # Remove dangling /sbin/rc${_fNam} symlink.  I think these are used only on SuSE Linux, but who knows ...
      ls -L "/sbin/rc${_fNam}" >/dev/null 2>&1 || rm -f "/sbin/rc${_fNam}" 2>/dev/null
    }
  }
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 == \"_dummy_\" {" >> $_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


exit 0
