#!/bin/bash
#
# chkconfig: 345 85 15
# description: Urbackup server daemon
# config: /etc/sysconfig/urbackup-server
#

### BEGIN INIT INFO
# Provides: urbackup-server
# Required-Start: $local_fs $network $remote_fs
# Required-Stop: $local_fs $network
# Default-Start: 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start and stop URbackup server
# Description: URBackup Server does backups
#              with special client software

### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions

if [ -x /usr/sbin/start_urbackup_server ]; then
    exec=start_urbackup_server
else
    exit 5
fi

prog=urbackup_srv

if [ -f /etc/sysconfig/urbackup-server ]
then
    . /etc/sysconfig/urbackup-server
fi

lockfile=/var/lock/subsys/urbackup-server

if [ "x$SQLITE_TMPDIR" != "x" ]
then
        SQLITE_TMPDIR="--sqlite_tmpdir \"$SQLITE_TMPDIR\""
fi

if [ "x$BROADCAST_INTERFACES" != "x" ]
then
        BROADCAST_INTERFACES="--broadcast_interfaces \"$BROADCAST_INTERFACES\""
fi

DAEMON_TMPDIR="$DAEMON_TMPDIR/urbackup_tmp"

if [ ! -d "$DAEMON_TMPDIR" ]
then
	mkdir $DAEMON_TMPDIR > /dev/null 2>&1
	chown urbackup:urbackup $DAEMON_TMPDIR
fi


urbdargs="--fastcgi_port $FASTCGI_PORT --logfile $LOGFILE --loglevel $LOGLEVEL --http_port $HTTP_PORT $SQLITE_TMPDIR $BROADCAST_INTERFACES"

start()
{
    echo -n $"Starting Urbackup server: "
    export TMPDIR=$DAEMON_TMPDIR
    daemon $exec $urbdargs
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    ulimit -n 10000 > /dev/null 2>&1
    return $retval
}

stop()
{
    echo -n $"Shutting down Urbackup server: "
    killproc $prog
    retval=$?
    echo
    [ $retval -eq 0 ] && ( rm -f $lockfile ; rm -Rf $DAEMON_TMPDIR > /dev/null 2>&1 )
    return $retval
}

restart()
{
    stop
    start
}

case "$1" in
    start|stop|restart)
        $1
        ;;
    force-reload)
        restart
        ;;
    status)
        status $prog
        ;;
    try-restart|condrestart)
        if status $prog >/dev/null ; then
            restart
        fi
        ;;
    reload)
        action $"Service ${0##*/} does not support the reload action: " /bin/false
        exit 3
        ;;
    *)
	echo $"Usage: $0 {start|stop|status|restart|try-restart|force-reload}"
	exit 2
	;;
esac
