#!/bin/bash
# Copyright © 2024 Broadcom Inc. and its subsidiaries. All Rights Reserved.
#######################################################################
# Script to run the SecureSpan Gateway
#######################################################################
#
# This script will set environment options using the shared profile
# and control the Gateway process.
#
# Usage:
#
#   ./gateway.sh COMMAND [OPTION]*
#
# Commands are:
#
#   run   - run in foreground (CTRL-C to exit)
#   start - start as a daemon
#   stop  - stop daemon
#
# Options starting with -J are passed through to the Java VM, other
# options are passed to the Gateway process.
#

# Set environment
umask 0002
SSGUSER="gateway"
SSGNODE="default"
SSGJDK_VERSION="1.6"
RUNASUSER="bash"
export SSGNODE SSGUSER

# appliance check
[ -d /opt/SecureSpan/Appliance ] && IS_APPLIANCE=yes

# Source profile for standard environment
cd `dirname $0`
. ../etc/profile

PC_CONTROL="/opt/SecureSpan/Controller/bin/pc.sh"

ensure_JDK "${SSGJDK_VERSION}"

# Process script options
declare -a SSGARGS
declare -a SSGOPTS
for OPTION in "$@" ; do
    if [ "${OPTION}" = "${OPTION#-J}" ] ; then
        SSGARGS[$((${#SSGARGS[*]} + 1))]="${OPTION}"
    else
        SSGOPTS[$((${#SSGOPTS[*]} + 1))]="${OPTION#-J}"
    fi
done

# Process options
GATEWAY_PID="${SSG_HOME}/node/${SSGNODE}/var/ssg.pid"

if [ -s "/opt/SecureSpan/Gateway/node/default/etc/conf/kmp.properties" ] ; then
    JAVA_OPTS="--module-path=/opt/nfast/java/classes -Dcom.l7tech.server.home=${SSG_HOME}/node/${SSGNODE}"
else
    JAVA_OPTS="-Dcom.l7tech.server.home=${SSG_HOME}/node/${SSGNODE}"
fi

# https://docs.oracle.com/en/java/javase/11/migrate/index.html#JSMIG-GUID-2C896CA8-927C-4381-A737-B1D81D964B7B
#   java launcher will exit if the java.ext.dirs system property is set, or if the lib/ext directory exists
#   -Djava.ext.dirs=/opt/SecureSpan/JDK/jre/lib/ext:/opt/SecureSpan/Gateway/runtime/lib/ext is not supported.  Use -classpath instead.
#				Error: Could not create the Java Virtual Machine.
#				Error: A fatal exception has occurred. Program will exit.
#	What probably broken without lib/ext?
#   - /opt/SecureSpan/Gateway/runtime/lib/ext contains jms-1.1.jar
#   - /opt/SecureSpan/JDK/jre/lib/ contains nCipherKM.jar
# CLASSPATH_L7_GW_SERVER will get updated dynamically with the individual jar dependencies of Gateway.jar
CLASSPATH_L7_GW_SERVER=/opt/SecureSpan/Gateway/runtime/lib/biz.aQute.bnd.annotation-5.3.0.jar:/opt/SecureSpan/Gateway/runtime/lib/layer7-policy-exporter-11.2.1.26672.jar:/opt/SecureSpan/Gateway/runtime/lib/layer7-gateway-config-11.2.1.26672.jar:/opt/SecureSpan/Gateway/runtime/lib/layer7-gateway-management-11.2.1.26672.jar:/opt/SecureSpan/Gateway/runtime/lib/layer7-gateway-rest-toolkit-11.2.1.26672.jar:/opt/SecureSpan/Gateway/runtime/lib/layer7-pm2-console-11.2.1.26672.jar:/opt/SecureSpan/Gateway/runtime/lib/layer7-gateway-common-11.2.1.26672.jar:/opt/SecureSpan/Gateway/runtime/lib/layer7-uddi-11.2.1.26672.jar:/opt/SecureSpan/Gateway/runtime/lib/layer7-proxy-11.2.1.26672.jar:/opt/SecureSpan/Gateway/runtime/lib/layer7-identity-11.2.1.26672.jar:/opt/SecureSpan/Gateway/runtime/lib/layer7-policy-11.2.1.26672.jar:/opt/SecureSpan/Gateway/runtime/lib/layer7-wsdl-11.2.1.26672.jar:/opt/SecureSpan/Gateway/runtime/lib/layer7-gateway-sca-common-11.2.1.26672.jar:/opt/SecureSpan/Gateway/runtime/lib/layer7-config-11.2.1.26672.jar:/opt/SecureSpan/Gateway/runtime/lib/layer7-common-11.2.1.26672.jar:/opt/SecureSpan/Gateway/runtime/lib/layer7-gateway-api-1.2.00.26672.jar:/opt/SecureSpan/Gateway/runtime/lib/layer7-cassandra-11.2.1.26672.jar:/opt/SecureSpan/Gateway/runtime/lib/layer7-ntlm-11.2.1.26672.jar:/opt/SecureSpan/Gateway/runtime/lib/layer7-json-11.2.1.26672.jar:/opt/SecureSpan/Gateway/runtime/lib/layer7-kerberos-11.2.1.26672.jar:/opt/SecureSpan/Gateway/runtime/lib/extension-api-11.2.1.26672.jar:/opt/SecureSpan/Gateway/runtime/lib/layer7-utility-11.2.1.26672.jar:/opt/SecureSpan/Gateway/runtime/lib/layer7-api-8.11.04.26672.jar:/opt/SecureSpan/Gateway/runtime/lib/layer7-bouncycastle-api-11.2.1.26672.jar:/opt/SecureSpan/Gateway/runtime/lib/layer7-jaaslounge-decoding-extension-11.2.1.26672.jar:/opt/SecureSpan/Gateway/runtime/lib/analytics-3.5.1.jar:/opt/SecureSpan/Gateway/runtime/lib/logging-interceptor-4.10.0.jar:/opt/SecureSpan/Gateway/runtime/lib/analytics-core-3.5.1.jar:/opt/SecureSpan/Gateway/runtime/lib/converter-gson-2.9.0.jar:/opt/SecureSpan/Gateway/runtime/lib/retrofit-mock-2.9.0.jar:/opt/SecureSpan/Gateway/runtime/lib/retrofit-2.9.0.jar:/opt/SecureSpan/Gateway/runtime/lib/opentelemetry-exporter-otlp-1.59.0.jar:/opt/SecureSpan/Gateway/runtime/lib/opentelemetry-exporter-sender-okhttp-1.59.0.jar:/opt/SecureSpan/Gateway/runtime/lib/okhttp-jvm-5.3.2.jar:/opt/SecureSpan/Gateway/runtime/lib/kotlin-stdlib-jdk8-1.8.0.jar:/opt/SecureSpan/Gateway/runtime/lib/okio-jvm-3.16.4.jar:/opt/SecureSpan/Gateway/runtime/lib/kotlin-stdlib-jdk7-1.8.0.jar:/opt/SecureSpan/Gateway/runtime/lib/kotlin-stdlib-2.2.21.jar:/opt/SecureSpan/Gateway/runtime/lib/annotations-2023.3.3.jar:/opt/SecureSpan/Gateway/runtime/lib/forms_rt-2023.3.3.jar:/opt/SecureSpan/Gateway/runtime/lib/cxf-rt-frontend-jaxws-4.1.8.jar:/opt/SecureSpan/Gateway/runtime/lib/cxf-rt-transports-http-4.1.8.jar:/opt/SecureSpan/Gateway/runtime/lib/cxf-rt-ws-addr-4.1.8.jar:/opt/SecureSpan/Gateway/runtime/lib/cxf-rt-ws-policy-4.1.8.jar:/opt/SecureSpan/Gateway/runtime/lib/cxf-rt-frontend-simple-4.1.8.jar:/opt/SecureSpan/Gateway/runtime/lib/cxf-rt-bindings-soap-4.1.8.jar:/opt/SecureSpan/Gateway/runtime/lib/cxf-rt-bindings-xml-4.1.8.jar:/opt/SecureSpan/Gateway/runtime/lib/cxf-rt-databinding-jaxb-4.1.8.jar:/opt/SecureSpan/Gateway/runtime/lib/cxf-rt-wsdl-4.1.8.jar:/opt/SecureSpan/Gateway/runtime/lib/cxf-core-4.1.8.jar:/opt/SecureSpan/Gateway/runtime/lib/hibernate-core-jakarta-5.6.15.Final.jar:/opt/SecureSpan/Gateway/runtime/lib/jakarta.xml.ws-api-4.0.3.jar:/opt/SecureSpan/Gateway/runtime/lib/jaxws-rt-2.1.7.jar:/opt/SecureSpan/Gateway/runtime/lib/jaxb-impl-3.0.2-l7p1.jar:/opt/SecureSpan/Gateway/runtime/lib/jaxb-core-3.0.2.jar:/opt/SecureSpan/Gateway/runtime/lib/jakarta.xml.bind-api-3.0.1.jar:/opt/SecureSpan/Gateway/runtime/lib/saaj-impl-3.0.2.jar:/opt/SecureSpan/Gateway/runtime/lib/jakarta.xml.soap-api-3.0.2.jar:/opt/SecureSpan/Gateway/runtime/lib/spring-context-support-6.2.18.jar:/opt/SecureSpan/Gateway/runtime/lib/spring-webmvc-6.2.18.jar:/opt/SecureSpan/Gateway/runtime/lib/spring-context-6.2.18.jar:/opt/SecureSpan/Gateway/runtime/lib/streambuffer-0.9.jar:/opt/SecureSpan/Gateway/runtime/lib/stax-ex-2.1.0.jar:/opt/SecureSpan/Gateway/runtime/lib/angus-mail-2.0.5.jar:/opt/SecureSpan/Gateway/runtime/lib/angus-activation-2.0.3.jar:/opt/SecureSpan/Gateway/runtime/lib/jakarta.mail-api-2.1.5.jar:/opt/SecureSpan/Gateway/runtime/lib/jakarta.activation-2.0.1.jar:/opt/SecureSpan/Gateway/runtime/lib/concurrent-1.3.4.jar:/opt/SecureSpan/Gateway/runtime/lib/tomcat-embed-core-10.1.57-l7p1.jar:/opt/SecureSpan/Gateway/runtime/lib/layer7-sun-jaxws-1.0.jar:/opt/SecureSpan/Gateway/runtime/lib/whirlycache-1.0.1-l7p1.jar:/opt/SecureSpan/Gateway/runtime/lib/commons-configuration2-2.11.0.jar:/opt/SecureSpan/Gateway/runtime/lib/commons-compress-1.27.1.jar:/opt/SecureSpan/Gateway/runtime/lib/liquibase-core-4.25.1.jar:/opt/SecureSpan/Gateway/runtime/lib/commons-text-1.12.0.jar:/opt/SecureSpan/Gateway/runtime/lib/opencsv-5.8.jar:/opt/SecureSpan/Gateway/runtime/lib/commons-lang3-3.17.0.jar:/opt/SecureSpan/Gateway/runtime/lib/httpclient5-5.4.3.jar:/opt/SecureSpan/Gateway/runtime/lib/commons-pool-1.6.jar:/opt/SecureSpan/Gateway/runtime/lib/commons-beanutils-1.11.0.jar:/opt/SecureSpan/Gateway/runtime/lib/commons-collections-3.2.2.jar:/opt/SecureSpan/Gateway/runtime/lib/nekohtml-1.9.15.jar:/opt/SecureSpan/Gateway/runtime/lib/xbean-saml-2.0-1.0.jar:/opt/SecureSpan/Gateway/runtime/lib/xbean-saml-1.1-1.0.jar:/opt/SecureSpan/Gateway/runtime/lib/jaaslounge-decoding-2.0-l7p6-SNAPSHOT.jar:/opt/SecureSpan/Gateway/runtime/lib/jersey-media-multipart-3.0.16.jar:/opt/SecureSpan/Gateway/runtime/lib/jaxen-1.1.4.jar:/opt/SecureSpan/Gateway/runtime/lib/jersey-server-3.0.16.jar:/opt/SecureSpan/Gateway/runtime/lib/jersey-client-3.0.16.jar:/opt/SecureSpan/Gateway/runtime/lib/jersey-hk2-3.0.16.jar:/opt/SecureSpan/Gateway/runtime/lib/jersey-media-jaxb-3.0.16.jar:/opt/SecureSpan/Gateway/runtime/lib/jersey-metainf-services-3.0.16.jar:/opt/SecureSpan/Gateway/runtime/lib/jersey-common-3.0.16.jar:/opt/SecureSpan/Gateway/runtime/lib/jakarta.ws.rs-api-3.0.0.jar:/opt/SecureSpan/Gateway/runtime/lib/xmlbeans-3.1.0.jar:/opt/SecureSpan/Gateway/runtime/lib/sftp-9.3.27.jar:/opt/SecureSpan/Gateway/runtime/lib/java-driver-query-builder-4.19.1.jar:/opt/SecureSpan/Gateway/runtime/lib/java-driver-core-4.19.1.jar:/opt/SecureSpan/Gateway/runtime/lib/config-1.4.1.jar:/opt/SecureSpan/Gateway/runtime/lib/metrics-jmx-4.1.18.jar:/opt/SecureSpan/Gateway/runtime/lib/tomcat-annotations-api-10.1.57.jar:/opt/SecureSpan/Gateway/runtime/lib/kerb-client-api-all-2.1.1.jar:/opt/SecureSpan/Gateway/runtime/lib/kerb-client-2.1.1.jar:/opt/SecureSpan/Gateway/runtime/lib/kerb-common-2.1.1.jar:/opt/SecureSpan/Gateway/runtime/lib/commons-io-2.18.0.jar:/opt/SecureSpan/Gateway/runtime/lib/quartz-2.3.2.jar:/opt/SecureSpan/Gateway/runtime/lib/c3p0-0.12.0.jar:/opt/SecureSpan/Gateway/runtime/lib/mchange-commons-java-0.4.0.jar:/opt/SecureSpan/Gateway/runtime/lib/hibernate-ehcache-5.6.15.Final.jar:/opt/SecureSpan/Gateway/runtime/lib/hibernate-commons-annotations-5.1.2.Final.jar:/opt/SecureSpan/Gateway/runtime/lib/hk2-locator-3.0.6.jar:/opt/SecureSpan/Gateway/runtime/lib/hk2-api-3.0.6.jar:/opt/SecureSpan/Gateway/runtime/lib/hk2-utils-3.0.6.jar:/opt/SecureSpan/Gateway/runtime/lib/jakarta.inject-api-2.0.1.jar:/opt/SecureSpan/Gateway/runtime/lib/javassist-3.30.2-GA.jar:/opt/SecureSpan/Gateway/runtime/lib/jboss-j2ee-4.0.5.GA.jar:/opt/SecureSpan/Gateway/runtime/lib/jboss-common-core-2.2.14.GA.jar:/opt/SecureSpan/Gateway/runtime/lib/ftpserver-core-1.1.2.jar:/opt/SecureSpan/Gateway/runtime/lib/mina-core-2.1.10.jar:/opt/SecureSpan/Gateway/runtime/lib/mysql-connector-j-8.4.0.jar:/opt/SecureSpan/Gateway/runtime/lib/slf4j-jdk14-2.0.17.jar:/opt/SecureSpan/Gateway/runtime/lib/jcl-over-slf4j-1.7.28.jar:/opt/SecureSpan/Gateway/runtime/lib/log4j-over-slf4j-1.7.28.jar:/opt/SecureSpan/Gateway/runtime/lib/jose4j-0.9.6.jar:/opt/SecureSpan/Gateway/runtime/lib/jetty-http2-hpack-12.0.34.jar:/opt/SecureSpan/Gateway/runtime/lib/jetty-http-12.0.34.jar:/opt/SecureSpan/Gateway/runtime/lib/jetty-alpn-java-client-12.0.34.jar:/opt/SecureSpan/Gateway/runtime/lib/jetty-alpn-client-12.0.34.jar:/opt/SecureSpan/Gateway/runtime/lib/jetty-io-12.0.34.jar:/opt/SecureSpan/Gateway/runtime/lib/jetty-util-12.0.34.jar:/opt/SecureSpan/Gateway/runtime/lib/log4j-to-slf4j-2.23.1.jar:/opt/SecureSpan/Gateway/runtime/lib/xmlsec-2.3.4.jar:/opt/SecureSpan/Gateway/runtime/lib/ntlmv2-lib-1.0.4.jar:/opt/SecureSpan/Gateway/runtime/lib/metrics-core-4.1.18.jar:/opt/SecureSpan/Gateway/runtime/lib/kerb-util-2.1.1.jar:/opt/SecureSpan/Gateway/runtime/lib/kerby-config-2.1.1.jar:/opt/SecureSpan/Gateway/runtime/lib/token-provider-2.1.1.jar:/opt/SecureSpan/Gateway/runtime/lib/kerb-crypto-2.1.1.jar:/opt/SecureSpan/Gateway/runtime/lib/kerb-core-2.1.1.jar:/opt/SecureSpan/Gateway/runtime/lib/kerby-pkix-2.1.1.jar:/opt/SecureSpan/Gateway/runtime/lib/slf4j-api-1.7.28.jar:/opt/SecureSpan/Gateway/runtime/lib/spring-orm-6.2.18.jar:/opt/SecureSpan/Gateway/runtime/lib/spring-jdbc-6.2.18.jar:/opt/SecureSpan/Gateway/runtime/lib/spring-tx-6.2.18.jar:/opt/SecureSpan/Gateway/runtime/lib/spring-web-6.2.18.jar:/opt/SecureSpan/Gateway/runtime/lib/spring-aop-6.2.18.jar:/opt/SecureSpan/Gateway/runtime/lib/spring-beans-6.2.18.jar:/opt/SecureSpan/Gateway/runtime/lib/spring-expression-6.2.18.jar:/opt/SecureSpan/Gateway/runtime/lib/spring-core-6.2.18.jar:/opt/SecureSpan/Gateway/runtime/lib/jakarta.transaction-api-2.0.1.jar:/opt/SecureSpan/Gateway/runtime/lib/spring-remoting-jakarta-5.3.48.jar:/opt/SecureSpan/Gateway/runtime/lib/ftplet-api-1.1.2.jar:/opt/SecureSpan/Gateway/runtime/lib/tomcat-embed-websocket-10.1.57.jar:/opt/SecureSpan/Gateway/runtime/lib/retrofit-1.9.0.jar:/opt/SecureSpan/Gateway/runtime/lib/gson-2.11.0.jar:/opt/SecureSpan/Gateway/runtime/lib/ojdbc10-19.16.0.0.jar:/opt/SecureSpan/Gateway/runtime/lib/mssql-jdbc-12.6.5.jre11.jar:/opt/SecureSpan/Gateway/runtime/lib/wiseman-jakarta-1.0.jar:/opt/SecureSpan/Gateway/runtime/lib/wsdl4j-1.6.2-l7p3.jar:/opt/SecureSpan/Gateway/runtime/lib/jakarta.jws-api-3.0.0.jar:/opt/SecureSpan/Gateway/runtime/lib/derby-10.17.1.0.jar:/opt/SecureSpan/Gateway/runtime/lib/derbytools-10.17.1.0.jar:/opt/SecureSpan/Gateway/runtime/lib/derbyshared-10.17.1.0.jar:/opt/SecureSpan/Gateway/runtime/lib/caffeine-3.1.8.jar:/opt/SecureSpan/Gateway/runtime/lib/opentelemetry-exporter-logging-1.59.0.jar:/opt/SecureSpan/Gateway/runtime/lib/opentelemetry-sdk-extension-autoconfigure-1.59.0.jar:/opt/SecureSpan/Gateway/runtime/lib/opentelemetry-exporter-otlp-common-1.59.0.jar:/opt/SecureSpan/Gateway/runtime/lib/opentelemetry-exporter-common-1.59.0.jar:/opt/SecureSpan/Gateway/runtime/lib/opentelemetry-sdk-extension-autoconfigure-spi-1.59.0.jar:/opt/SecureSpan/Gateway/runtime/lib/opentelemetry-sdk-1.59.0.jar:/opt/SecureSpan/Gateway/runtime/lib/opentelemetry-sdk-metrics-1.59.0.jar:/opt/SecureSpan/Gateway/runtime/lib/opentelemetry-sdk-trace-1.59.0.jar:/opt/SecureSpan/Gateway/runtime/lib/opentelemetry-sdk-logs-1.59.0.jar:/opt/SecureSpan/Gateway/runtime/lib/opentelemetry-sdk-common-1.59.0.jar:/opt/SecureSpan/Gateway/runtime/lib/opentelemetry-api-1.59.0.jar:/opt/SecureSpan/Gateway/runtime/lib/opentelemetry-context-1.59.0.jar:/opt/SecureSpan/Gateway/runtime/lib/log4j-core-2.23.1.jar:/opt/SecureSpan/Gateway/runtime/lib/log4j-api-2.23.1.jar:/opt/SecureSpan/Gateway/runtime/lib/spring-shell-1.2.0.RELEASE.jar:/opt/SecureSpan/Gateway/runtime/lib/spqr-0.12.0.jar:/opt/SecureSpan/Gateway/runtime/lib/graphql-java-extended-scalars-19.1.jar:/opt/SecureSpan/Gateway/runtime/lib/graphql-java-19.11.jar:/opt/SecureSpan/Gateway/runtime/lib/netty-handler-4.1.136.Final.jar:/opt/SecureSpan/Gateway/runtime/lib/picocli-4.7.5.jar:/opt/SecureSpan/Gateway/runtime/lib/xom-1.2.8.jar:/opt/SecureSpan/Gateway/runtime/lib/aspectjweaver-1.6.12.jar:/opt/SecureSpan/Gateway/runtime/lib/guava-33.4.0-jre.jar:/opt/SecureSpan/Gateway/runtime/lib/cglib-nodep-2.2.2.jar:/opt/SecureSpan/Gateway/runtime/lib/javax.el-3.0.1-b09.jar:/opt/SecureSpan/Gateway/runtime/lib/osgi.annotation-8.0.0.jar:/opt/SecureSpan/Gateway/runtime/lib/org.osgi.namespace.extender-1.0.1.jar:/opt/SecureSpan/Gateway/runtime/lib/org.osgi.namespace.service-1.0.0.jar:/opt/SecureSpan/Gateway/runtime/lib/org.osgi.resource-1.0.1.jar:/opt/SecureSpan/Gateway/runtime/lib/org.osgi.service.serviceloader-1.0.0.jar:/opt/SecureSpan/Gateway/runtime/lib/micrometer-observation-1.15.11.jar:/opt/SecureSpan/Gateway/runtime/lib/validation-api-2.0.1.Final.jar:/opt/SecureSpan/Gateway/runtime/lib/httpcore5-h2-5.3.4.jar:/opt/SecureSpan/Gateway/runtime/lib/httpcore5-5.3.4.jar:/opt/SecureSpan/Gateway/runtime/lib/jcifs-1.3.17.jar:/opt/SecureSpan/Gateway/runtime/lib/servlet-api-2.5.jar:/opt/SecureSpan/Gateway/runtime/lib/serializer-2.7.3.jar:/opt/SecureSpan/Gateway/runtime/lib/layer7-uddi-lib-jakarta-1.1.0.jar:/opt/SecureSpan/Gateway/runtime/lib/resolver-20050927.jar:/opt/SecureSpan/Gateway/runtime/lib/commons-codec-1.17.1.jar:/opt/SecureSpan/Gateway/runtime/lib/apache-harmony-asn1-0.1.jar:/opt/SecureSpan/Gateway/runtime/lib/servlet-api-resources-2.5.jar:/opt/SecureSpan/Gateway/runtime/lib/xercesImpl-2.12.2.jar:/opt/SecureSpan/Gateway/runtime/lib/jakarta.persistence-api-3.0.0.jar:/opt/SecureSpan/Gateway/runtime/lib/xbean-xenc-xmldsig-1.0.jar:/opt/SecureSpan/Gateway/runtime/lib/saxon9ee-9.4.0.9.jar:/opt/SecureSpan/Gateway/runtime/lib/policy-1.0.jar:/opt/SecureSpan/Gateway/runtime/lib/xalan-2.7.3-l7p1.jar:/opt/SecureSpan/Gateway/runtime/lib/jackson-datatype-jsr310-2.18.5.jar:/opt/SecureSpan/Gateway/runtime/lib/jackson-annotations-2.18.5.jar:/opt/SecureSpan/Gateway/runtime/lib/jackson-datatype-jdk8-2.18.5.jar:/opt/SecureSpan/Gateway/runtime/lib/jackson-module-parameter-names-2.18.5.jar:/opt/SecureSpan/Gateway/runtime/lib/jackson-dataformat-yaml-2.18.5.jar:/opt/SecureSpan/Gateway/runtime/lib/jackson-databind-2.18.5.jar:/opt/SecureSpan/Gateway/runtime/lib/jackson-core-2.18.5.jar:/opt/SecureSpan/Gateway/runtime/lib/snakeyaml-2.3.jar:/opt/SecureSpan/Gateway/runtime/lib/mimepull-1.9.6.jar:/opt/SecureSpan/Gateway/runtime/lib/sunxacml-1.2.jar:/opt/SecureSpan/Gateway/runtime/lib/lz4-java-1.11.0.jar:/opt/SecureSpan/Gateway/runtime/lib/json-schema-validator-0.1.6.l7p2.jar:/opt/SecureSpan/Gateway/runtime/lib/json-schema-validator-0.0.1-SNAPSHOT.jar:/opt/SecureSpan/Gateway/runtime/lib/java-driver-guava-shaded-4.19.1.jar:/opt/SecureSpan/Gateway/runtime/lib/native-protocol-1.5.2.jar:/opt/SecureSpan/Gateway/runtime/lib/HdrHistogram-2.1.12.jar:/opt/SecureSpan/Gateway/runtime/lib/reactive-streams-1.0.3.jar:/opt/SecureSpan/Gateway/runtime/lib/woodstox-core-7.2.1.jar:/opt/SecureSpan/Gateway/runtime/lib/xmlschema-core-2.3.2.jar:/opt/SecureSpan/Gateway/runtime/lib/xml-resolver-1.2.jar:/opt/SecureSpan/Gateway/runtime/lib/asm-9.10.1.jar:/opt/SecureSpan/Gateway/runtime/lib/hibernate-validator-6.2.5.Final.jar:/opt/SecureSpan/Gateway/runtime/lib/jboss-logging-3.3.2.Final.jar:/opt/SecureSpan/Gateway/runtime/lib/byte-buddy-1.15.11.jar:/opt/SecureSpan/Gateway/runtime/lib/antlr-2.7.7-l7p1.jar:/opt/SecureSpan/Gateway/runtime/lib/jandex-2.0.5.Final.jar:/opt/SecureSpan/Gateway/runtime/lib/classmate-1.3.4.jar:/opt/SecureSpan/Gateway/runtime/lib/dom4j-2.1.3.jar:/opt/SecureSpan/Gateway/runtime/lib/ehcache-2.10.10.22.12.jar:/opt/SecureSpan/Gateway/runtime/lib/spring-jcl-6.2.18.jar:/opt/SecureSpan/Gateway/runtime/lib/backo-1.0.0.jar:/opt/SecureSpan/Gateway/runtime/lib/auto-value-annotations-1.10.1.jar:/opt/SecureSpan/Gateway/runtime/lib/error_prone_annotations-2.27.0.jar:/opt/SecureSpan/Gateway/runtime/lib/commons-collections4-4.4.jar:/opt/SecureSpan/Gateway/runtime/lib/checker-qual-3.37.0.jar:/opt/SecureSpan/Gateway/runtime/lib/opentelemetry-common-1.59.0.jar:/opt/SecureSpan/Gateway/runtime/lib/jline-2.12.jar:/opt/SecureSpan/Gateway/runtime/lib/failureaccess-1.0.2.jar:/opt/SecureSpan/Gateway/runtime/lib/neethi-3.2.3.jar:/opt/SecureSpan/Gateway/runtime/lib/micrometer-commons-1.15.11.jar:/opt/SecureSpan/Gateway/runtime/lib/aopalliance-repackaged-3.0.6.jar:/opt/SecureSpan/Gateway/runtime/lib/osgi-resource-locator-1.0.3.jar:/opt/SecureSpan/Gateway/runtime/lib/jakarta.validation-api-3.0.2.jar:/opt/SecureSpan/Gateway/runtime/lib/gson-java8-datatype-1.1.0.jar:/opt/SecureSpan/Gateway/runtime/lib/geantyref-1.3.13.jar:/opt/SecureSpan/Gateway/runtime/lib/classgraph-4.8.151.jar:/opt/SecureSpan/Gateway/runtime/lib/netty-transport-native-unix-common-4.1.136.Final.jar:/opt/SecureSpan/Gateway/runtime/lib/netty-codec-4.1.136.Final.jar:/opt/SecureSpan/Gateway/runtime/lib/netty-transport-4.1.136.Final.jar:/opt/SecureSpan/Gateway/runtime/lib/netty-resolver-4.1.136.Final.jar:/opt/SecureSpan/Gateway/runtime/lib/netty-buffer-4.1.136.Final.jar:/opt/SecureSpan/Gateway/runtime/lib/netty-common-4.1.136.Final.jar:/opt/SecureSpan/Gateway/runtime/lib/stax2-api-4.3.0.jar:/opt/SecureSpan/Gateway/runtime/lib/java-dataloader-3.2.0.jar:/opt/SecureSpan/Gateway/runtime/lib/nimbus-jose-jwt-10.6.jar:/opt/SecureSpan/Gateway/runtime/lib/kerby-asn1-2.1.1.jar:/opt/SecureSpan/Gateway/runtime/lib/kerby-util-2.1.1.jar:${SSG_JAVA_HOME}/jre/lib/ext/*:${SSG_HOME}/runtime/lib/ext/*:${SSG_HOME}/runtime/Gateway.jar

##search through system.properties to find com.l7tech.security.fips.enabled
## file location $SSG_HOME/node/default/conf/system.properties
fips_enabled=false

SYSTEM_PROPERTIES_FILE=$SSG_HOME/node/default/etc/conf/system.properties
if grep -qi "^\s*com.l7tech.security.fips.enabled\s*=\s*true\s*$" $SYSTEM_PROPERTIES_FILE; then
  fips_enabled=true
fi

if $fips_enabled; then
  CLASSPATH_L7_GW_SERVER=${CLASSPATH_L7_GW_SERVER}:${SSG_HOME}/runtime/bcfips/*
else
  CLASSPATH_L7_GW_SERVER=${CLASSPATH_L7_GW_SERVER}:${SSG_HOME}/runtime/bc/*
fi

JAVA_OPTS="${JAVA_OPTS} -classpath ${CLASSPATH_L7_GW_SERVER}"

if [ -n "${SSG_JAVA_OPTS}" ] ; then
  JAVA_OPTS="${JAVA_OPTS} ${SSG_JAVA_OPTS}"
fi
if [ -n "${NODE_OPTS}" ] ; then
  JAVA_OPTS="${JAVA_OPTS} ${NODE_OPTS}"
fi
for OPT in "${SSGOPTS[@]}"; do
    if [[ "${JAVA_OPTS}" =~ "${OPT%%=*}=[a-zA-Z0-9_\.-]{1,245}" ]] ; then
        JAVA_OPTS=${JAVA_OPTS/${BASH_REMATCH}/${OPT}}
    else
        JAVA_OPTS="${JAVA_OPTS} ${OPT}"
    fi
done

# this puts the necessary "id" that supports -u first in the path on Solaris, and is a noop on other OSes
OLDPATH=$PATH
PATH="/usr/xpg4/bin:$PATH"

# Sanity checks
if [ -z "${SSG_JAVA_HOME}" ] ; then
    echo "Java is not configured for gateway."
    exit 13
fi
if [ ! -x "${SSG_JAVA_HOME}/bin/java" ] ; then
    echo "Java not found: ${SSG_JAVA_HOME}"
    exit 13
fi
if [ "$(id -u)" != "$(id -u ${SSGUSER})" ] ; then
    if [ "$1" = "run" ] || [ "$(id -u)" != "0" ] ; then
        echo "Please run as the user: ${SSGUSER}"
        exit 13
    else 
        RUNASUSER="su "${SSGUSER}""
    fi
fi

# Helper functions
pid_running() {
  if [ -f "${GATEWAY_PID}" ] ; then (
    PIDTEXT=`cat ${GATEWAY_PID}`;
    PID=`printf "%d" ${PIDTEXT}`;
    if [ ${PID} -eq 0 ]; then
      return 1;
    fi;
    (ps -e -o pid | egrep -- "^ *${PID}$" >/dev/null);
    if [ $? -eq 0 ]; then (
      (ps -e -o command | egrep -- "[G]ateway.jar" >/dev/null);
      return $?);
    else
      return 1;
    fi
    );
  else
    return 1;
  fi
}

pid_exited() {
  if pid_running ; then
    return 1;
  else
    return 0;
  fi
}

wait_for_pid() {
  TRYCOUNT=0
  while [ $TRYCOUNT -lt 45 ]
  do
    if pid_exited; then
      return 0
    fi
    TRYCOUNT=`expr $TRYCOUNT + 1`
    sleep 1
  done
  # Too many tries; give up
  return 1
}

ensureNotRunning() {
    if [ ! -z "${GATEWAY_PID}" ]; then
        if pid_running ; then
            echo "Gateway is already running."
            exit 33
        fi
    fi
}

echoOptions() {
    echo "Gateway commands:"
    echo -e "\trun   - Run the gateway with console output"
    echo -e "\tstart - Start the gateway (option: -console)"
    echo -e "\tstop  - Stop the gateway (option: -force)"
    echo ""
}

# Debug options
if [ "$1" = "jpda" ] ; then
  if [ -z "$JPDA_TRANSPORT" ]; then
    JPDA_TRANSPORT="dt_socket"
  fi
  if [ -z "$JPDA_ADDRESS" ]; then
    JPDA_ADDRESS="8000"
  fi
  if [ -z "$JPDA_OPTS" ]; then
    JPDA_OPTS="-Xdebug -Xrunjdwp:transport=$JPDA_TRANSPORT,address=$JPDA_ADDRESS,server=y,suspend=n"
  fi
  JAVA_OPTS="$JAVA_OPTS $JPDA_OPTS"
  shift
fi

# is the PC calling?
if [ "$1" = "pc" ] ; then
    PC_CALL="yes"
    shift
fi

# Move to node directory and perform action
cd "${SSG_HOME}/node/${SSGNODE}"

if [ "$1" = "start" ] ; then
    shift

    if [ "$1" = "-console" ]; then
        shift
        JAVA_OPTS="${JAVA_OPTS} -Dcom.l7tech.server.log.console=true"
    fi

    if [ -z "${IS_APPLIANCE}" -a -z "${PC_CALL}" ] ; then
      echo "Starting Process Controller..."
      ${PC_CONTROL} start
    else
        ensureNotRunning

        #enable logging of stdout/stderr using JDK logging as well as the standard SSG logging facilities
        ${RUNASUSER} <<-SUEND
        "${SSG_JAVA_HOME}/bin/java" -Djava.util.logging.config.class=com.l7tech.server.log.JdkLogConfig ${JAVA_OPTS} com.l7tech.server.boot.GatewayMain "${SSGARGS[@]}" &

        if [ ! -z "${GATEWAY_PID}" ]; then
            rm -f "${GATEWAY_PID}"
            echo \$! > "${GATEWAY_PID}"
        fi
SUEND
    fi


elif [ "$1" = "run" ] ; then
    shift

    if [ -z "${IS_APPLIANCE}" -a -z "${PC_CALL}" ] ; then
      echo "Starting Process Controller..."
      ${PC_CONTROL} start
    else
        ensureNotRunning

        if [ -n "${GATEWAY_PID}" ]; then
            rm -f "${GATEWAY_PID}"
            echo $$ > "${GATEWAY_PID}"
        fi

        if [ -z "${IS_APPLIANCE}" ]; then
            # Software only version requires emptying stdout/stderr or it may block.
            # "/dev/null" can be replaced with a filename in the following to capture messages if required
            exec "${SSG_JAVA_HOME}/bin/java" ${JAVA_OPTS} com.l7tech.server.boot.GatewayMain "${SSGARGS[@]}" &> /dev/null
        else
            exec "${SSG_JAVA_HOME}/bin/java" ${JAVA_OPTS} com.l7tech.server.boot.GatewayMain "${SSGARGS[@]}"
        fi
    fi

elif [ "$1" = "stop" ] ; then
  shift

    if [ -z "${IS_APPLIANCE}" -a -z "${PC_CALL}" ] ; then
    echo "Stopping Process Controller..."
    ${PC_CONTROL} stop
  fi

  FORCE=0
  if [ "$1" = "-force" ]; then
    shift
    FORCE=1
  fi

  if [ $FORCE -eq 0 ]; then
    if [ -n "${GATEWAY_PID}" ] && [ -f "${GATEWAY_PID}" ]; then
        # signal process to stop
        kill -TERM $(<"${GATEWAY_PID}") >/dev/null 2>&1

        if wait_for_pid; then
          exit 0
        fi
    else
        echo "Shutdown failed -- unable to determine process ID, use --force"
        exit 21
    fi

    echo "Shutdown failed -- must use -force"
    exit 21
  fi

  if [ $FORCE -eq 0 ]; then
    echo "Shutdown failed -- to forcibly terminate the process, use -force"
    exit 21
  fi

  if [ $FORCE -eq 1 ]; then
    if [ ! -z "$GATEWAY_PID" ]; then
       echo "Killing: `cat $GATEWAY_PID`"
       kill -9 `cat $GATEWAY_PID`
       exit 0
    else
       echo "Kill failed: \$GATEWAY_PID not set"
       exit 21;
    fi
  fi
elif [ -n "$1" ] ; then
  echo -e "\nInvalid command \"${1}\"\n"
  echoOptions
  exit 17
else
  echoOptions
  exit 17
fi
