#!/bin/sh

#
# SCM-Server start script
#

#
# MIT License
#
# Copyright (c) 2020-present Cloudogu GmbH and Contributors
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#

# chkconfig: 35 35 65
# description: SCM-Server
#
### BEGIN INIT INFO
# Provides:       scm-server
# Required-Start: $local_fs $remote_fs $network $time $named
# Required-Stop:  $local_fs $remote_fs $network $time $named
# Default-Start:  2 3 4 5
# Default-Stop:   0 1 6
# Description:    SCM-Server
### END INIT INFO

# start script is based on the one posted from JavaNode to SCM-Manager mailing 
# list: https://groups.google.com/d/msg/scmmanager/-wNjenUbl0Q/CkELJ6fLMHsJ


# Source function library.
if [ -x /etc/rc.d/init.d/functions ]; then
. /etc/rc.d/init.d/functions
fi

# Check for and source configuration file otherwise set defaults
RETVAL=0

appname=ScmServerDaemon

# See how we were called.
start() {
    if [ $(ps aux | grep java | grep ${appname} | wc -l) = 0 ]
    then
    	  echo "SCM-Server will now be started"
    	  /opt/scm-server/bin/scm-server start
    else
	  echo "SCM-Server already running"
	  status
    fi
}

stop() {
    if [ ! $(ps aux | grep java | grep ${appname} | wc -l) = 0 ]
    then
	  echo "SCM-Server will now be stopped"
          /opt/scm-server/bin/scm-server stop
    else
          echo "SCM-Server is not running"
    fi
}

status() {
    if [ "$(ps auxwww | grep java | grep ${appname} | awk '{ print $1 " PID:" $2 }')" ]; then 
	  echo "SCM-Server is running" 
	  ps auxwww | grep java | grep ${appname} | awk '{ print " PID: " $2 }'
    else 
	  echo "SCM-Server is not running"
    fi
}

restart() {
    stop
    SECONDS=0
    STAT=$( ps auxwww | grep  java | grep ${appname} |  wc -l )
    while [ $STAT -ne 0 ]
    do
      echo -n .
      sleep 3
        if [ $SECONDS -gt 300 ]
        then
          SCM_PID=$( ps auxwww | grep  java | grep ${appname} | awk '{ print $2 }' )
          kill -9 $SCM_PID
        fi
      STAT=$( ps auxwww | grep  java | grep ${appname} |  wc -l )
    done
    status
    start
    status
}

# See how we were called.

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        restart
        ;;
  status)
        status
        ;;
  *)
        echo "Usage: $0 {start|stop|restart}"
        exit 1
esac

exit $RETVAL
