#!/bin/sh
### BEGIN INIT INFO
# Provides: resilio-agent
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Should-Start: $network
# Should-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: daemonized version of resilio-agent.
# Description: Starts the resilio-agent daemon.
### END INIT INFO


DAEMON=/usr/bin/rslagent
SYNC_USER=rslagent
CONFIG=/etc/resilio-agent/sync.conf
STORAGE=/var/lib/resilio-agent


. /lib/lsb/init-functions


wait_process()
{
    while kill -0 "$1" 2> /dev/null; do
        sleep 0.5
    done
}


start()
{
    echo "Starting Resilio Connect Agent"
    mkdir -p $STORAGE
    chown -R $SYNC_USER:$SYNC_USER $STORAGE
    start-stop-daemon --start --quiet -b -o -c $SYNC_USER -u $SYNC_USER --exec $DAEMON --umask 0002 -- --config $CONFIG --storage $STORAGE
}


stop()
{
    start-stop-daemon --stop -o -c $SYNC_USER -K -u $SYNC_USER -x $DAEMON
    wait_process $(pidof $DAEMON)
}

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart|reload|force-reload)
  	stop
  	start
    ;;
  *)
    echo "Usage: /etc/init.d/resilio-agent {start|stop|restart}"
    exit 1
    ;;
esac

exit 0
