#!@l_prefix@/bin/openpkg rc
##
##  rc.varnish -- Run-Commands
##

%config
    varnish_enable="$openpkg_rc_def"
    varnish_conf_vcl="@l_prefix@/etc/varnish/varnish.vcl"
    varnish_conf_flags="-a 127.0.0.1:8080 -T 127.0.0.1:8079 -h critbit -s file,@l_prefix@/var/varnish/varnish.db,128M"

%common
    varnish_pidfile="@l_prefix@/var/varnish/varnish.pid"
    varnish_signal () {
        [ -f $varnish_pidfile ] && kill -$1 `cat $varnish_pidfile`
    }
    varnish_start () {
        @l_prefix@/sbin/varnishd \
            -u @l_nusr@ -g @l_ngrp@ \
            -P $varnish_pidfile \
            -f $varnish_conf_vcl \
            $varnish_conf_flags
    }

%status -u @l_susr@ -o
    varnish_usable="unknown"
    varnish_active="no"
    rcService varnish enable yes && \
        varnish_signal 0 && varnish_active="yes"
    echo "varnish_enable=\"$varnish_enable\""
    echo "varnish_usable=\"$varnish_usable\""
    echo "varnish_active=\"$varnish_active\""

%start -u @l_susr@
    rcService varnish enable yes || exit 0
    rcService varnish active yes && exit 0
    varnish_start

%stop -u @l_susr@
    rcService varnish enable yes || exit 0
    rcService varnish active no  && exit 0
    varnish_signal TERM
    sleep 2
    rm -f $varnish_pidfile 2>/dev/null || true

%restart -u @l_susr@
    rcService varnish enable yes || exit 0
    rcService varnish active no  && exit 0
    rc varnish stop start

