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

%config
    docker_gen_enable="$openpkg_rc_def"
    docker_gen_flags="-endpoint tcp://127.0.0.1:2375"
    docker_gen_log_prolog="true"
    docker_gen_log_epilog="true"
    docker_gen_log_numfiles="10"
    docker_gen_log_minsize="1M"
    docker_gen_log_complevel="9"

%common
    docker_gen_cfgfile="@l_prefix@/etc/docker-gen/docker-gen.cfg"
    docker_gen_pidfile="@l_prefix@/var/docker-gen/run/docker-gen.pid"
    docker_gen_logfile="@l_prefix@/var/docker-gen/log/docker-gen.log"
    docker_gen_signal () {
        [ -f $docker_gen_pidfile ] && kill -$1 `cat $docker_gen_pidfile`
    }

%status -u @l_susr@ -o
    docker_gen_usable="unknown"
    docker_gen_active="no"
    rcService docker_gen enable yes && \
        docker_gen_signal 0 && docker_gen_active="yes"
    echo "docker_gen_enable=\"$docker_gen_enable\""
    echo "docker_gen_usable=\"$docker_gen_usable\""
    echo "docker_gen_active=\"$docker_gen_active\""

%start -p 150 -u @l_susr@
    rcService docker-gen enable yes || exit 0
    rcService docker-gen active yes && exit 0
    (   nohup @l_prefix@/sbin/docker-gen \
           -config "$docker_gen_cfgfile" \
           $docker_gen_flags \
        </dev/null >>$docker_gen_logfile 2>&1 &
        echo $! >$docker_gen_pidfile
    ) >/dev/null 2>&1
    sleep 2

%stop -p 850 -u @l_susr@
    rcService docker-gen enable yes || exit 0
    rcService docker-gen active no  && exit 0
    docker_gen_signal TERM
    sleep 2
    rm -f $docker_gen_pidfile >/dev/null 2>&1 || true

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

%daily -u @l_susr@
    rcService docker-gen enable yes || exit 0
    shtool rotate -f \
        -n ${docker_gen_log_numfiles} -s ${docker_gen_log_minsize} -d \
        -z ${docker_gen_log_complevel} -m 664 -o @l_susr@ -g @l_sgrp@ \
        -P "${docker_gen_log_prolog}" \
        -E "${docker_gen_log_epilog}; rc docker-gen restart" \
        $docker_gen_logfile

