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

%config
    viewmtn_enable="$openpkg_rc_def"
    viewmtn_bind="127.0.0.1:8080"
    viewmtn_graphcache="10000"
    viewmtn_log_prolog="true"
    viewmtn_log_epilog="true"
    viewmtn_log_numfiles="10"
    viewmtn_log_minsize="1M"
    viewmtn_log_complevel="9"

%common
    viewmtn_pidfile="@l_prefix@/var/viewmtn/viewmtn.pid"
    viewmtn_signal () {
        [ -f $viewmtn_pidfile ] && kill -$1 `cat $viewmtn_pidfile`
    }

%status -u @l_rusr@ -o
    viewmtn_usable="unknown"
    viewmtn_active="no"
    rcService viewmtn enable yes && \
        viewmtn_signal 0 && viewmtn_active="yes"
    echo "viewmtn_enable=\"$viewmtn_enable\""
    echo "viewmtn_usable=\"$viewmtn_usable\""
    echo "viewmtn_active=\"$viewmtn_active\""

%start -u @l_rusr@
    rcService viewmtn enable yes || exit 0
    rcService viewmtn active yes && exit 0
    ( cd @l_prefix@/libexec/viewmtn
      PATH="@l_prefix@/bin:/bin:/usr/bin:/sbin:/usr/sbin"
      export PATH
      nohup ./viewmtn.py ${viewmtn_bind} \
          </dev/null >/dev/null 2>&1 &
      echo $! >$viewmtn_pidfile
    ) >/dev/null 2>&1

%stop -u @l_rusr@
    rcService viewmtn enable yes || exit 0
    rcService viewmtn active no  && exit 0
    viewmtn_signal TERM
    sleep 2
    rm -f $viewmtn_pidfile >/dev/null 2>&1 || true

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

%daily -u @l_rusr@
    rcService viewmtn enable yes || exit 0
    ( cd @l_prefix@/var/viewmtn/graph || exit $?
      days=100
      while [ $days -gt 0 ]; do
          kb=`(du -sk | awk '{ print $1; }') 2>/dev/null`
          if [ ".$kb" != . ]; then
              if [ $kb -lt $viewmtn_graphcache ]; then
                  break
              fi
              find . -mtime +$days -print | xargs rm -f 2>/dev/null || true
          fi
          days=`expr $days - 1`
      done
      rmdir ./* 2>/dev/null || true
    ) || exit $?

