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

%config
    monotone_enable="$openpkg_rc_def"
    monotone_daemon="no"
    monotone_daemon_host="127.0.0.1"
    monotone_daemon_port="4691"
    monotone_daemon_dbfile="@l_prefix@/var/monotone/monotone.db"
    monotone_log_prolog="true"
    monotone_log_epilog="true"
    monotone_log_numfiles="10"
    monotone_log_minsize="1M"
    monotone_log_complevel="9"

%common
    monotone_daemon_etcdir="@l_prefix@/etc/monotone"
    monotone_daemon_pidfile="@l_prefix@/var/monotone/monotone-server.pid"
    monotone_daemon_logfile="@l_prefix@/var/monotone/monotone-server.log"
    monotone_signal () {
        [ -f $monotone_daemon_pidfile ] && kill -$1 `cat $monotone_daemon_pidfile`
    }

%status -u @l_rusr@ -o
    monotone_usable="unknown"
    monotone_active="no"
    rcService monotone enable yes && \
        monotone_signal 0 && monotone_active="yes"
    echo "monotone_enable=\"$monotone_enable\""
    echo "monotone_usable=\"$monotone_usable\""
    echo "monotone_active=\"$monotone_active\""

%start -u @l_rusr@
    rcService monotone enable yes || exit 0
    rcService monotone active yes && exit 0
    if rcVarIsYes monotone_daemon; then
        ( HOME="$monotone_daemon_etcdir/monotone-server"
          export HOME
          nohup @l_prefix@/bin/mtn \
            --confdir="$monotone_daemon_etcdir/monotone-server" \
            --keydir="$monotone_daemon_etcdir//monotone-server/keys" \
            --log="$monotone_daemon_logfile" \
            --db="$monotone_daemon_dbfile" \
            serve \
            --bind="$monotone_daemon_host:$monotone_daemon_port" \
            --pid-file="$monotone_daemon_pidfile" &
        ) >/dev/null 2>&1
    fi

%stop -u @l_rusr@
    rcService monotone enable yes || exit 0
    rcService monotone active no  && exit 0
    if rcVarIsYes monotone_daemon; then
        monotone_signal TERM
        sleep 2
        rm -f $monotone_daemon_pidfile 2>/dev/null || true
    fi

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

%daily -u @l_rusr@
    rcService monotone enable yes || exit 0
    shtool rotate -f \
        -n $monotone_log_numfiles -s $monotone_log_minsize -d \
        -z $monotone_log_complevel -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "$monotone_log_prolog" \
        -E "$monotone_log_epilog" \
        $monotone_daemon_logfile

