##
##  nexus.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   3.34.0
%define       V_dist   3.34.0-01
%define       V_subdir 3.34.0-01

#   package information
Name:         nexus
Summary:      Artifact Repository Manager
URL:          http://www.sonatype.com/nexus-repository-oss
Vendor:       Sonatype
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        PLUS
Group:        Database
License:      AGPL
Version:      %{V_opkg}
Release:      20210902

#   list of sources
Source0:      http://download.sonatype.com/nexus/3/nexus-%{V_dist}-unix.tar.gz
Source1:      nexus.sh
Source2:      rc.nexus
Patch0:       nexus.patch

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

%description
    Nexus manages software artifacts required for development,
    deployment, and provisioning. If you develop software, Nexus can
    help you share those artifacts with other developers and end-users.
    It supports the repository formats of Maven, NuGet, NPM, Docker and
    Bower.

%track
    prog nexus = {
        version   = %{V_dist}
        url       = https://www.sonatype.com/products/repository-oss-download
        regex     = nexus-(\d+\.\d+\.\d+-\d+)-unix\.tar\.gz
    }

%prep
    %setup -q -n nexus-%{V_subdir}
    %patch -p0

%build
    #   cleanup
    rm -f etc/*.orig

    #   patch configuration
    %{l_shtool} subst \
        -e 's;^reference.:file.:;;g' \
        etc/karaf/startup.properties

%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/nexus \
        $RPM_BUILD_ROOT%{l_prefix}/lib/nexus \
        $RPM_BUILD_ROOT%{l_prefix}/share/nexus \
        $RPM_BUILD_ROOT%{l_prefix}/var/nexus/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/nexus/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/nexus/tmp \
        $RPM_BUILD_ROOT%{l_prefix}/var/nexus/data \
        $RPM_BUILD_ROOT%{l_prefix}/var/nexus/deploy

    #   install components
    cp -rp etc/* $RPM_BUILD_ROOT%{l_prefix}/etc/nexus/
    cp -rp lib/* $RPM_BUILD_ROOT%{l_prefix}/lib/nexus/
    cp -rp public $RPM_BUILD_ROOT%{l_prefix}/share/nexus/
    cp -rp system $RPM_BUILD_ROOT%{l_prefix}/share/nexus/
    ln -s %{l_prefix}/etc/nexus $RPM_BUILD_ROOT%{l_prefix}/lib/nexus/etc
    ln -s %{l_prefix}/var/nexus/data $RPM_BUILD_ROOT%{l_prefix}/lib/nexus/data
    ln -s %{l_prefix}/var/nexus/deploy $RPM_BUILD_ROOT%{l_prefix}/lib/nexus/deploy
    ln -s %{l_prefix}/share/nexus/public $RPM_BUILD_ROOT%{l_prefix}/lib/nexus/public
    ln -s %{l_prefix}/share/nexus/system $RPM_BUILD_ROOT%{l_prefix}/lib/nexus/system

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

    #   install run-command script
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE rc.nexus} $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/nexus/*' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/etc/nexus' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/etc/nexus/*' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/nexus' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/nexus/*'

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   display final hints on initial installation
        ( echo "Next steps:"
          echo ""
          echo "1. By default, Nexus runs its server process on IPv4 address"
          echo "   127.0.0.1, TCP port 8081. Please change this by editing the"
          echo "   properties in the configuration file:"
          echo "       $RPM_INSTALL_PREFIX/etc/nexus/nexus-default.properties"
          echo ""
          echo "2. Start the Nexus server by running:"
          echo "       \$ $RPM_INSTALL_PREFIX/bin/openpkg rc nexus start"
          echo ""
          echo "3. Connect to the Nexus server with a browser by visiting URL"
          echo "   (login with username 'admin' and password 'admin123'):"
          echo "       http://127.0.0.1:8081/"
        ) | %{l_rpmtool} msg -b -t notice
    fi
    if [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} nexus status 2>/dev/null`
        [ ".$nexus_active" = .yes ] && %{l_rc} nexus restart
    fi
    exit 0

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

