##
##  jenkins.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:         jenkins
Summary:      Continuous Integration Server
URL:          http://jenkins-ci.org/
Vendor:       Kohsuke Kawaguchi
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Building
License:      MIT
Version:      2.532
Release:      20251015

#   list of sources
Source0:      http://repo.jenkins-ci.org/public/org/jenkins-ci/main/jenkins-war/%{version}/jenkins-war-%{version}.war
Source1:      jenkins-cli.sh
Source2:      rc.jenkins

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

%description
    Jenkins is an extendable Open Source Continuous Integration server.

%track
    prog jenkins = {
        version   = %{version}
        url       = http://repo.jenkins-ci.org/public/org/jenkins-ci/main/jenkins-war/
        regex     = (2\.\d+)/
    }

%prep
    %setup -q -T -c

%build
    unzip -q -x %{SOURCE0} WEB-INF/lib/cli-%{version}.jar

%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}/lib/jenkins \
        $RPM_BUILD_ROOT%{l_prefix}/var/jenkins/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/jenkins/tmp \
        $RPM_BUILD_ROOT%{l_prefix}/var/jenkins/wrk

    #   install program
    %{l_shtool} install -c -m 644 \
        %{SOURCE0} $RPM_BUILD_ROOT%{l_prefix}/lib/jenkins/jenkins.war
    %{l_shtool} install -c -m 644 \
        WEB-INF/lib/cli-%{version}.jar $RPM_BUILD_ROOT%{l_prefix}/lib/jenkins/jenkins-cli.jar

    #   install wrapper script
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE jenkins-cli.sh} $RPM_BUILD_ROOT%{l_prefix}/bin/jenkins-cli

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

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

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   display final hints on initial installation
        ( echo "Next steps:"
          echo ""
          echo "1. By default, Jenkins runs its server process on IPv4 address"
          echo "   127.0.0.1, TCP port 8080. Start the Jenkins server by running:"
          echo "       \$ $RPM_INSTALL_PREFIX/bin/openpkg rc jenkins start"
          echo ""
          echo "2. Connect to the Jenkins server with a browser by visiting URL"
          echo "       http://127.0.0.1:8080/"
        ) | %{l_rpmtool} msg -b -t notice
    fi
    if [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} jenkins status 2>/dev/null`
        [ ".$jenkins_active" = .yes ] && %{l_rc} jenkins restart
    fi
    exit 0

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

