##
##  tomcat.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:         tomcat
Summary:      Apache Tomcat
URL:          http://tomcat.apache.org/
Vendor:       Apache Software Foundation
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        PLUS
Group:        Web
License:      Apache Software License 2.0
Version:      9.0.111
Release:      20251014

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

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

%description
    Apache Tomcat is the reference implementation of both a Java Servlet
    API 3.0 and a JavaServer Pages (JSP) 2.2 compliant container.

%track
    prog tomcat = {
        version   = %{version}
        url       = http://www.apache.org/dist/tomcat/tomcat-9/
        regex     = v(\d+\.\d+\.\d+)
    }

%prep
    %setup -q -n apache-tomcat-%{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/tomcat \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/tomcat \
        $RPM_BUILD_ROOT%{l_prefix}/share/tomcat/doc \
        $RPM_BUILD_ROOT%{l_prefix}/var/tomcat/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/tomcat/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/tomcat/temp \
        $RPM_BUILD_ROOT%{l_prefix}/var/tomcat/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/tomcat/
      %{l_tar} -xf -
    ) || exit $?

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

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

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

    #   relocate logging directory
    ln -s \
        %{l_prefix}/var/tomcat/log \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/tomcat/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/tomcat/tomcat-users.xml
    %{l_shtool} subst \
        -e 's;"$CATALINA_BASE"/temp;%{l_prefix}/var/tomcat/temp;' \
        -e 's;"$CATALINA_BASE"/logs;%{l_prefix}/var/tomcat/log;' \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/tomcat/bin/catalina.sh

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

    #   install run-command script
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE rc.tomcat} $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/tomcat/run' \
        '%attr(755,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/tomcat/log' \
        '%attr(755,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/tomcat/temp' \
        '%attr(755,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/tomcat/work' \
        '%attr(755,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/tomcat/webapps' \
        '%dir %attr(755,%{l_rusr},%{l_rgrp}) %{l_prefix}/etc/tomcat' \
        '%config %attr(644,%{l_rusr},%{l_rgrp}) %{l_prefix}/etc/tomcat/*' \
        '%attr(755,%{l_rusr},%{l_rgrp}) %{l_prefix}/etc/tomcat/Catalina' \
        '%attr(755,%{l_rusr},%{l_rgrp}) %{l_prefix}/etc/tomcat/Catalina/localhost'

%files -f files

%clean

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

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

