#!/bin/sh
#
# Author: Sergii Bratchenko / Sonja Neubauer
#
# /etc/init.d/tunnelbroker
#
### BEGIN INIT INFO
# Provides: tunnelbroker
# Required-Start: 
# Required-Stop: 
# Default-Start:  3 4 5
# Default-Stop:   0 1 2 6
# Short-Description: tunnelbroker
# Description: tunnelbroker service 
### END INIT INFO

webservice="webservice"
webserviceDir="/opt/webservice"
serviceName="tunnelbroker"   
pidFile="/var$webserviceDir/$serviceName.pid"
#serviceLogFile="/var/log/$webservice/service-$serviceName.log"
logFile="/var/log/$webservice/$serviceName.log"
maxShutdownTime=10
maxKillTime=3

JRE_HOME=`${webserviceDir}/scripts/getJavaPath.sh tunnelbroker`

export JRE_HOME
export JAVA_HOME=$JRE_HOME

javaOptions="-Xmx128m -XX:MaxPermSize=40m"
javaStart="$JAVA_HOME/bin/java $javaOptions -jar $webserviceDir/$serviceName/$serviceName.jar"


function doFileWritable {
    local filename="$1"
    touch $filename || return 1
    return 0
}
 
function checkProcess {
    local pid="$1"

    if [ -z "$pid" -o "$pid" == " " ]; then 
        return 1 
    fi

    if [ ! -e /proc/$pid ]; then
        return 1
    fi
    grep -q --binary -F "$serviceName" /proc/$pid/cmdline
    if [ $? -ne 0 ]; then
      return 1;
    fi
    return 0
} 
   
function getServicePid {
    if [ ! -f $pidFile ]; then 
        return 1 
    fi
    servicePid="$(<$pidFile)"
    checkProcess $servicePid || return 1
    return 0 
}   


function setLogFilePermission {
    if [ -f $logFile ]; then
        chmod 640 $logFile >/dev/null 2>&1
        chmod 640 $logFile.[0-9]* >/dev/null 2>&1
    fi
    umask 0026
}

function startServiceProcess {
    rm -f $pidFile
    doFileWritable $pidFile || return 1
    #doFileWritable $serviceLogFile || return 1
    
    chrt -o -p 0 $$
    #local cmd="setsid $javaStart >>$serviceLogFile 2>&1 & echo \$! >$pidFile"
    local cmd="setsid $javaStart >> $logFile 2>&1 & echo \$! >$pidFile"
    $SHELL -c "$cmd" || return 1

    sleep 1
    servicePid="$(<$pidFile)"
    checkProcess $servicePid
    if [ $? -ne 0 ]; then
        echo -ne "\n$serviceName start failed, see logfile."
        return 1
    fi
    return 0 
}  

function stopServiceProcess {
  
    kill $servicePid || return 1
    for ((i=0; i<maxShutdownTime*10; i++)); do
        checkProcess $servicePid
        if [ $? -ne 0 ]; then
            rm -f $pidFile
            return 0
        fi
        sleep 0.1
    done
    echo -e "\n$serviceName did not terminate within $maxShutdownTime seconds, sending SIGKILL..."
    kill -9 $servicePid || return 1
    for ((i=0; i<maxKillTime*10; i++)); do
        checkProcess $servicePid
        if [ $? -ne 0 ]; then
            rm -f $pidFile
            return 0
        fi
        sleep 0.1
    done    
    echo "Error: $serviceName could not be stopped within $maxShutdownTime+$maxKillTime seconds!"
    return 1; 
}

function startService {
    getServicePid
    if [ $? -eq 0 ]; then
        echo -n "$serviceName is already running" 
        rc_failed 0 
        rc_status -v 
        return 0
    fi
    echo -n "Starting $serviceName  "
    logger -t tunnelbroker "using JRE_HOME=$JRE_HOME for tunnelbroker startup"

    # ensure that the security jce jar files exists and are appropriate
    if [ -x /opt/soco/ibm_jce_policy-install.sh ]; then
      /opt/soco/ibm_jce_policy-install.sh $JRE_HOME
    else
      err_text="Error: Softgate not installed, therefore can not deploy java security policy files, tunnelbroker may fail at startup"
      echo ${err_text}
      logger -t tunnelbroker ${err_text}
    fi
                  
    setLogFilePermission
    startServiceProcess
    if [ $? -ne 0 ]; then 
        rc_failed 1 
        rc_status -v 
        return 1 
    fi
    rc_failed 0
    rc_status -v
    return 0
}

function stopService {
    getServicePid
    if [ $? -ne 0 ]; then 
        echo -n "$serviceName is not running" 
        rc_failed 0 
        rc_status -v 
        return 0 
    fi
    echo -n "Stopping $serviceName  "
    stopServiceProcess
    if [ $? -ne 0 ]; then 
        rc_failed 1
        rc_status -v
        return 1
    fi
    rc_failed 0
    rc_status -v
    return 0
}  

function checkServiceStatus {
    echo -n "Checking for $serviceName:  "
    if getServicePid; then
        rc_failed 0
    else
        rc_failed 3
    fi
    rc_status -v
    return 0; 
}

. /etc/rc.status

#[OSFOURK-23199] DTB is not starting in V10R1.35
# fake tunnelbroker status in SLES15 V10R1 - this enables us to easily revert the change in case we do want the tunnelbroker back and of course we fix/adapt the real issue why it is not working on SLES15
fake_status_file=/dev/shm/fake.tunnelbroker.on

rc_reset
case "$1" in
    start)
        logger -t tunnelbroker "received service command: $1"

        #[OSFOURK-23199] DTB is not starting in V10R1.35
        touch $fake_status_file
        #some dummy process to fork for just a little while
        sleep 3 &
        exit 0

        startService
        ;;
    stop)                                                
        logger -t tunnelbroker "received service command: $1"

        #[OSFOURK-23199] DTB is not starting in V10R1.35
        rm -f $fake_status_file
        exit 0

        stopService
        ;;
    restart|force-reload)
        logger -t tunnelbroker "received service command: $1"

        #[OSFOURK-23199] DTB is not starting in V10R1.35
        touch $fake_status_file
        #some dummy process to fork for just a little while
        sleep 3 &
        exit 0

        stopService && startService
        ;;
    status)                                              

        #[OSFOURK-23199] DTB is not starting in V10R1.35
        [ -e $fake_status_file ] && exit 0
        #not used in case flag file not present
        exit 3

        #checkServiceStatus

        ;;
    *)
        echo "Usage: service $serviceName {start|stop|status|restart|force-reload}"
        exit 1
        ;;
esac
rc_exit 

