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

### BEGIN INIT INFO
# Provides:          urbackup-client
# Required-Start:    $network $local_fs $remote_fs
# Required-Stop:     $remote_fs $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Daemon process for UrBackup Client
# Description:       This software does backups
#                    with special client software
#                    
### END INIT INFO

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

DESC="UrBackup Client Daemon"             # Introduce a short description here
NAME="urbackup-client"             # Introduce the short server's name here
PREFIX="/usr"
DAEMON=$PREFIX/bin/start_urbackup_client # Introduce the server's location here
DAEMON_REAL=$PREFIX/bin/urbackup_client
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
DAEMON_TMPDIR=/tmp
NICE_LEVEL="0"
IOSCHED_CLASS="best-effort"
prog=urbackup_client


if [ -x $DAEMON ]; then
    exec=start_urbackup_client
else
    exit 5
fi

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

#Default options that are passed to the Daemon.

#logfile name
LOGFILE="urbackup_client.log"

#Either debug,warn,info or error
LOGLEVEL="error"

#Tmp file directory - be carefull this can get very large
DAEMON_TMPDIR="/tmp"

# Read configuration variable file if it is present
if [ -r /etc/sysconfig/$NAME ] && . /etc/sysconfig/$NAME

DAEMON_ARGS="--logfile $LOGFILE --loglevel $LOGLEVEL"


# Define LSB log_* functions.
# Depend on redhat-lsb-core to ensure that this file is present.
. /lib/lsb/init-functions

DAEMON_TMPDIR="$DAEMON_TMPDIR/urbackup_client_tmp"

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


# Function that starts the daemon/service
start()
{
    echo -n $"Starting Urbackup client: "
    export TMPDIR=$DAEMON_TMPDIR
    daemon $exec $DAEMON_ARGS
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    ulimit -n 10000 > /dev/null 2>&1
    return $retval
}

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

restart()
{
    echo -n $"Restarting $DESC" "$NAME"
    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

exit $retval