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

%config
    jenkins_enable="$openpkg_rc_def"
    jenkins_host="127.0.0.1"
    jenkins_port="8080"
    jenkins_tmpdir="@l_prefix@/var/jenkins/tmp"
    jenkins_wrkdir="@l_prefix@/var/jenkins/wrk"
    jenkins_flags=""
    jenkins_log_prolog="true"
    jenkins_log_epilog="true"
    jenkins_log_numfiles="10"
    jenkins_log_minsize="1M"
    jenkins_log_complevel="9"

%common
    jenkins_pidfile="@l_prefix@/var/jenkins/run/jenkins.pid"
    jenkins_logfile="@l_prefix@/var/jenkins/run/jenkins.log"
    jenkins_signal () {
        [ -f $jenkins_pidfile ] && kill -$1 `cat $jenkins_pidfile`
    }

%status -u @l_rusr@ -o
    jenkins_usable="unknown"
    jenkins_active="no"
    rcService jenkins enable yes && \
        jenkins_signal 0 && jenkins_active="yes"
    echo "jenkins_enable=\"$jenkins_enable\""
    echo "jenkins_usable=\"$jenkins_usable\""
    echo "jenkins_active=\"$jenkins_active\""

%start -u @l_rusr@
    rcService jenkins enable yes || exit 0
    rcService jenkins active yes && exit 0
    cmd="HOME=\"$jenkins_tmpdir\";"
    cmd="$cmd export HOME;"
    cmd="$cmd exec @l_prefix@/bin/java"
    cmd="$cmd -Vsun-jdk"
    cmd="$cmd -Djava.io.tmpdir=\"$jenkins_tmpdir\""
    cmd="$cmd -DJENKINS_HOME=\"$jenkins_wrkdir\""
    cmd="$cmd -jar @l_prefix@/lib/jenkins/jenkins.war"
    cmd="$cmd --httpListenAddress=\"$jenkins_host\""
    cmd="$cmd --httpPort=\"$jenkins_port\""
    cmd="$cmd --httpsPort=\"-1\""
    cmd="$cmd --ajp13Port=\"-1\""
    cmd="$cmd $jenkins_flags"
    ( eval "nohup $cmd </dev/null >>$jenkins_logfile 2>&1 &"
      echo $! >$jenkins_pidfile
    ) >/dev/null 2>&1

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

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

%daily -u @l_susr@
    rcService jenkins enable yes || exit 0
    shtool rotate -f \
        -n ${jenkins_log_numfiles} -s ${jenkins_log_minsize} -d \
        -z ${jenkins_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${jenkins_log_prolog}" \
        -E "${jenkins_log_epilog}; rc jenkins restart" \
        $jenkins_logfile

