#!/bin/sh
#  $Id: gateway_status.sh 144933 2017-02-08 09:50:25Z justin.carveley@oracle.com $
#  Copyright (c) 2008, 2020, Oracle and/or its affiliates.
#  This software code contains proprietary information
#  and should be treated as confidential.
#
#  Component: Secure Global Desktop Gateway
#  Author: Bill Weir

. "${INSTDIR}/bin/scripts/common"

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

usage() {
    echo "
gateway status [[--format text|xml|delimited] [--delimiter <delim>]]

  Shows the current status of the Secure Global Desktop Gateway, including
  a summary of the connections being serviced and how they are distributed.
  
  --format text|xml|delimited   Output format (default: text)
  --delimiter <delim>           Delimiter used with delimited output format
    "
    exit 1
}

GetSGDServers() {
    # Note: This keytool code is repeated and should be placed in common
    # location which can be shared by the scripts.
    # The keytool command can be used without the password specified as
    # an argument to list the keystore contents. When used this way,
    # keytool asks for a password which can be answered with a carriage
    # return. The keytool will still present the results but displays a
    # warning message which is sent to /dev/null
    LANG=C LC_ALL=C ${GW_JAVA_DIR}/keytool \
	-list -v \
	-keystore "${INSTDIR}/proxy/etc/keystore" << EOF 2> /dev/null |
EOF
    while read data; do
	case "${data}" in
	    Alias*[nN]ame:*)
                alias=`expr "${data}" : 'Alias[^:]*: *\(.*\)'`
		;;
	    Entry*[tT]ype:*)
                type=`expr "${data}" : 'Entry[^:]*: *\(.*\)'`
		[ "${type}" = "trustedCertEntry" ] && echo "${alias}"
		;;
        esac
    done |
    LANG=C LC_ALL=C sort -u |
    while read alias; do
       case "${alias}" in
         *-ssl)
            # ignore the ssl certs
            ;;
         *)
            echo ${alias}
            ;;
       esac
    done
}

QueryProxy() {
    EMPTY=`grep empty ${INSTDIR}/etc/reflection-ssl.xml`
    [ -n "${EMPTY}" ] && {
      # The reflection service is not enabled
      echo "${i18n_statusup10_gw}"
      echo "${i18n_statusmoreinfo_gw}" 
      exit 1
    } 

    SGDSERVERS=`GetSGDServers|tr '\012' ' '`

    XML="" DELIMITED="" DELIMITER=""
    while [ -n "$1" ]; do
    case "$1" in
    --delimiter)
        case "$2" in
            "" | --*) printf "${i18n_invalidarg01_gw}" "$1"; usage
        esac
        DELIMITER="$2"
        shift
        ;;
    --format)
        case "$2" in
            xml) XML="true";;
            delimited) DELIMITED="true";;
            text) ;;
            *) printf "${i18n_invalidarg01_gw}" "$1"; usage;;
        esac
        shift
        ;;
    --help)
        usage
        exit 1
        ;;
    *) usage "$1"
    esac
    shift
    done

    # Build the command line for the Java code
    AIPMAXCON=`grep maxConnections ../etc/aip-maxcon.xml | tr '<' ' ' | tr '>' ' ' | awk '{print $2}'`
    HTTPMAXCON=`grep maxConnections ../etc/http-maxcon.xml | tr '<' ' ' | tr '>' ' ' | awk '{print $2}'`
    WSMAXCON=`grep maxConnections ../etc/ws-maxcon.xml | tr '<' ' ' | tr '>' ' ' | awk '{print $2}'`
    CMDLINE="${CMDLINE} -maxconns ${AIPMAXCON} ${HTTPMAXCON} ${WSMAXCON}" 
 
    # Add format arguments
    [ -n "${XML}" ] && {
        CMDLINE="${CMDLINE} -xml"
    }

    [ -n "${DELIMITED}" -a -n "${DELIMITER}" ] && {
        CMDLINE="${CMDLINE} -delimiter ${DELIMITER}"
    }

    # Execute the request
    KEYSTORE_PASSWD=`cat ${INSTDIR}/etc/password.reflection`
    HOST=`sed 's/\(.*\):/\1 /' < ${INSTDIR}/etc/reflection-auth-binding.xml | tr '<' ' ' | tr '>' ' ' | awk '{ print $2 }'`
    PORT=`sed 's/\(.*\):/\1 /' < ${INSTDIR}/etc/reflection-auth-binding.xml | tr '<' ' ' | tr '>' ' ' | awk '{ print $3 }'`
    [ "${HOST}" = '*' ] && HOST="localhost"
    ID_MAPPINGS="${INSTDIR}/etc/serveridmap.properties"

    JAVA_ARGS="-classpath ${INSTDIR}/proxy/jarp.jar \
        -Djavax.net.ssl.trustStore=${INSTDIR}/proxy/etc/keystore.reflection \
        -Djavax.net.ssl.trustStorePassword=${KEYSTORE_PASSWD} \
        -Djavax.net.ssl.keyStore=${INSTDIR}/proxy/etc/keystore.reflection \
        -Djavax.net.ssl.keyStorePassword=${KEYSTORE_PASSWD} \
        -DserverId.mapping.file="${ID_MAPPINGS}" \
        proxy.output.GatewayStatus ${HOST} ${PORT}"

    ${GW_JAVA_DIR}/java ${JAVA_ARGS} "${SGDSERVERS}" ${CMDLINE}
}


isUp()
{
    ApachePid=$1
    ProxyPid=$2
    
    if [ -n "${ApachePid}" -a -n "${ProxyPid}" ]; then
	printf "1\n"
    else
	printf "0\n"
    fi

    exit 0
}

. ${INSTDIR}/etc/version

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
}


ApachePid=`GetApachePid`
ProxyPid=`GetProxyPid`

case "$1" in
    isup)
	isUp "${ApachePid}" "${ProxyPid}"
	;;
esac

# Check for --help 
for arg in "$@"; do
   case "$arg" in
        --help)
            usage
            ;;
   esac
done 

if [ -n "${ApachePid}" -a -z "${ProxyPid}" ]; then
    # Apache running, Proxy not
    printf "${i18n_statusstrange11_gw}" "${INSTDIR}"
elif [ -z "${ApachePid}" -a -n "${ProxyPid}" ]; then
    # Proxy running, Apache not
    printf "${i18n_statusstrange11_gw}" "${INSTDIR}"
elif [ -n "${ApachePid}" -a -n "${ProxyPid}" ]; then
    # Proxy and Apache both running
    QueryProxy "$@"
else
    # neither Proxy nor Apache running
    echo "${i18n_statusdown10_gw}"
fi

exit 0
