#!/bin/sh
#  /etc/init.d/antmedia

### BEGIN INIT INFO
# Provides:          AntMedia
# Required-Start:    $remote_fs $syslog $network
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Starts the ant media service
# Description:       This file is used to start the daemon and should be placed in /etc/init.d
# chkconfig: 2345 85 85
# processname: red5
### END INIT INFO

# Author:   Sheldon Neilson <sheldon[AT]neilson.co.za>
# Url:      www.neilson.co.za
# Date:     25/04/2013
# Modified by Paul Gregoire <mondain[AT]gmail.com> on 18 May 2016

NAME="antmedia"
DESC="Ant Media Service"

# The path to Jsvc
EXEC="/usr/bin/jsvc"

# The path to the folder containing daemon jar
FILE_PATH="/usr/local/$NAME"
# If red5 home is set, use it
if [ ! -z "$RED5_HOME" ]; then 
  echo "Setting file path using RED5_HOME"
  FILE_PATH=$RED5_HOME;
fi
export RED5_HOME=$FILE_PATH;
echo "Path $FILE_PATH";

# The path to the folder containing the java runtime
JAVA_HOME="/usr/lib/jvm/java-8-oracle"

# Our classpath including our jar file and the Apache Commons Daemon library
CLASS_PATH="$FILE_PATH/commons-daemon-1.0.15.jar:$FILE_PATH/ant-media-server-service.jar:$FILE_PATH/conf"

# The fully qualified name of the class to execute
CLASS="org.red5.daemon.EngineLauncher"

# Any command line arguments to be passed to the our Java Daemon implementations init() method 
ARGS="9999"

# The file that will contain our process identification number (pid) for other scripts/programs that need to access it.
PID="/tmp/$NAME.pid"

# System.out writes to this file...
LOG_OUT="$FILE_PATH/log/$NAME-service.log"

# System.err writes to this file...
LOG_ERR="$FILE_PATH/log/$NAME-error.log"

# JAVA options
# You can set JVM additional options here if you want
if [ -z "$JVM_OPTS" ]; then 
    JVM_OPTS="-Xverify:none -XX:+TieredCompilation -XX:+UseBiasedLocking -XX:InitialCodeCacheSize=8m -XX:ReservedCodeCacheSize=32m -Dorg.terracotta.quartz.skipUpdateCheck=true -XX:+UseG1GC"
fi

OS=`uname`
case "$OS" in
  CYGWIN*|MINGW*) # Windows Cygwin or Windows MinGW
  P=";" # Since these are actually Windows, let Java know
  ;;
  Linux*)
      LD_LIBRARY_PATH=$FILE_PATH/lib/native
      export LD_LIBRARY_PATH
      # Native path
      NATIVE="-Djava.library.path=$LD_LIBRARY_PATH"
  ;;
  Darwin*)
      DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:$FILE_PATH/lib/native
      export DYLD_LIBRARY_PATH
      # Native path
      NATIVE="-Djava.library.path=$DYLD_LIBRARY_PATH"
  ;;
  SunOS*)
      if [ -z "$JAVA_HOME" ]; then 
          export JAVA_HOME=/opt/local/java/sun6; 
      fi
  ;;
  *)
  # Do nothing
  ;;
esac




# Set up logging options
LOGGING_OPTS="-Dlogback.ContextSelector=org.red5.logging.LoggingContextSelector -Dcatalina.useNaming=true"
# Set up security options
SECURITY_OPTS="-Djava.security.debug=failure -Djava.security.egd=file:/dev/./urandom"
# Set up tomcat options
TOMCAT_OPTS="-Dcatalina.home=$RED5_HOME"

ADDITIONAL_OPTS="-Djava.library.path=$RED5_HOME/lib/native"

JAVA_OPTS="$LOGGING_OPTS $SECURITY_OPTS $JAVA_OPTS $JVM_OPTS $NATIVE $TOMCAT_OPTS $ADDITIONAL_OPTS"

jsvc_exec()
{   
    cd $FILE_PATH
    $EXEC -home $JAVA_HOME -user antmedia -cp $CLASS_PATH -cwd $RED5_HOME $JAVA_OPTS -errfile $LOG_ERR -umask $(printf '%d' 133) -pidfile $PID $1 $CLASS $ARGS
}

case "$1" in
    start)  
        echo "Starting the $DESC..."        
        
        #define ulimit
        ulimit -n 65536
        
        # Start the service
        jsvc_exec
        
        echo "The $DESC has started."
    ;;
    stop)
        echo "Stopping the $DESC..."
        
        # Stop the service
        jsvc_exec "-stop"       
        
        echo "The $DESC has stopped."
    ;;
    restart)
        if [ -f "$PID" ]; then
            
            echo "Restarting the $DESC..."
            
            # Stop the service
            jsvc_exec "-stop"
            
            # Start the service
            jsvc_exec
            
            echo "The $DESC has restarted."
        else
            echo "Daemon not running, no action taken"
            exit 1
        fi
            ;;
    *)
    echo "Usage: /etc/init.d/$NAME {start|stop|restart}" >&2
    exit 3
    ;;
esac
