#!/bin/bash
### BEGIN INIT INFO
# Provides:       deki
# Required-Start: 
# Required-Stop:  
# Default-Start:  2 3 4 5
# Default-Stop:   0 1 6
# Short-Description: MindTouch API
# Description:    MindTouch API service
### END INIT INFO

prog="MindTouch Host Service"

DEKIHOST_CONF="/etc/dekiwiki/mindtouch.host.conf"
DEKIWIKI_USER="wwwrun"

if [ ! -f $DEKIHOST_CONF ]; then
    echo "$DEKIHOST_CONF does not exist"
    exit 1
fi

. $DEKIHOST_CONF

# Use the Mono extension if available
if [ -f /opt/novell/mono/bin/mono-addon-environment.sh ]
then
   source /opt/novell/mono/bin/mono-addon-environment.sh
   MONO=`which mono`
fi

# set default values if not specified in $DEKIHOST_CONF

if [ ! -f "$MONO" ]; then
    echo "Please specify the full path to your mono binary"
    exit 1
fi

if [ ! -d "$BIN_DIR" ]; then
    echo "Please specify the path to your MindTouch bin directory"
    exit 1
fi

if [ -z "$APIKEY" ]; then
    echo "Please specify your APIKEY"
    exit 1
fi

if [ -z "$PATH_PREFIX" ]; then
    PATH_PREFIX="@api"
fi

if [ -z "$HTTP_PORT" ]; then
    HTTP_PORT="8081"
fi

if [ -z "$IP" ]; then
    IP="localhost"
fi

if [ -z "$HOST_EXE" ]; then
    HOST_EXE="$BIN_DIR/mindtouch.host.exe"
fi

if [ -z "$SCRIPT" ]; then
    SCRIPT="/etc/dekiwiki/mindtouch.deki.startup.xml"
fi

if [ -z "$NOTTY" ]; then
    NOTTY="notty"
fi

if [ -z "$CONNECT_LIMIT" ]; then
    CONNECT_LIMIT="-5"
fi

if [ -z "$LOGDIR" ]; then
    LOGDIR="/srv/www/dekiwiki"
fi

if [ -z "$LOGFILE" ]; then
    LOGFILE="$LOGDIR/deki-api.log"
fi

if [ ! -z "$GUID" ]; then
    GUID="guid $GUID"
fi

if [ ! -z "$STORAGEDIR" ]; then
    STORAGEDIR="storage-dir $STORAGEDIR"
fi

pidof_dekiwiki() {
    # we grep on assembly name and port in case multiple mono processes are running
    echo $(ps -U $DEKIWIKI_USER -o pid,cmd | grep mindtouch.host.exe | grep $HTTP_PORT |awk '{print $1}')
}

stop() {
    echo -n "Stopping MindTouch API: mindtouch.host.exe" 
    PID=$(pidof_dekiwiki)
    if [ -z "$PID" ]; then
        echo
        echo "MindTouch is not running"
    else
        # attempt to shut down gracefully using curl
        curl -m 10 -s -d "" "http://$IP:$HTTP_PORT/host/?apikey=$APIKEY&dream.in.verb=DELETE"
        sleep 3
    
        # if the host didn't shut down properly then kill it
        PID=$(pidof_dekiwiki)
        if [ -n "$PID" ]; then
            killproc $PID
        fi
        echo .
    fi
}

start() {
    if [ ! -d $LOGDIR ]; then 
        mkdir $LOGDIR
        chown $DEKIWIKI_USER $LOGDIR
    fi

    PID=$(pidof_dekiwiki)
    if [ -n "$PID" ]; then
        echo "MindTouch is already running: $PID"
        exit 1;
    fi
    MONO_ARGS="$HOST_EXE apikey $APIKEY script $SCRIPT path-prefix $PATH_PREFIX http-port $HTTP_PORT ip $IP connect-limit $CONNECT_LIMIT $NOTTY $GUID $STORAGEDIR"
    echo -n "Starting MindTouch API: mindtouch.host.exe"
    #startproc -u $DEKIWIKI_USER $MONO $MONO_ARGS >> $LOGFILE 2>&1 
    su -s /bin/bash -c "exec $MONO $MONO_ARGS " $DEKIWIKI_USER >> $LOGFILE 2>&1 &
    echo .
}

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
    PID=$(pidof_dekiwiki)
    if [ -n "$PID" ]; then
        echo "MindTouch is running: $PID"
    else
        echo "MindTouch is stopped"
    fi
    ;;
  restart|force-reload)
    stop
    start
    ;;
  force-reload)
    stop
    start
    ;;
  *)
    echo "Usage: $0 {start|stop|force-reload|restart|status}"
    exit 1
esac

exit 0
