##
##  wildfly.spec -- OpenPKG RPM Package Specification
##  Copyright (c) 2000-2025 OpenPKG Project <http://openpkg.org/>
##
##  Permission to use, copy, modify, and distribute this software for
##  any purpose with or without fee is hereby granted, provided that
##  the above copyright notice and this permission notice appear in all
##  copies.
##
##  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
##  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
##  SUCH DAMAGE.
##

#   package version
%define       V_opkg 37.0.1
%define       V_dist 37.0.1.Final

#   package information
Name:         wildfly
Summary:      Open Source Java Application Server
URL:          http://wildfly.org/
Vendor:       Red Hat, Inc.
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Web
License:      LGPL
Version:      %{V_opkg}
Release:      20250905

#   list of sources
Source0:      https://github.com/wildfly/wildfly/releases/download/%{V_dist}/wildfly-%{V_dist}.tar.gz
Source1:      rc.wildfly

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  java, JAVA-JDK
PreReq:       java, JAVA-JDK

%description
    WildFly (formerly JBOSS AS) is the leading Open Source,
    standards-compliant, Java EE based application server.

%track
    prog wildfly = {
        version   = %{V_dist}
        url       = http://wildfly.org/downloads/
        regex     = wildfly-(\d+\.\d+\.\d+(\.Final)?)\.tar\.gz
    }

%prep
    %setup -q -n wildfly-%{V_dist}

%build
    #   remove unnecessary stuff
    rm -rf domain
    rm -rf docs
    rm -f  bin/domain*
    rm -rf bin/service
    rm -rf bin/init.d
    rm -f  bin/*.bat
    rm -f  *.txt

%install
    #   create install directories
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/sbin \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d \
        $RPM_BUILD_ROOT%{l_prefix}/etc/wildfly \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/wildfly \
        $RPM_BUILD_ROOT%{l_prefix}/var/wildfly/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/wildfly/run

    #   install default configuration
    cp -rp standalone/configuration/* \
        $RPM_BUILD_ROOT%{l_prefix}/etc/wildfly/
    rm -rf standalone/configuration

    #   install default server environment
    cp -rp standalone/* \
        $RPM_BUILD_ROOT%{l_prefix}/var/wildfly/
    rm -rf standalone
    ln -s ../../etc/wildfly \
        $RPM_BUILD_ROOT%{l_prefix}/var/wildfly/configuration

    #   install remaining application server code
    cp -rp * \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/wildfly/

    #   install rc script
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE rc.wildfly} $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/

    #   create wrapper scripts
    ( echo "#!/bin/sh"
      echo "exec %{l_prefix}/libexec/wildfly/bin/jboss-cli.sh \"\$@\""
    ) >wildfly-cli
    %{l_shtool} install -c -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/sbin/
    ( echo "#!/bin/sh"
      echo "exec %{l_prefix}/libexec/wildfly/bin/add-user.sh \"\$@\""
    ) >wildfly-adduser
    %{l_shtool} install -c -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/sbin/

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%dir %attr(-,%{l_nusr},%{l_ngrp}) %{l_prefix}/etc/wildfly' \
        '%attr(-,%{l_nusr},%{l_ngrp}) %{l_prefix}/etc/wildfly/*' \
        '%dir %attr(-,%{l_nusr},%{l_ngrp}) %{l_prefix}/var/wildfly' \
        '%attr(-,%{l_nusr},%{l_ngrp}) %{l_prefix}/var/wildfly/deployments' \
        '%attr(-,%{l_nusr},%{l_ngrp}) %{l_prefix}/var/wildfly/lib' \
        '%attr(-,%{l_nusr},%{l_ngrp}) %{l_prefix}/var/wildfly/lib/ext' \
        '%attr(-,%{l_nusr},%{l_ngrp}) %{l_prefix}/var/wildfly/tmp' \
        '%attr(-,%{l_nusr},%{l_ngrp}) %{l_prefix}/var/wildfly/tmp/auth' \
        '%attr(-,%{l_nusr},%{l_ngrp}) %{l_prefix}/var/wildfly/log' \
        '%attr(-,%{l_nusr},%{l_ngrp}) %{l_prefix}/var/wildfly/run' \
        '%config %{l_prefix}/etc/wildfly/*'

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   display final hint after initial installation
        ( echo "Wildfly is successfully installed and configured with its"
          echo "standalone server default configuration in:"
          echo "    $RPM_INSTALL_PREFIX/var/wildfly/"
          echo "After setting an admin account with:"
          echo "    \$ $RPM_INSTALL_PREFIX/sbin/wildfly-adduser"
          echo "and then starting Wildfly with"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/openpkg rc wildfly start"
          echo "you may connect to its Web management interface under"
          echo "    http://127.0.0.1:8080/"
          echo "in order to further configure the Wildfly application server."
          echo "Wildfly configuration files are located in the directory:"
          echo "    $RPM_INSTALL_PREFIX/etc/wildfly/"
          echo "Wildfly can be re-configured on the CLI with:"
          echo "    \$ $RPM_INSTALL_PREFIX/sbin/wildfly-cli"
        ) | %{l_rpmtool} msg -b -t notice
    fi

