#!/bin/bash
# Copyright (C) 2008-2018  Salvo "LtWorf" Tomaselli
#
# Weborf is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# author Salvo "LtWorf" Tomaselli <tiposchi@tiscali.it>


### BEGIN INIT INFO
# Provides:          weborf
# Required-Start:    $network $local_fs $syslog $remote_fs
# Required-Stop:     $remote_fs
# Should-Start:      $named
# Should-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Fast and small webserver
# Description:       Weborf is a configurationless webserver mainly meant to allow users
#                    to easily share their directories over the web.
#                    It also supports php5-cgi.
### END INIT INFO
function clear_cache {
    CDIR=`cat /etc/weborf.conf | egrep "^cachedir=" | cut -d= -f2`
    rm -rf $CDIR/*
}

function status {
    if ! test -e $PIDFILE
        then
            echo "Weborf doesn't appear to be running"
        else
            echo "Weborf appears to be running"
    fi
}

function stopWeborf {
    #Stops all the running weborf servers (the ones started with init)

    if ! test -e $PIDFILE
    then
        echo "Weborf is not running or it was not started by init"
        exit 0
    fi

    echo -n "Stopping weborf "
    for i in `cat $PIDFILE`
    do
        echo -n ..
        kill $i
    done
    echo "done"
    rm -f $PIDFILE
}

function startWeborf {
    #This function will start all the needed processes for weborf
    if test -e $PIDFILE
    then
        logger -s -perror "Weborf already running, if you're sure it is not running, delete $PIDFILE"
        exit 2
    fi

    logger -s "Starting weborf"
    nohup /usr/share/weborf/weborf_launcher /etc/weborf.conf >/dev/null 2> /dev/null &
    echo ${!} >> $PIDFILE # Writes PID so weborf can be terminated
}


PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/weborf                    # Introduce the server's location here
NAME=weborf                               # Introduce the short server's name here
PIDFILE=/var/run/$NAME.pid

test -x $DAEMON || exit 0

. /lib/lsb/init-functions

# Default options, these can be overriden by the information
# at /etc/default/$NAME
DAEMON_OPTS=""          # Additional options given to the server

DIETIME=3               # Time to wait for the server to die, in seconds
                        # If this value is set too low you might not
                        # let some servers to die gracefully and
                        # 'restart' will not work

#STARTTIME=2             # Time to wait for the server to start, in seconds
                        # If this value is set each time the server is
                        # started (on start or restart) the script will
                        # stall to try to determine if it is running
                        # If it is not set and the server takes time
                        # to setup a pid file the log message might
                        # be a false positive (says it did not start
                        # when it actually did)

case $1 in
    start)
        startWeborf;;
    stop)
        stopWeborf;;
    restart)
        stopWeborf
        startWeborf;;
    force-reload)
        stopWeborf
        startWeborf;;
    status)
        status;;
    clearcache)
        clear_cache;;
    * )
        echo "Usage: $0 {start|stop|restart|force-reload|status|clearcache}";;
esac
