#!/bin/bash
#
# chkconfig: - 85 15
# description: Lightweight inotify based sync daemon
#
# processname:  lsyncd
# config:       /etc/lsyncd.conf
# config:       /etc/sysconfig/lsyncd
# pidfile:      /var/run/lsyncd.pid
# Source function library

if [ -f /etc/init.d/functions ];then
  . /etc/init.d/functions
fi

if [ -f /lib/lsb/init-functions ];then
  . /lib/lsb/init-functions
fi

# Source networking configuration.
if [ -f /etc/sysconfig/network ];then
  . /etc/sysconfig/network
fi

LSYNCD_OPTIONS="-pidfile /var/run/lsyncd.pid {$SERVER_PATH}/rsyncd/lsyncd.conf"
if [ -e /etc/sysconfig/lsyncd ]; then
  . /etc/sysconfig/lsyncd
fi
RETVAL=0
prog="lsyncd"
thelock=/var/lock/subsys/lsyncd
LSYNCD_USER=root

start() {
    [ -f {$SERVER_PATH}/rsyncd/lsyncd.conf ] || exit 6
    echo -n $"Starting $prog: "
    if [ $UID -ne 0 ]; then
            RETVAL=1
            failure
    else
            nohup /usr/bin/lsyncd $LSYNCD_OPTIONS > /dev/null &
            RETVAL=$?
            [ $RETVAL -eq 0 ] && touch $thelock
    fi;
    echo
    return $RETVAL
}

stop() {
    echo -n $"Stopping $prog: "
    if [ $UID -ne 0 ]; then
            RETVAL=1
            failure
    else
            killproc lsyncd
            RETVAL=$?
            [ $RETVAL -eq 0 ] && rm -f $thelock
    fi;
    echo
    return $RETVAL
}

reload(){
    echo -n $"Reloading $prog: "
    killproc lsyncd -HUP
    RETVAL=$?
    echo
    return $RETVAL
}

restart(){
    stop
    start
}

condrestart(){
    [ -e $thelock ] && restart
    return 0
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;

  restart)
        restart
        ;;
  reload)
        reload
        ;;
  condrestart)
        condrestart
        ;;

  status)
        status lsyncd
        RETVAL=$?
        ;;
  *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|reload}"
        RETVAL=1
esac

exit $RETVAL
