#!/bin/sh
### BEGIN INIT INFO
# Required-Start:       $local_fs $remote_fs $network
# Default-Start:        3 5
# Default-Stop:         0 1 2 6
# Short-Description:    Loggerhead
# Description:          Manage Loggerhead (a web viewer for projects in bazaar)
### END INIT INFO


#
# Configure this please:
# (Please stop loggerhead before changing the configuration, otherwise this
#   script might not be able to kill loggerhead)
#

LHUSER=loggerhead

if [ `whoami` = "$LHUSER" ]; then
    SUDO=""
else
    SUDO="sudo -H -u $LHUSER"
fi

# If loggerhead-serve is not in your path, you will need to specify the full path:
SERVE_BRANCHES_CMD=loggerhead-serve

LOG_FOLDER=/var/log/loggerhead
LOG_FILE=$LOG_FOLDER/loggerheadd.log
URL_PREFIX=/loggerhead
PORT=8080

#please specify the base directory to serve:
BZRROOT=/bzrroot

# You can add additional options to loggerhead-serve here:
START_CMD="$SERVE_BRANCHES_CMD --prefix=$URL_PREFIX --log-folder=$LOG_FOLDER --port=$PORT $BZRROOT"


#
# main part
#

loggerhead_process(){
    $SUDO pgrep -fl "$START_CMD"
}

loggerhead_status(){
    proccess=`loggerhead_process`
    #echo "$proccess"
    listening=`netstat -nl |grep -e ":$PORT "`
    #echo "$listening"
    if [ -z "$proccess" ]; then
        echo "Loggerhead is *not* running."
    else
        echo "Loggerhead is running."
        if [ -z "$listening" ]; then
            echo "This server is *not* listening on port $PORT."
        else
            echo "This server is listening on port $PORT."
        fi
    fi
}

start_loggerhead(){
    echo "Starting loggerhead.   (See $LOG_FOLDER for details.)"

    # make sure the log folder is created
    if [ ! -d $LOG_FOLDER ]
    then
        $SUDO mkdir -p $LOG_FOLDER
    fi
    echo "" > $LOG_FILE
    $SUDO python3 $START_CMD > $LOG_FILE 2>&1 &

    #wait a little while of some logging to appear
    log=""
    for i in $(seq 1 3 30); do
        log=`cat $LOG_FILE`
        if [ -n "$log" ]; then
            break
        fi
        sleep 0.3
    done
    tail $LOG_FILE
    loggerhead_status
}

stop_loggerhead(){
    echo "Stopping loggerhead."
    $SUDO pkill -f "$START_CMD"
    loggerhead_status
}

case "$1" in
    start)
        start_loggerhead
    ;;
    stop)
        stop_loggerhead
    ;;
    status)
        loggerhead_status
    ;;
    restart)
        stop_loggerhead
        start_loggerhead
    ;;
    *)
        echo "Usage: loggerheadd { start | stop | status | restart }"
        exit 1
esac
