#!/bin/bash
#
# Startup script for Artifactory in Tomcat Servlet Engine
#
# chkconfig: 345 86 14
# description: Artifactory Tomcat Servlet Engine
# processname: artifactory
# pidfile: /var/lib/artifactory/run/artifactory.pid
#
### BEGIN INIT INFO
# Provides:          artifactory
# Required-Start:    $remote_fs $syslog $network
# Required-Stop:     $remote_fs $syslog $network
# Default-Start:     3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start Artifactory on Tomcat
# Description:       Manages the services needed to run Artifactory on a dedicated Tomcat- a Maven Proxy
### END INIT INFO
#

# Read default Artifactory environment variables from etc
. /etc/artifactory/default

if [ -z "$ARTIFACTORY_HOME" ] || [ ! -d "$ARTIFACTORY_HOME" ]; then
    echo "ERROR: Artifactory home folder not defined or does not exists at $ARTIFACTORY_HOME"
    exit 1
fi

if [ -z "$TOMCAT_HOME" ] || [ ! -d "$TOMCAT_HOME" ]; then
    echo "ERROR: Tomcat Artifactory folder not defined or does not exists at $TOMCAT_HOME"
    exit 1
fi

# Basic variables used
CATALINA_MGNT_PORT=8015
CATALINA_PID_FOLDER=/var/lib/artifactory/run
CATALINA_LOCK_FILE=$CATALINA_PID_FOLDER/lock
CATALINA_PID=$CATALINA_PID_FOLDER/artifactory.pid

# User under which tomcat will run
if [ -z "$ARTIFACTORY_USER" ]; then
    # Will run as current user (may be root!!!)
    ARTIFACTORY_USER=$USER
fi

if [ ! -d "$CATALINA_PID_FOLDER" ]; then
    mkdir "$CATALINA_PID_FOLDER" || exit $?
fi

chown $ARTIFACTORY_USER "$CATALINA_PID_FOLDER" || exit $?

RETVAL=0

findShutdownPort() {
    SHUTDOWN_PORT=`netstat -vatn|grep LISTEN|grep $CATALINA_MGNT_PORT|wc -l`
}

isAlive() {
	pidValue=""
	javaPs=""
	if [ -e "/opt/tomcat/hudson/hudson-tomcat.pid" ]; then
		pidValue=`cat /opt/tomcat/hudson/hudson-tomcat.pid`
		if [ -n "$pidValue" ]; then
			javaPs=`ps -p $pidValue | grep java`
		fi
	fi
}

# start, debug, stop, and status functions
start() {
    # Start Tomcat in normal mode
	isAlive
    findShutdownPort
	if [ $SHUTDOWN_PORT -ne 0 ] || [ -n "$javaPs" ]; then
        echo "Artifactory Tomcat already started"
    else
        echo "Starting Artifactory tomcat as $ARTIFACTORY_USER..."
        noFileVal=`ulimit -n`
        minNoFileMax=32000
        if [ "$noFileVal" != "unlimited" ] && [ $noFileVal -lt $minNoFileMax ]; then
            ulimit -n $minNoFileMax || echo "WARNING: Max number of open files $noFileVal is too small!
You should add:
artifactory soft nofile $minNoFileMax
artifactory hard nofile $minNoFileMax
to your /etc/security/limits.conf file."
        fi
        su -l $ARTIFACTORY_USER -c "$TOMCAT_HOME/bin/startup.sh"
        RETVAL=$?
        if [ $RETVAL -ne 0 ]; then
            echo "ERROR: Artifactory Tomcat server did not start. Please check the logs"
            exit 1
        fi
        findShutdownPort
        nbSeconds=1
        while [ $SHUTDOWN_PORT -eq 0 ] && [ $nbSeconds -lt 45 ]; do
            sleep 1
            let "nbSeconds = $nbSeconds + 1"
            findShutdownPort
        done
        if [ $SHUTDOWN_PORT -eq 0 ]; then
            echo "ERROR: Artifactory Tomcat server did not start in 45 seconds. Please check the logs"
            exit 1
        fi
        echo "Artifactory Tomcat started in normal mode"
		[ $RETVAL=0 ] && touch ${CATALINA_LOCK_FILE}
    fi
}

stop() {
	isAlive
    findShutdownPort
	if [ $SHUTDOWN_PORT -eq 0 ] && [ -z "$javaPs" ]; then
        echo "Artifactory Tomcat already stopped"
        RETVAL=0
    else
        echo "Stopping Artifactory Tomcat..."
		if [ $SHUTDOWN_PORT -ne 0 ]; then
            su -l $ARTIFACTORY_USER -c "$TOMCAT_HOME/bin/shutdown.sh"
            RETVAL=$?
		else
			RETVAL=1
		fi
        killed=false
        if [ $RETVAL -ne 0 ]; then
            echo "WARN: Artifactory Tomcat server shutdown script failed. Sending kill signal to $pidValue"
            if [ -n "$pidValue" ]; then
                killed=true
                kill $pidValue
                RETVAL=$?
            fi
        fi
		# Wait 2 seconds for process to die
		sleep 2
        findShutdownPort
		isAlive
        nbSeconds=1
		while [ $SHUTDOWN_PORT -ne 0 ] || [ -n "$javaPs" ] && [ $nbSeconds -lt 30 ]; do
			if [ $nbSeconds -eq 10 ] && [ -n "$pidValue" ]; then
				# After 10 seconds try to kill the process
                echo "WARN: Artifactory Tomcat server shutdown not done after 10 seconds. Sending kill signal"
                kill $pidValue
                RETVAL=$?
            fi
            if [ $nbSeconds -eq 25 ] && [ -n "$pidValue" ]; then
                # After 25 seconds try to kill -9 the process
                echo "WARN: Artifactory Tomcat server shutdown not done after 25 seconds. Sending kill -9 signal"
                kill -9 $pidValue
                RETVAL=$?
            fi
            sleep 1
            let "nbSeconds = $nbSeconds + 1"
            findShutdownPort
        done
		if [ $SHUTDOWN_PORT -eq 0 ] && [ -z "$javaPs" ]; then
           echo "Artifactory Tomcat stopped"
        else
           echo "ERROR: Artifactory Tomcat did not stop"
           RETVAL=1
        fi
    fi
    [ $RETVAL=0 ] && rm -f "$CATALINA_LOCK_FILE" "$CATALINA_PID"
}

status() {
    findShutdownPort
    if [ $SHUTDOWN_PORT -eq 0 ]; then
        echo "Artifactory Tomcat stopped"
    else
        echo "Artifactory Tomcat running"
    fi
}

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

exit $RETVAL
