#!/bin/sh

# This stuff will be ignored by systems that don't use chkconfig.
# chkconfig: 345 98 13
# description: scheduler-launcher

# config:

#chmod a+x /etc/init.d/easy-launcher
#chkconfig --add easy-launcher


### BEGIN INIT INFO
# Provides:         easy-launcher
# Required-Start:
# Required-Stop:
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: EasyLauncher
# Description:       EasyLauncher service
### END INIT INFO

# Starts and stops the EasyLauncher

# Some general variables

HOME=/opt/easyportal/launcher
VERSION=*

export JAVA_HOME=/usr/lib/jvm/jre-17-openjdk
#. /etc/profile.d/easydata.sh

# starts EasyLauncher
start () {
     if [ ! -z  "$(ps ax | grep -v grep | grep java | grep launcher-${VERSION}.jar | awk ' {print $1}')" ] ; then
        echo "Runtime service is still running"
        exit 1
     fi
        cd ${HOME}
        sudo -E -u easyportal bash -c "nohup $JAVA_HOME/bin/java -Xmx100m -jar ./launcher-${VERSION}.jar > ./log/start.log 2>&1 &"
        touch /var/lock/subsys/easy-launcher
}


# stops EasyLauncher
stop () {
if [ ! -z  "$(ps ax | grep -v grep | grep java | grep launcher-${VERSION}.jar | awk ' {print $1}')" ] ; then
         PID=$(ps ax | grep -v grep | grep java | grep launcher-${VERSION}.jar | awk ' {print $1}')
         kill -TERM ${PID}
         echo SIGTERM sent to process ${PID}
         rm /var/lock/subsys/easy-launcher
     else
         echo Process easy-launcher not found!
     fi
}


case "$1" in
    init)
      start
      ;;
    start)
      start
      ;;
    stop)
      stop
      ;;
    zap)
     stop
      ;;
    restart)
      stop
      sleep 5
      start
      ;;
    *)
        echo "Usage: /etc/init.d/easy-launcher {start|stop}"
        exit 1
        ;;
esac

exit 0
