#!/bin/bash
### BEGIN INIT INFO
# Provides:          selenium
# Required-Start:    $local_fs $network
# Required-Stop:     $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: selenium
# Description:       selenium test framework
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

DISPLAY_ID="{{ selenium_display_id }}"
RUN_AS=root

JAVA_BIN=/usr/bin/java

XVFB_BIN=/usr/bin/xvfb-run

SELENIUM_DIR={{ selenium_install_dir }}/selenium
SELENIUM_JAR_FILE="$SELENIUM_DIR/selenium-server-standalone-{{ selenium_version }}.jar"
DAEMON_PID_FILE="$SELENIUM_DIR/selenium.pid"
SELENIUM_LOG_FILE="$SELENIUM_DIR/selenium.log"
SELENIUM_DAEMON_OPTS=" -client -jar $SELENIUM_JAR_FILE -log $SELENIUM_LOG_FILE -port {{ selenium_port }}"

export DISPLAY="$DISPLAY_ID"

set -e

. /lib/lsb/init-functions

case "$1" in
    start)
        if status_of_proc -p $DAEMON_PID_FILE "$SELENIUM_JAR_FILE" $SELENIUM_JAR_FILE > /dev/null; then
            log_progress_msg "Service already running"
        else
            log_daemon_msg "Starting Selenium server"
            log_progress_msg "selenium"
            start-stop-daemon -c $RUN_AS --start --quiet --background --pidfile $DAEMON_PID_FILE --make-pidfile --exec $XVFB_BIN {{ selenium_xvfb_args }} $JAVA_BIN -- $SELENIUM_DAEMON_OPTS
        fi
        ;;

    stop)
        if status_of_proc -p $DAEMON_PID_FILE "$SELENIUM_JAR_FILE" $SELENIUM_JAR_FILE > /dev/null; then
            log_daemon_msg "Stopping Selenium server"
            log_progress_msg "selenium"
            DAEMON_PID=$(cat $DAEMON_PID_FILE)
            DAEMON_CHILDREN=$(pstree -l -p $DAEMON_PID |grep "([[:digit:]]*)" -o |tr -d '()')
            # Stop daemon itself.
            start-stop-daemon --stop --pidfile $DAEMON_PID_FILE
            # Stop all child processes.
            sudo kill $DAEMON_CHILDREN
        else
            log_progress_msg "Service not running"
        fi
        ;;

    restart|force-reload)
        $0 stop
        sleep 1
        $0 start
	;;

	status)
        status_of_proc -p $DAEMON_PID_FILE "$SELENIUM_JAR_FILE" $SELENIUM_JAR_FILE && exit 0 || exit $?
    ;;

    *)
      	N=/etc/init.d/selenium
        echo "Usage: $N {start|stop|restart|force-reload|status}" >&2
        exit 1
        ;;
esac
