#!/bin/sh
#  $Id: gateway_stop.sh 58770 2015-03-31 12:26:18Z pc161604 $
#  Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
#  This software code contains proprietary information
#  and should be treated as confidential.
#
#  Component: Secure Global Desktop Gateway
#  Author: Bill Weir

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


[ "${trace}" = "x" ] && set -x

usage() {
    echo "${i18n_stopusage10_gw}"
    exit 1
}

# Have symmetrical requirement with 'gateway start'
u="`LANG=C LC_ALL=C id | sed 's/.*uid=\([0-9][0-9]*\).*/\1/'`"
if [ "$u" = "0" ]
then
  : # all is okay
else
  echo "You must be root to run this program"
  exit 15
fi


ASK="t"
case "$1" in
--force) ASK=""; shift
esac

[ -n "$1" ] && usage

. ${INSTDIR}/etc/version

[ -n "${ASK}" ] && {
    a_confirm="${i18n_no}"
[ -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}"
[ "" != "NOPROMPT" ] && echo ""
echo # blank line between prompt and question
_valid=false
while [ $_valid = false ]
do
  echo $N "${i18n_confirmstop30_gw} [$a_confirm] $C"
  read _answer
  if [ -z "$_answer" ]
  then
    _answer="$a_confirm"   # 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
a_confirm="$_answer"

    case "${a_confirm}" in
    [${i18n_case_no}]* ) exit 1
    esac
}

apachepath="${INSTDIR}/httpd/httpd-${VERSION_APACHE}"
apachebin=$apachepath/bin/apachectl
apacheconf=$apachepath/conf/httpd.conf
fApachePid="${apachepath}/logs/httpd.pid"
fProxyPid="${INSTDIR}/proxy/var/run/proxy.pid"

GetApachePid() {
    _pid=""
    [ -s "${fApachePid}" ] && _pid=`tr -cd '0123456789' <"${fApachePid}"`
    [ -n "${_pid}" ] && [ -n "`ps -p${_pid} | grep \"${_pid}\"`" ] && echo "${_pid}"
    return 0
}

GetProxyPid() {
    _pid=""
    [ -s "${fProxyPid}" ] && _pid=`tr -cd '0123456789' <"${fProxyPid}"`
    [ -n "${_pid}" ] && [ -n "`ps -p${_pid} | grep \"${_pid}.*java\"`" ] && echo "${_pid}"
    return 0
}


echo "${i18n_stopping10_gw}"

[ -n "${LD_LIBRARY_PATH}" ] && LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:"
LD_LIBRARY_PATH="${LD_LIBRARY_PATH}${apachepath}/lib" export LD_LIBRARY_PATH
stop_cmd="-D SSL -k stop"
$apachebin -d $apachepath $stop_cmd >/dev/null

# wait for the Apache pid file to go away
n=10
while [ "${n}" -gt 0 -a -s "${fApachePid}" ]; do
    sleep 1
    n=`expr "${n}" - 1`
done

ProxyPid=`GetProxyPid`
[ -n "${ProxyPid}" ] && {
    n=10
    while [ -n "${ProxyPid}" -a "${n}" -gt 0 ]; do
	[ "${n}" -eq 10 -o "${n}" -eq 5 ] && kill "${ProxyPid}" 2>/dev/null
	sleep 1
	ProxyPid=`GetProxyPid`
	n=`expr "${n}" - 1`
    done
    [ -n "${ProxyPid}" ] && {
	kill -9 "${ProxyPid}" 2>/dev/null
	sleep 5
	ProxyPid=`GetProxyPid`
    }
    if [ -n "${ProxyPid}" ]; then
	echo "${i18n_cantstop10_gw}"
	exit 1
    else
	rm -f ${fProxyPid} 2>/dev/null
    fi
}

exit 0
