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

%config
    wildfly_enable="$openpkg_rc_def"

%common
    wildfly_bindir="@l_prefix@/libexec/wildfly/bin"
    wildfly_pidfile="@l_prefix@/var/wildfly/run/wildfly.pid"
    wildfly_signal () {
        [ -f $wildfly_pidfile ] && kill -$1 `cat $wildfly_pidfile`
    }
    wildfly_start () {
        eval `JAVA_PLATFORM="sun-jdk" @l_prefix@/bin/java-toolkit -e`
        export JBOSS_HOME="@l_prefix@/libexec/wildfly"
        export JBOSS_BASE_DIR="@l_prefix@/var/wildfly"
        export JBOSS_LOG_DIR="@l_prefix@/var/wildfly/log"
        export JBOSS_CONFIG_DIR="@l_prefix@/etc/wildfly"
        export JBOSS_PIDFILE="$wildfly_pidfile"
        export LAUNCH_JBOSS_IN_BACKGROUND=true
        (   nohup $wildfly_bindir/standalone.sh \
            </dev/null >/dev/null 2>&1 &
        ) >/dev/null 2>&1
    }
    wildfly_stop () {
        eval `JAVA_PLATFORM="sun-jdk" @l_prefix@/bin/java-toolkit -e`
        export JBOSS_HOME="@l_prefix@/libexec/wildfly"
        export JBOSS_BASE_DIR="@l_prefix@/var/wildfly"
        export JBOSS_LOG_DIR="@l_prefix@/var/wildfly/log"
        export JBOSS_CONFIG_DIR="@l_prefix@/etc/wildfly"
        $wildfly_bindir/jboss-cli.sh --connect command=":shutdown"
        sleep 2
        wildfly_signal TERM
        sleep 2
        rm -f $wildfly_pidfile >/dev/null 2>&1 || true
    }

%status -u @l_nusr@ -o
    wildfly_usable="unknown"
    wildfly_active="no"
    rcService wildfly enable yes && \
        wildfly_signal 0 && wildfly_active="yes"
    echo "wildfly_enable=\"$wildfly_enable\""
    echo "wildfly_usable=\"$wildfly_usable\""
    echo "wildfly_active=\"$wildfly_active\""

%start -u @l_nusr@
    rcService wildfly enable yes || exit 0
    rcService wildfly active yes && exit 0
    wildfly_start

%stop -u @l_nusr@
    rcService wildfly enable yes || exit 0
    rcService wildfly active no  && exit 0
    wildfly_stop

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

