#!/bin/sh

BASE="/var/packages/elephantdrive/target"

CONFIG_DIR="$BASE/conf/"

SYM_WEB="/var/services/web/elephantdrive"

# PATH TO PID FILE
PID_FILE="/var/run/elephantdriveDaemon.pid"

LOG_FILE="/var/log/elephantdrive.log"

start_service ()
{
    if [ -f ${PID_FILE} ] && [ -d /proc/`cat ${PID_FILE}` ]; then
        return 1
    fi
    # Launch the server in the background.
    ${BASE}/bin/elephantdriveDaemon -l -c ${CONFIG_DIR} MonitorConfigChanges=true > /dev/null 2>&1 &
    echo $! > $PID_FILE 

    return 0
}

stop_service ()
{
    # Kill elephantdriveDaemon
    if [ -f "$PID_FILE" ]
    then
        kill `cat "$PID_FILE"`
        rm -f $PID_FILE
    fi    
}

status_service ()
{
    if [ -f ${PID_FILE} ] && [ -d /proc/`cat ${PID_FILE}` ]; then
        return 0
    fi
    if [ -f ${PID_FILE} ]; then
        return 1
    fi
    return 3
}

case $1 in
    start)
        start_service
        echo "Running ElephantDrive"
        exit 0
        ;;
    stop)
        stop_service
        echo "Stopped ElephantDrive"
        exit 0
        ;;
    status)
        if status_service
        then
            echo "ElephantDrive is running"
            exit 0
        else
            echo "ElephantDrive is not running"
            exit 1
        fi
        ;;

    log)
        if [ -f $LOG_FILE ];
        then
           echo $LOG_FILE
        fi
        exit 0
        ;;
    *)
        exit 1
        ;;
esac
