#!/bin/sh

JENKINS_WAR="/usr/lib/cloudbees-core-oc/cloudbees-core-oc.war"
test -r "$JENKINS_WAR" || { echo "$JENKINS_WAR not installed";
        if [ "$1" = "stop" ]; then exit 0;
        else exit 5; fi; }

# Check for existence of needed config file and read it
JENKINS_CONFIG=/etc/sysconfig/cloudbees-core-oc
test -e "$JENKINS_CONFIG" || { echo "$JENKINS_CONFIG not existing";
        if [ "$1" = "stop" ]; then exit 0;
        else exit 6; fi; }
test -r "$JENKINS_CONFIG" || { echo "$JENKINS_CONFIG not readable. Perhaps you forgot 'sudo'?";
        if [ "$1" = "stop" ]; then exit 0;
        else exit 6; fi; }

# Read config
if [ -f "$JENKINS_CONFIG" ]; then
    set -a
    . "$JENKINS_CONFIG"
    set +a
fi

# Set up environment accordingly to the configuration settings
[ -n "$JENKINS_HOME" ] || { echo "JENKINS_HOME not configured in $JENKINS_CONFIG";
        if [ "$1" = "stop" ]; then exit 0;
        else exit 6; fi; }
[ -d "$JENKINS_HOME" ] || { echo "JENKINS_HOME directory does not exist: $JENKINS_HOME";
        if [ "$1" = "stop" ]; then exit 0;
        else exit 1; fi; }

# Search usable Java as /usr/bin/java might not point to minimal version required by Jenkins.
# see http://www.nabble.com/guinea-pigs-wanted-----Hudson-RPM-for-RedHat-Linux-td25673707.html
candidates="
/etc/alternatives/java
/usr/lib/jvm/java-1.8.0/bin/java
/usr/lib/jvm/jre-1.8.0/bin/java
/usr/lib/jvm/java-11.0/bin/java
/usr/lib/jvm/jre-11.0/bin/java
/usr/lib/jvm/java-11-openjdk-amd64
/usr/lib/jvm/java-17.0/bin/java
/usr/lib/jvm/jre-17.0/bin/java
/usr/lib/jvm/java-17-openjdk-amd64
/usr/bin/java
"
for candidate in $candidates
do
  [ -x "$JENKINS_JAVA_CMD" ] && break
  JENKINS_JAVA_CMD="$candidate"
done

PARAMS=("--logfile=/var/log/cloudbees-core-oc/cloudbees-core-oc.log")
PARAMS+=("--webroot=/var/cache/cloudbees-core-oc/war")
[ -n "$JENKINS_PORT" ] && PARAMS+=("--httpPort=$JENKINS_PORT")
[ -n "$JENKINS_LISTEN_ADDRESS" ] && PARAMS+=("--httpListenAddress=$JENKINS_LISTEN_ADDRESS")
[ -n "$JENKINS_HTTPS_PORT" ] && PARAMS+=("--httpsPort=$JENKINS_HTTPS_PORT")
[ -n "$JENKINS_HTTPS_KEYSTORE" ] && PARAMS+=("--httpsKeyStore=$JENKINS_HTTPS_KEYSTORE")
[ -n "$JENKINS_HTTPS_KEYSTORE_PASSWORD" ] && PARAMS+=("--httpsKeyStorePassword=$JENKINS_HTTPS_KEYSTORE_PASSWORD")
[ -n "$JENKINS_HTTPS_LISTEN_ADDRESS" ] && PARAMS+=("--httpsListenAddress=$JENKINS_HTTPS_LISTEN_ADDRESS")
[ -n "$JENKINS_DEBUG_LEVEL" ] && PARAMS+=("--debug=$JENKINS_DEBUG_LEVEL")
[ -n "$JENKINS_HANDLER_STARTUP" ] && PARAMS+=("--handlerCountStartup=$JENKINS_HANDLER_STARTUP")

if [ "$JENKINS_ENABLE_ACCESS_LOG" = "yes" ]; then
    PARAMS+=("--accessLoggerClassName=winstone.accesslog.SimpleAccessLogger" "--simpleAccessLogger.format=combined" "--simpleAccessLogger.file=/var/log/cloudbees-core-oc/access_log")
fi

tmpdir=$(mktemp -d)
trap "rm -rf $tmpdir" EXIT

DEFAULT_JENKINS_JAVA_OPTIONS=("-Dcb.distributable.name=RedHat / Fedora RPM")
DEFAULT_JENKINS_JAVA_OPTIONS+=("-Dcb.distributable.commit_sha=856ff1f1c7f496506ca4a7592a94a454f36dd1d9")
DEFAULT_JENKINS_JAVA_OPTIONS+=("-Djava.io.tmpdir=$tmpdir")


if [[ "$(declare -p JENKINS_JAVA_OPTIONS)" =~ "declare -a" ]]; then
  set -x
  $JENKINS_JAVA_CMD "${DEFAULT_JENKINS_JAVA_OPTIONS[@]}" "${JENKINS_JAVA_OPTIONS[@]}" -jar $JENKINS_WAR ${PARAMS[@]} $JENKINS_ARGS
else
  set -x
  $JENKINS_JAVA_CMD "${DEFAULT_JENKINS_JAVA_OPTIONS[@]}" $JENKINS_JAVA_OPTIONS -jar $JENKINS_WAR ${PARAMS[@]} $JENKINS_ARGS
fi