#! /bin/sh

### BEGIN INIT INFO
# Provides:          vinyl
# Required-Start:    $local_fs $remote_fs $network
# Required-Stop:     $local_fs $remote_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start HTTP accelerator
# Description:       This script provides a server-side cache
#                    to be run in front of a httpd and should
#                    listen on port 80 on a properly configured
#                    system
### END INIT INFO

# Source function library
. /lib/lsb/init-functions

SERVICE=vinyld
DESC="HTTP accelerator"
PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/vinyld
PIDFILE=/run/$SERVICE.pid

test -x $DAEMON || { echo "${DAEMON} has no execute bit"; exit 0; }

# Include vinyl defaults if available
if [ -f /etc/default/vinyl ] ; then
        . /etc/default/vinyl
fi

# Open files (usually 1024, which is way too small for vinyl)
ulimit -n ${NFILES:-131072}

# Maxiumum locked memory size for shared memory log
ulimit -l ${MEMLOCK:-82000}

# If $DAEMON_OPTS is not set at all in /etc/default/vinyl, use minimal useful
# defaults (Backend at localhost:8080, a common place to put a locally
# installed application server.)
DAEMON_OPTS=${DAEMON_OPTS:--b localhost}

# Ensure we have a PATH
export PATH="${PATH:+$PATH:}/usr/sbin:/usr/bin:/sbin:/bin"

start_vinyld() {
    log_daemon_msg "Starting $DESC" "$SERVICE"
    output=$(/bin/tempfile -s.vinyl)
    if start-stop-daemon \
        --start --pidfile ${PIDFILE} --exec ${DAEMON} -- \
        ${DAEMON_OPTS} -P ${PIDFILE} > ${output} 2>&1; then
        log_end_msg 0
    else
        log_end_msg 1
        cat $output
        exit 1
    fi
    rm $output
}

disabled_vinyld() {
    log_daemon_msg "Not starting $DESC" "$SERVICE"
    log_progress_msg "disabled in /etc/default/vinyl"
    log_end_msg 0
}

stop_vinyld() {
    log_daemon_msg "Stopping $DESC" "$SERVICE"
    if start-stop-daemon \
        --stop --pidfile $PIDFILE --retry 10 \
        --exec $DAEMON; then
        log_end_msg 0
    else
        log_end_msg 1
    fi

    if test -r $PIDFILE; then
        read -r PID < $PIDFILE
        if test ! -d /proc/$PID ; then
            # stale pidfile
            unset PID
            rm -f $PIDFILE
        fi
    fi
}

reload_vinyld() {
    log_daemon_msg "Reloading $DESC" "$SERVICE"
    # Workaround for the reload script to make it work with instances
    export DAEMON_OPTS
    if /usr/share/vinyl/reload-vcl -q; then
        log_end_msg 0
    else
        log_end_msg 1
    fi
}

status_vinyld() {
    start-stop-daemon \
        --status --pidfile $PIDFILE \
        --exec $DAEMON
    exit $?
}

configtest() {
	log_daemon_msg "Checking syntax" "$SERVICE"

	$DAEMON ${DAEMON_OPTS} -C -n /tmp 1>/dev/null 2>&1
	local ret=${?:-1}
	log_end_msg $ret
	if [ $ret != 0 ]; then
		$DAEMON ${DAEMON_OPTS} -C -n /tmp
	fi

	return $ret
}


case "$1" in
    start)
        case "${START:-}" in
            [Yy]es|[Yy]|1|[Tt]|[Tt]rue)
                start_vinyld
                ;;
            *)
                disabled_vinyld
                ;;
        esac
        ;;
    stop)
        stop_vinyld
        ;;
    reload)
        reload_vinyld
        ;;
    status)
        status_vinyld
        ;;
    restart|force-reload)
        $0 stop
        $0 start
        ;;
    configtest)
        configtest
        ;;
    *)
        log_success_msg "Usage: $0 {start|stop|restart|reload|force-reload|configtest}"
        exit 1
        ;;
esac
