##
##  glassfish.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 5.1.0
%define       V_dist 5.1.0

#   package information
Name:         glassfish
Summary:      Java Enterprise Edition Application Server
URL:          http://glassfish.java.net/
Vendor:       ORACLE
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        PLUS
Group:        Web
License:      CDDL/GPL
Version:      %{V_opkg}
Release:      20190810

#   list of sources
Source0:      https://repo1.maven.org/maven2/org/glassfish/main/distributions/glassfish/%{V_dist}/glassfish-%{V_dist}.zip
Source1:      rc.glassfish
Patch0:       glassfish.patch

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

%description
    GlassFish is an application server supporting the Java Platform,
    Enterprise Edition 6 (Java EE 6) standard with production-ready
    features such as clustering and centralized administration. Its
    overall flexibility and ease of use reduce cost by providing
    improved developer productivity, simplified application
    architecture, and dynamic upgrade support. GlassFish is ORACLE's
    official reference implementation of Java EE 6.

%track
    prog glassfish = {
        version   = %{version}
        url       = https://glassfish.java.net/download.html
        regex     = glassfish-(__VER__)\.zip
    }

%prep
    %setup -q -n glassfish5
    %patch -p0

%build
    #   remove Windows-specific files
    rm -f bin/*.bat
    rm -f glassfish/bin/*.bat

    #   substitute paths
    %{l_shtool} subst %{l_value -s -a} \
        glassfish/config/asenv.conf

%install
    #   create installation hierarchy
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/bin \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d \
        $RPM_BUILD_ROOT%{l_prefix}/etc/glassfish \
        $RPM_BUILD_ROOT%{l_prefix}/lib/glassfish \
        $RPM_BUILD_ROOT%{l_prefix}/var/glassfish/domains \
        $RPM_BUILD_ROOT%{l_prefix}/var/glassfish/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/glassfish/log

    #   install components
    cp -rp glassfish/config/* $RPM_BUILD_ROOT%{l_prefix}/etc/glassfish/
    rm -rf glassfish/config
    rm -rf glassfish/domains
    cp -r * $RPM_BUILD_ROOT%{l_prefix}/lib/glassfish/
    ln -s ../../../etc/glassfish $RPM_BUILD_ROOT%{l_prefix}/lib/glassfish/glassfish/config

    #   install startup wrapper script
    ( echo "#!/bin/sh"
      echo "PATH=%{l_prefix}/bin:\$PATH"
      echo "exec %{l_prefix}/lib/glassfish/glassfish/bin/asadmin \${1+\"\$@\"}"
    ) >asadmin.sh
    %{l_shtool} install -c -m 755 \
        asadmin.sh $RPM_BUILD_ROOT%{l_prefix}/bin/asadmin

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

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %{l_prefix}/etc/glassfish/*' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/glassfish' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/glassfish/*' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/glassfish/*/*'

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   create initial domain "domain1"
        su - %{l_rusr} -c \
            "$RPM_INSTALL_PREFIX/bin/asadmin create-domain --nopassword domain1 2>&1" | \
        $RPM_INSTALL_PREFIX/lib/openpkg/shtool prop \
            -p "Creating initial GlassFish domain \"domain1\""
        if [ ! -d $RPM_INSTALL_PREFIX/var/glassfish/domains/domain1 ]; then
            echo "ERROR: failed to create initial GlassFish domain \"domain1\"" 1>&2
            exit 1
        fi

        #   display final hints on initial installation
        ( echo "By default, GlassFish runs its initial domain \"domain1\" on IPv4"
          echo "address 127.0.0.1, TCP port 4848 (admin) and 8080 (applications)."
          echo "Start the GlassFish server by running:"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/openpkg rc glassfish start"
          echo "Connect to the GlassFish admin console by visiting URL:"
          echo "    http://127.0.0.1:4848/"
          echo "Deploy applications by running (for instance):"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/asadmin deploy hello.war"
          echo "Connect to the deployed application by visiting URL:"
          echo "    http://127.0.0.1:8080/hello"
          echo "Usually you want to set an admin password (by default it is empty)"
          echo "and enable encryption for the communication to the admin service:"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/asadmin change-admin-password"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/asadmin enable-secure-admin"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/openpkg rc glassfish stop start"
          echo "After this connect to the GlassFish admin console by visiting URL:"
          echo "    https://127.0.0.1:4848/"
        ) | %{l_rpmtool} msg -b -t notice
    fi
    if [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} glassfish status 2>/dev/null`
        [ ".$glassfish_active" = .yes ] && %{l_rc} glassfish restart
    fi
    exit 0

%preun
    if [ $1 -eq 0 ]; then
        #   before erase, stop service and remove runtime files
        %{l_rc} glassfish stop 2>/dev/null
        rm -rf $RPM_INSTALL_PREFIX/var/glassfish/run/* >/dev/null 2>&1 || true
        rm -rf $RPM_INSTALL_PREFIX/var/glassfish/tmp/* >/dev/null 2>&1 || true
    fi
    exit 0

