##
##  pm2.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_pm2      5.1.0
%define       V_pm2_snap 20210611

#   package information
Name:         pm2
Summary:      Node Process Manager
URL:          http://pm2.io/
Vendor:       UnitTech
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Process
License:      MIT
Version:      %{V_pm2}
Release:      20210611

#   list of sources
Source0:      http://download.openpkg.org/components/versioned/pm2/pm2-%{V_pm2}-%{V_pm2_snap}.tar.gz
Source1:      pm2.sh
Source2:      pm2.c
Source3:      rc.pm2

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, node-openpkg
PreReq:       OpenPKG, openpkg >= 20160101, node

%description
    PM2 is the next-generation CLI process manager for Node.js with
    native clusterization and load-balancing.

%track
    prog pm2:release = {
        version   = %{V_pm2}
        url       = https://www.npmjs.com/package/pm2
        regex     = <strong>(__VER__)</strong>\s+is\s+the\s+latest
    }
    prog pm2:snapshot = {
        version   = %{V_pm2}-%{V_pm2_snap}
        url       = http://download.openpkg.org/components/versioned/pm2/
        regex     = pm2-(__VER__)\.tar\.gz
    }

%prep
    %setup -q -c

%build
    #   build the setuid wrapper
    cp %{SOURCE pm2.c} .
    %{l_shtool} subst %{l_value -s -a} pm2.c
    %{l_cc} %{l_cflags} %{l_ldflags} -o pm2 pm2.c

    #   remove useless content
    #   %{l_prefix}/bin/node-openpkg squeeze

%install
    #   create installation hierarchy
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/bin \
        $RPM_BUILD_ROOT%{l_prefix}/sbin \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d \
        $RPM_BUILD_ROOT%{l_prefix}/lib/node/pkg/pm2 \
        $RPM_BUILD_ROOT%{l_prefix}/var/pm2/app \
        $RPM_BUILD_ROOT%{l_prefix}/var/pm2/run

    #   install program parts
    %{l_prefix}/bin/node-openpkg install \
        %{l_prefix}/lib/node/pkg/pm2/

    #   install setuid wrapper script
    %{l_shtool} install -c -m 755 \
        pm2 \
        $RPM_BUILD_ROOT%{l_prefix}/sbin/pm2

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

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

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%attr(4755,%{l_nusr},%{l_ngrp}) %{l_prefix}/sbin/pm2' \
        '%attr(-,%{l_nusr},%{l_ngrp}) %{l_prefix}/var/pm2' \
        '%attr(-,%{l_nusr},%{l_ngrp}) %{l_prefix}/var/pm2/app' \
        '%attr(-,%{l_nusr},%{l_ngrp}) %{l_prefix}/var/pm2/run'

%files -f files

%clean

%post
    #   after upgrade, restart service
    [ $1 -eq 2 ] || exit 0
    eval `%{l_rc} pm2 status 2>/dev/null`
    [ ".$pm2_active" = .yes ] && %{l_rc} pm2 restart
    exit 0

%preun
    #   before erase, stop service and remove log files
    [ $1 -eq 0 ] || exit 0
    %{l_rc} pm2 stop 2>/dev/null
    rm -rf $RPM_INSTALL_PREFIX/var/pm2/run/* >/dev/null 2>&1 || true
    rm -rf $RPM_INSTALL_PREFIX/var/pm2/app/* >/dev/null 2>&1 || true
    exit 0

