#! /bin/sh

### BEGIN INIT INFO
# Provides:          unity3d-cacheserver
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Should-Start:      $named
# Default-Start:     2 3 4 5
# Default-Stop:      
# Short-Description: Unity Asset Cache Server
# Description:       The Unity Asset Cache Server drastically reduces the time it takes to import assets when working in large teams. It makes it easier to control the schedule of complex, multi-team projects. When working in large teams it is unavoidable that everyone has to repeatedly import the same assets. The Unity Asset Cache Server facilitates a more smooth and time saving sharing of asset imports between projects and folders. Switching between different platforms takes minutes instead of hours on large scale projects, and, once you set it up, the Asset Cache Server works away in the background to reduce wait times, without getting in your way.
### END INIT INFO

set -e

# /etc/init.d/unity3d-cacheserver: start and stop the Unity asset cache server

DAEMON=/usr/bin/CacheServer
CACHESERVER_ENABLE=true
CACHESERVER_OPTS=''
CACHESERVER_PID_FILE=/var/run/unity3d-cacheserver.pid

test -x $DAEMON || exit 0

. /lib/lsb/init-functions

export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"

cacheserver_start() {
    if start-stop-daemon --start --quiet --background \
        --pidfile $CACHESERVER_PID_FILE --make-pidfile \
        --exec $DAEMON \
        -- $CACHESERVER_OPTS
    then
        rc=0
        sleep 1
        if ! kill -0 $(cat $CACHESERVER_PID_FILE) >/dev/null 2>&1; then
            log_failure_msg "unity3d-cacheserver 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 $CACHESERVER_PID_FILE
    fi
} # cacheserver_start


case "$1" in
  start)
	if "$CACHESERVER_ENABLE"; then
	    log_daemon_msg "Starting unity3d-cacheserver" "unity3d-cacheserver"
	    if [ -s $CACHESERVER_PID_FILE ] && kill -0 $(cat $CACHESERVER_PID_FILE) >/dev/null 2>&1; then
		log_progress_msg "apparently already running"
		log_end_msg 0
		exit 0
	    fi
            cacheserver_start
        else
            if [ -s "$CACHESERVER_CONFIG_FILE" ]; then
                [ "$VERBOSE" != no ] && log_warning_msg "unity3d-cacheserver not enabled, not starting..."
            fi
	fi
	;;
  stop)
	log_daemon_msg "Stopping unity3d-cacheserver" "unity3d-cacheserver"
	start-stop-daemon --stop --quiet --oknodo --pidfile $CACHESERVER_PID_FILE
	log_end_msg $?
	rm -f $CACHESERVER_PID_FILE
	;;

  restart|reload|force-reload)
	set +e
	if $CACHESERVER_ENABLE; then
	    log_daemon_msg "Restarting unity3d-cacheserver" "unity3d-cacheserver"
	    if [ -s $CACHESERVER_PID_FILE ] && kill -0 $(cat $CACHESERVER_PID_FILE) >/dev/null 2>&1; then
		start-stop-daemon --stop --quiet --oknodo --pidfile $CACHESERVER_PID_FILE || true
		sleep 1
	    else
		log_warning_msg "unity3d-cacheserver not running, attempting to start."
	    	rm -f $CACHESERVER_PID_FILE
	    fi
            cacheserver_start
        else
	    log_warning_msg "unity3d-cacheserver daemon not enabled in $CACHESERVER_DEFAULTS_FILE, not starting..."
	fi
	;;

  status)
	status_of_proc -p $CACHESERVER_PID_FILE "$DAEMON" unity3d-cacheserver
	exit $?	# notreached due to set -e
	;;
  *)
	echo "Usage: /etc/init.d/unity3d-cacheserver {start|stop|reload|force-reload|restart|status}"
	exit 1
esac

exit 0
