export SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
export EXIT_CODE=0
function start_foreground() {
    export CATALINA_HOME="$SCRIPT_DIR/tomcat"
            cd "$SCRIPT_DIR/tomcat"
            ./bin/catalina.sh run "$@"
    export EXIT_CODE=$?
}
function start() {
    export CATALINA_PID=$SCRIPT_DIR/tomcat/bin/catalina.pid;
            cd "$SCRIPT_DIR/tomcat"
            ./bin/catalina.sh start "$@"
    export EXIT_CODE=$?
}
function stop() {
    cd "$SCRIPT_DIR/tomcat"
            ./bin/shutdown.sh; "$@"
    export EXIT_CODE=$?
}
function separator() {
     "$@"
    export EXIT_CODE=$?
}
function install_tomcat() {
    "$SCRIPT_DIR/bin/daemon/"install.sh "$@"
    export EXIT_CODE=$?
}
function remove_tomcat() {
    "$SCRIPT_DIR/bin/daemon/"remove.sh "$@"
    export EXIT_CODE=$?
}
#skip menu if arguments are passed
if [ -z "$1" ]; then
while [ answer != "0" ]
do
clear
cd "$SCRIPT_DIR"
echo  "Lucee Control Script"
echo "  usage: $(basename $0) [start-foreground|start|stop|install-tomcat|remove-tomcat]"
echo
echo "Select from the following functions"
echo
echo "  0  EXIT"
echo "  1  start-foreground (start server in foreground)"
echo "  2  start (start the server)"
echo "  3  stop (stop the server)"
echo "  ************************************************************"
echo "  5  install-tomcat (install the tomcat application server as seperate service)"
echo "  6  remove-tomcat (remove the tomcat application server as seperate service)"
echo
read -p "Option number: " answer
  case $answer in
    0) break ;;
    1) start_foreground
    ;;
    2) start
    ;;
    3) stop
    ;;
    4) separator
    ;;
    5) install_tomcat
    ;;
    6) remove_tomcat
    ;;
    *) break ;;
    esac
 echo "press RETURN for menu"
 read key
done
exit 0
fi
#arguments were passed in
if [ $1 = start-foreground ]; then
   start_foreground ${@:2}
   exit $EXIT_CODE
fi
if [ $1 = start ]; then
   start ${@:2}
   exit $EXIT_CODE
fi
if [ $1 = stop ]; then
   stop ${@:2}
   exit $EXIT_CODE
fi
if [ $1 = separator ]; then
   separator ${@:2}
   exit $EXIT_CODE
fi
if [ $1 = install-tomcat ]; then
   install_tomcat ${@:2}
   exit $EXIT_CODE
fi
if [ $1 = remove-tomcat ]; then
   remove_tomcat ${@:2}
   exit $EXIT_CODE
fi
