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

%config
    git_lfs_enable="$openpkg_rc_def"
    git_lfs_daemon="no"
    git_lfs_flags=""
    git_lfs_log_prolog="true"
    git_lfs_log_epilog="true"
    git_lfs_log_numfiles="10"
    git_lfs_log_minsize="1M"
    git_lfs_log_complevel="9"

%common
    git_lfs_cfgfile="@l_prefix@/etc/git-lfs/git-lfs-server.cf"
    git_lfs_pidfile="@l_prefix@/var/git-lfs/run/git-lfs-server.pid"
    git_lfs_logfile="@l_prefix@/var/git-lfs/log/git-lfs-server.log"
    git_lfs_signal () {
        [ -f $git_lfs_pidfile ] && kill -$1 `cat $git_lfs_pidfile`
    }

%status -u @l_rusr@ -o
    git_lfs_usable="unknown"
    git_lfs_active="no"
    rcService git-lfs enable yes && \
        git_lfs_signal 0 && git_lfs_active="yes"
    echo "git_lfs_enable=\"$git_lfs_enable\""
    echo "git_lfs_usable=\"$git_lfs_usable\""
    echo "git_lfs_active=\"$git_lfs_active\""

%start -u @l_rusr@
    rcService git-lfs enable yes || exit 0
    rcService git-lfs active yes && exit 0
    rcVarIsYes git_lfs_daemon || exit 0
    (   cd @l_prefix@/var/git-lfs/data
        . @l_prefix@/etc/git-lfs/git-lfs-server.cf
        export LFS_LISTEN LFS_HOST LFS_CONTENTPATH LFS_ADMINUSER LFS_ADMINPASS LFS_CERT LFS_KEY LFS_SCHEME
        nohup @l_prefix@/sbin/git-lfs-server $git_lfs_flags \
        </dev/null >>$git_lfs_logfile 2>&1 &
        echo $! >$git_lfs_pidfile
    ) >/dev/null 2>&1

%stop -u @l_rusr@
    rcService git-lfs enable yes || exit 0
    rcService git-lfs active no  && exit 0
    rcVarIsYes git_lfs_daemon || exit 0
    git_lfs_signal TERM
    sleep 2
    rm -f $git_lfs_pidfile >/dev/null 2>&1 || true

%restart -u @l_rusr@
    rcService git-lfs enable yes || exit 0
    rcService git-lfs active no  && exit 0
    rcVarIsYes git_lfs_daemon || exit 0
    rc git-lfs stop start

%daily -u @l_rusr@
    rcService git-lfs enable yes || exit 0
    rcVarIsYes git_lfs_daemon || exit 0
    shtool rotate -f \
        -n ${git_lfs_log_numfiles} -s ${git_lfs_log_minsize} -d \
        -z ${git_lfs_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${git_lfs_log_prolog}" \
        -E "${git_lfs_log_epilog}; rc git-lfs restart" \
        $git_lfs_logfile

