#!/bin/bash

# Tomcat: Start/Stop Tomcat 7
#
# chkconfig: - 90 10
# description: Tomcat is a Java application Server.

JAVA_HOME=/usr/lib/jvm/java-8-oracle
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH
CATALINA_HOME=/usr/share/tomcat7
TOMCAT_USER=tomcat7

RETVAL=0
start(){
    echo "Starting Tomcat7: "
    iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080
    su - $TOMCAT_USER -c "$CATALINA_HOME/bin/startup.sh"
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ]
    return $RETVAL
}

stop(){
    echo "Shutting down Tomcat7: "
    $CATALINA_HOME/bin/shutdown.sh
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ]
    return $RETVAL
}

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

