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

%config
    docker_distribution_enable="$openpkg_rc_def"
    docker_distribution_flags=""
    docker_distribution_log_prolog="true"
    docker_distribution_log_epilog="true"
    docker_distribution_log_numfiles="10"
    docker_distribution_log_minsize="1M"
    docker_distribution_log_complevel="9"

%common
    docker_distribution_cfgfile="@l_prefix@/etc/docker-distribution/docker-registry.yml"
    docker_distribution_pidfile="@l_prefix@/var/docker-distribution/run/docker-registry.pid"
    docker_distribution_logfile="@l_prefix@/var/docker-distribution/log/docker-registry.log"
    docker_distribution_signal () {
        [ -f $docker_distribution_pidfile ] && kill -$1 `cat $docker_distribution_pidfile`
    }

%status -u @l_rusr@ -o
    docker_distribution_usable="unknown"
    docker_distribution_active="no"
    rcService docker-distribution enable yes && \
        docker_distribution_signal 0 && docker_distribution_active="yes"
    echo "docker_distribution_enable=\"$docker_distribution_enable\""
    echo "docker_distribution_usable=\"$docker_distribution_usable\""
    echo "docker_distribution_active=\"$docker_distribution_active\""

%start -u @l_rusr@
    rcService docker-distribution enable yes || exit 0
    rcService docker-distribution active yes && exit 0
    (   nohup @l_prefix@/sbin/docker-registry \
           $docker_distribution_flags \
           $docker_distribution_cfgfile \
        </dev/null >>$docker_distribution_logfile 2>&1 &
        echo $! >$docker_distribution_pidfile
    ) >/dev/null 2>&1

%stop -u @l_rusr@
    rcService docker-distribution enable yes || exit 0
    rcService docker-distribution active no  && exit 0
    docker_distribution_signal TERM
    sleep 2
    rm -f $docker_distribution_pidfile >/dev/null 2>&1 || true

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

%daily -u @l_rusr@
    rcService docker-distribution enable yes || exit 0
    shtool rotate -f \
        -n ${docker_distribution_log_numfiles} -s ${docker_distribution_log_minsize} -d \
        -z ${docker_distribution_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${docker_distribution_log_prolog}" \
        -E "${docker_distribution_log_epilog}; rc docker-distribution restart" \
        $docker_distribution_logfile

