#! /bin/sh

# Start script for lsyncd, to be used for local user
#
# Heavily inspired of corresponding SYSV init script 
# in debian package 

DAEMON=/usr/local/bin/lsyncd

test -x $DAEMON || exit 0

. /lib/lsb/init-functions

NAME=lsyncd
LSYNCD_HOME=$HOME/.lsyncd
PIDFILE=$LSYNCD_HOME/$NAME.pid
CONFFILE=$LSYNCD_HOME/lsyncd.lua

test -d $LSYNCD_HOME || mkdir -p $LSYNCD_HOME

set -e

log_daemon_msg "Starting lsyncd daemon" "lsyncd"
if [ -s $PIDFILE ] && kill -0 $(cat $PIDFILE) >/dev/null 2>&1; then
	log_progress_msg "apparently already running"
	log_end_msg 1
	exit 0
fi

if start-stop-daemon --start --quiet --user $USERNAME --pidfile $PIDFILE \
	--exec $DAEMON -- --pidfile $PIDFILE $CONFFILE; then
	rc=0
	sleep 1
	if ! kill -0 $(cat $PIDFILE) >/dev/null 2>&1; then
		log_failure_ms "lsyncd daemon failed to start"
		rc=1
	fi
else
	rc=1
fi
if [ $rc -eq 0 ]; then
	log_end_msg 0
else
	log_end_msg 1
	rm -f $PIDFILE
fi


