#!/bin/sh
#
# clsync    live sync tool based on inotify
#
# chkconfig: 345 98 02
# description: live sync tool based on inotify
# processname: clsync
# config: /etc/clsync/clsync.conf
# pidfile: /var/run/clsync.pid
#
### BEGIN INIT INFO
# Provides:          clsync
# Should-Start:      $remote_fs $local_fs $syslog
# Should-Stop:       $remote_fs $local_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: clsync daemon init script
# Description:       This script launches the clsync daemon.
### END INIT INFO

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

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

SourceIfNotEmpty /etc/sysconfig/clsync

MYNAME="${0/*\//}"
MYBLOCK="${MYNAME#clsync-}"

PIDFILE=/var/run/clsync-${MYBLOCK}.pid
LOCKFILE=/var/lock/subsys/clsync-${MYBLOCK}

start()
{
    local blockarg
    [[ -n "$MYBLOCK" ]] && blockarg="--config-block $MYBLOCK"
    start_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" -- \
        clsync --background --output=syslog --pid-file "$PIDFILE" \
        --config-file /etc/clsync/clsync.conf $blockarg $CLSYNC_ARGS
    RETVAL=$?
    return $RETVAL
}

stop()
{
    stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" -- clsync
    RETVAL=$?
    return $RETVAL
}

restart()
{
    stop
    start
}

send_signal()
{
    echo -n "$2: "
    stop_daemon --no-announce --pidfile "$PIDFILE" -$1 -- clsync
    RETVAL=$?
    return $RETVAL
} 

help() {
	cat <<- EOF
	Usage: ${0##*/}[-BLOCK] ACTION
	ACTION  requested action on clsync daemon
	BLOCK   config file block to read from

	You may run different clsync instances using different blocks
	of the same config by creating symlink to clsync init.d script
	in format clsync-BLOCK, e.g. clsync-home will use [home] block
	of the config file.

	Default config is /etc/clsync/clsync.conf, you may override it
	using --config-file option via CLSYNC_ARGS variable in the
	/etc/sysconfig/clsync file. You may provide any other clsync
	options there; though all clsync options may be provided via
	config file using different blocks if necessary.

	Actions with standard behaviour:
	start        start clsync
	stop         stop clsync
	restart      restart clsync
	condstop     stop clsync if running
	condrestart  restart clsync if running
	status       query current clsync status

	Actions with clsync-specific behaviour, they make sence only if
	clsync is already running, will be noop otherwise:
	reload       reload filter rules without clsync restart
	resync       start full resync (aka initial sync)
	gc           run threads garbage collection
	kill         kill clsync, but keep children running
	quit         gracefully quit after current sync operation will
	             be finished; makes sense only with --sync-on-quit
	             option, clsync will exit immediately otherwise
	help         show this help message
	EOF
	RETVAL=1
}

# See how we were called.
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        restart
        ;;
    condstop)
        if [ -e "$LOCKFILE" ]; then
            stop
        fi
        ;;
    condrestart)
        if [ -e "$LOCKFILE" ]; then
            restart
        fi
        ;;
    status)
        status --pidfile "$PIDFILE" -- clsync
        RETVAL=$?
        ;;
    reload)
        send_signal 1 "reloading filter rules"
        ;;
    resync)
        send_signal 12 "running full resync"
        ;;
    gc)
        send_signal 10 "running threads GC"
        ;;
    kill)
        send_signal 9 "killing clsync with children left"
        ;;
    quit)
        send_signal 3 "will exit after current sync is finished (if --sync-on-quit is enabled, immediately otherwise)"
        ;;
    help)
        help
        ;;
    *)
        msg_usage "${0##*/} {start|stop|restart|condstop|condrestart|status|reload|resync|gc|kill|quit|help}"
        RETVAL=1
esac

exit $RETVAL
