##
##  tomee.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 information
Name:         tomee
Summary:      Apache Tomcat Enterprise Edition (TomEE)
URL:          http://tomee.apache.org/
Vendor:       Apache Software Foundation
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Web
License:      Apache Software License 2.0
Version:      10.1.2
Release:      20250921

#   list of sources
Source0:      http://www.apache.org/dist/tomee/tomee-%{version}/apache-tomee-%{version}-plus.tar.gz
Source1:      rc.tomee
Patch0:       tomee.patch

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

%description
    Apache Tomcat Enterprise Edition (TomEE) is the reference
    implementation of both a Java Servlet API 3.0 and a JavaServer Pages
    (JSP) 2.2 compliant container plus everything needed to be compliant
    to the Java Enterprise Edition (Java EE) 6 Web Profile, JAX-RS and
    EJB specification.

%track
    prog tomee = {
        version   = %{version}
        url       = http://tomee.apache.org/downloads.html
        regex     = apache-tomee-(\d+\.\d+(?:\.\d+)*)-plus\.tar\.gz
    }

%prep
    %setup -q -n apache-tomee-plus-%{version}
    %patch -p0

%build

%install
    #   create installation tree
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d \
        $RPM_BUILD_ROOT%{l_prefix}/etc/tomee \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/tomee \
        $RPM_BUILD_ROOT%{l_prefix}/share/tomee/doc \
        $RPM_BUILD_ROOT%{l_prefix}/var/tomee/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/tomee/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/tomee/temp \
        $RPM_BUILD_ROOT%{l_prefix}/var/tomee/work

    #   unpack vendor distribution and relocate to final location
    find . -name "*.orig" -print | xargs rm -f
    %{l_tar} -cf - \
        bin/*.sh bin/*.jar conf lib webapps |\
    ( umask 022
      cd $RPM_BUILD_ROOT%{l_prefix}/libexec/tomee/
      %{l_tar} -xf -
    ) || exit $?

    #   relocate configuration directory
    mv  $RPM_BUILD_ROOT%{l_prefix}/libexec/tomee/conf/* \
        $RPM_BUILD_ROOT%{l_prefix}/etc/tomee/
    rmdir \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/tomee/conf
    ln -s \
        %{l_prefix}/etc/tomee \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/tomee/conf
    %{l_shtool} subst %{l_value -s -a} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/tomee/server.xml
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/tomee/Catalina/localhost

    #   relocate web application directory
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/libexec/tomee/webapps/examples
    mv  $RPM_BUILD_ROOT%{l_prefix}/libexec/tomee/webapps \
        $RPM_BUILD_ROOT%{l_prefix}/var/tomee/webapps
    ln -s \
        %{l_prefix}/var/tomee/webapps \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/tomee/webapps

    #   relocate working directory
    ln -s \
        %{l_prefix}/var/tomee/work \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/tomee/work

    #   relocate logging directory
    ln -s \
        %{l_prefix}/var/tomee/log \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/tomee/logs

    #   relocate logfile and temporary directory
    %{l_shtool} subst %{l_value -s -a} \
        -e "s;@l_password@;`apg -n1 -a0 -m8 -x8 -MCL`;g" \
        $RPM_BUILD_ROOT%{l_prefix}/etc/tomee/tomcat-users.xml
    %{l_shtool} subst \
        -e 's;"$CATALINA_BASE"/temp;%{l_prefix}/var/tomee/temp;' \
        -e 's;"$CATALINA_BASE"/logs;%{l_prefix}/var/tomee/log;' \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/tomee/bin/catalina.sh

    #   make scripts executable
    chmod 755 $RPM_BUILD_ROOT%{l_prefix}/libexec/tomee/bin/*

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

    #   determine package files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%attr(755,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/tomee/run' \
        '%attr(755,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/tomee/log' \
        '%attr(755,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/tomee/temp' \
        '%attr(755,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/tomee/work' \
        '%attr(755,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/tomee/webapps' \
        '%dir %attr(755,%{l_rusr},%{l_rgrp}) %{l_prefix}/etc/tomee' \
        '%config %attr(644,%{l_rusr},%{l_rgrp}) %{l_prefix}/etc/tomee/*' \
        '%attr(755,%{l_rusr},%{l_rgrp}) %{l_prefix}/etc/tomee/Catalina' \
        '%attr(755,%{l_rusr},%{l_rgrp}) %{l_prefix}/etc/tomee/Catalina/localhost'

%files -f files

%clean

%post
    #   after upgrade, restart service
    if [ $1 -eq 2 ]; then
        eval `%{l_rc} tomee status 2>/dev/null`
        [ ".$tomee_active" = .yes ] && %{l_rc} tomee restart
    fi
    exit 0

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

