#!/bin/sh
#
#
# chkconfig: 2345 55 25
# description: Terracotta L2 server daemon                                                                                                              
# processname: terracotta
# pidfile: /var/run/terracotta.pid
#

SHUTDOWN_WAIT=30
JAVA_HOME=/usr/java/latest
JAVA_OPTS="-server -Xms256m -Xmx1024m -XX:NewRatio=3 -XX:MaxTenuringThreshold=15 -XX:+HeapDumpOnOutOfMemoryError"
TC_INSTALL_DIR=/opt/terracotta-3.1.0
TC_CONFIG=/etc/terracotta/tc-config.xml
TC_LOG=/var/log/terracotta/terracotta.out
TC_USER=tomcat
TC_NAME=linus




# Source function library.
if [ -f /etc/init.d/functions ]; then
  . /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ] ; then
  . /etc/rc.d/init.d/functions
else
  exit 0
fi

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

# check to see if tomcat is starting at boot time or manually
unset ISBOOT
if [ "${NAME:0:1}" = "S" -o "${NAME:0:1}" = "K" ]; then
    NAME="${NAME:3}"
    ISBOOT="1"
fi

# For SELinux we need to use 'runuser' not 'su'
if [ -x "/sbin/runuser" ]; then
    SU="/sbin/runuser"
else
    SU="/bin/su"
fi
 


# See how we were called.
function start() {
    echo -n "Starting Terracotta "
    if [ -f "/var/lock/subsys/terracotta" ] ; then
          if [ -f "/var/run/terracotta.pid" ]; then
            read kpid < /var/run/terracotta.pid
            if checkpid $kpid 2>&1; then
                echo_success
                echo
                return 0
            fi
        fi
    fi

    # fix permissions on the log and pid files
    TC_PID="/var/run/terracotta.pid"
    touch $TC_PID
    chown ${TC_USER}:${TC_USER} $TC_PID
    touch $TC_LOG
    chown ${TC_USER}:${TC_USER} $TC_LOG

    # determine the TC_SCRIPT to start the server
    TC_SCRIPT="${TC_INSTALL_DIR}/bin/start-tc-server.sh"

    # add required exports to the TC_SCRIPT
    TC_SCRIPT="export JAVA_OPTS=\"${JAVA_OPTS}\"; export JAVA_HOME=${JAVA_HOME}; export TC_PID=${TC_PID}; ${TC_SCRIPT}"

    # start terracotta
    $SU - $TC_USER -c "${TC_SCRIPT} -f ${TC_CONFIG} -name ${TC_NAME}" \
	>> $TC_LOG 2>&1

    RETVAL="$?"
    if [ "$RETVAL" -eq 0 ]; then
        echo_success
        touch /var/lock/subsys/terracotta
    else
        echo_failure
    fi
    echo
    return $RETVAL
}
 


function stop() {
    RETVAL="0"
    echo -n "Stopping Terracotta: "
    if [ -f "/var/lock/subsys/terracotta" ]; then

        $SU - $TOMCAT_USER -c "export JAVA_HOME=${JAVA_HOME}; ${TC_INSTALL_DIR}/bin/stop-tc-server.sh" \
	 >> $TC_LOG 2>&1    

        RETVAL="$?"
        if [ "$RETVAL" -eq "0" ]; then
            count="0"
            if [ -f "/var/run/terracotta.pid" ]; then
                read kpid < /var/run/terracotta.pid
                until [ "$(ps --pid $kpid | grep -c $kpid)" -eq "0" ] || \
                      [ "$count" -gt "$SHUTDOWN_WAIT" ]; do
                    if [ "$SHUTDOWN_VERBOSE" = "true" ]; then
                        echo "waiting for processes $kpid to exit"
                    fi
                    sleep 1
                    let count="${count}+1"
                done
                if [ "$count" -gt "$SHUTDOWN_WAIT" ]; then
                    if [ "$SHUTDOWN_VERBOSE" = "true" ]; then
                        echo "killing processes which didn't stop after $SHUTDOWN_WAIT seconds"
                    fi
                    kill -9 $kpid
                fi
                echo_success
            fi
            rm -f /var/lock/subsys/terracotta /var/run/terracotta.pid
        else
            echo_failure
        fi
    else
        echo_success
    fi
    echo
    return $RETVAL
}

case "$1" in
 start)
	start
	 ;;

 stop)
	stop
	 ;;
 restart)
        stop
        start
        ;;
 condrestart|try-restart)
        if [ -f "/var/run/terracotta.pid" ]; then
            stop
            start
        fi
        ;;
 status)
        if [ -f "/var/run/terracotta.pid" ]; then
            status terracotta
            RETVAL="$?"
        else
            pid="$(/usr/bin/pgrep -d , -u tomcat -G tomcat java)"
            if [ -z "$pid" ]; then
                status terracotta
                RETVAL="$?"
            else
                echo "terracotta (pid $pid) is running..."
                RETVAL="0"
            fi
        fi
        ;;
 *)
        echo -n "Usage: $0 {start|stop|restart|condrestart|try-restart|status}"
        RETVAL="2"      
esac

exit $RETVAL