#!/bin/bash
#  $Id:
#  Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
#
#  Search for a local Gateway and remove configurations made for colocation if found.

source common.sh
i18n_msgfile="${TTAINSTDIR}/etc/pkg/tta/install/en-us_setup"
[ -s "${i18n_msgfile}" ] && source "${i18n_msgfile}"

# 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


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

if [ -f ${TTAMSG}/common -a -f ${TTAMSG}/drop_gateway.msg ]; then
  . ${TTAMSG}/common
  . ${TTAMSG}/drop_gateway.msg
else
  echo "Unable to open message file: ${TTAMSG}/drop_gateway.msg"
  exit 1
fi

if [ -f ${TTAMSG}/common -a -f ${TTAMSG}/gateway_common.msg ]; then
  . ${TTAMSG}/common
  . ${TTAMSG}/gateway_common.msg
else
  echo "Unable to open message file: ${TTAMSG}/gateway_common.msg"
  exit 1
fi

GetPrivUsrGrp()
{
    cat $TTAINSTDIR/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" : '.* \([^ ]*\)$'`

gwprivusr="sgdgsys"
gwprivgrp="sgdgserv"


force_drop="no" # Mandate use of --force switch

# Verify that the server has a local gateway installed and that
# the gateway is configured for a co-located deployment. Will exit
# and provide users with information it it cannot find a local
# gateway configured for co-location.
#
# Args: $1 installation directory of the SGD server
#       $2 installation directory of the SGD GW
#
verifyGatewayInstalledAndColocated()
{
    sgd_install_path="$1"
    gw_install_path="$2"
    sgd_peer_dns="$("${sgd_install_path}/bin/scripts/getconfig" --peerdns)"
 
    # verify that sgd server has a local GW
    if [ "${force_drop}" != "yes" ]
    then
        echo "${msg_local_detecting_gw}"
        detectLocalGateway "${sgd_install_path}" "${gw_install_path}" "${sgd_peer_dns}"
    fi
    # check if server is actually colocated
    haveColocatedConstraint "${sgd_install_path}"
    if [ $? -eq ${COMMON_RETURN_FALSE} ]
    then
    exit_on_error "${error_local_not_colocated}"
    fi
}

# Check the arguments
if [ $# -eq 0 ]
then
    exit_usage_error "${drop_gateway_short_usage}"
fi

# Drop modes
DROP_LOCAL="local"

# Parse arg to determined drop mode
while [ -n "$1" ]
do
    case "$1" in
        --help)
            exit_usage_error "${drop_gateway_long_usage}"
            ;;
        --local)
            drop_mode="${DROP_LOCAL}"
            ;;
        --force)
            force_drop="yes"
            ;;
        *)
            echo "${bad_argument} $1"
            exit_usage_error "${drop_gateway_short_usage}"
            ;;
    esac
    shift
done

# Drop the gateway
case "${drop_mode}" in
    "${DROP_LOCAL}")

    # ask user if they are sure they wish to drop gateway
    response="${i18n_no}"
    msg="${msg_local_drop_notify}"
    the_question="${msg_local_drop_sure}"
[ -z "${i18n_yes}" ] && i18n_yes="${a_yes}"
[ -z "${i18n_no}" ] && i18n_no="${a_no}"
[ -z "${i18n_case_yes}" ] && i18n_case_yes="${a_case_yes}"
[ -z "${i18n_case_no}" ] && i18n_case_no="${a_case_no}"
[ -z "${i18n_s_yes}" ] && i18n_s_yes="${s_yes}"
[ -z "${i18n_s_no}" ] && i18n_s_no="${s_no}"
[ "${msg}" != "NOPROMPT" ] && echo "${msg}"
echo # blank line between prompt and question
_valid=false
while [ $_valid = false ]
do
  echo $N "${the_question} [$response] $C"
  read _answer
  if [ -z "$_answer" ]
  then
    _answer="$response"   # default
  fi
  case "$_answer" in
    [${i18n_case_yes}]* )
	    _answer=$i18n_s_yes
	    _valid=true
	    ;;
    [${i18n_case_no}]* )
	    _answer=$i18n_s_no
	    _valid=true
	    ;;
  esac
done
response="$_answer"


    if [ "$i18n_s_no" == "$response" ]
    then
        echo "${msg_local_drop_nothing_changed}" 
        exit 0 
    fi
    
    verifyGatewayInstalledAndColocated "${TTAINSTDIR}" "${GW_INSTALL_PATH}"
    
    # change the sgd webserver http/https ports back to default values
    changeWebserverPorts "${TTAINSTDIR}"  "${SGD_COLOCATED_HTTP_PORT}" \
                 "${SGD_COLOCATED_HTTPS_PORT}" "${HTTP_PORT}" "${HTTPS_PORT}"
    
    # change the webservices http port back to the default value
    changeWebservicePorts "${TTAINSTDIR}" "${SGD_COLOCATED_HTTP_PORT}" \
                  "${HTTP_PORT}"
    
    removeColocatedConstraint "${TTAINSTDIR}"

    # remove local gateway entry from the SGD server
    ${TTAINSTDIR}/bin/tarantella gateway remove --name ${SGD_GW_ALIAS} > /dev/null
    
    # reset the security gateway configuration
    ${TTAINSTDIR}/bin/tarantella config edit --security-gateway $'""' > /dev/null

    # remove the gateways' certificate from the SGD server
    rm -f "${TTAINSTDIR}/${SGD_GW_CERT_FILE}"
    
    # remove local SGD server entry from the gateway
    if [ -x "${GW_INSTALL_PATH}/bin/gateway" ]
    then
        "${GW_INSTALL_PATH}/bin/gateway" server remove --server "${GW_SGD_ALIAS}" >/dev/null
    fi
    echo "${msg_local_drop_complete}"
esac
    


