##
##  npm.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_npm     11.6.2
%define       V_node24  24.10.0
%define       V_node22  22.20.0
%define       V_node20  20.19.5

#   package information
Name:         npm
Summary:      Node Package Manager
URL:          https://www.npmjs.com/
Vendor:       npm, Inc.
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        PLUS
Group:        Archiver
License:      MIT
Version:      %{V_npm}
Release:      20251010

#   list of sources
Source0:      https://github.com/npm/cli/archive/v%{V_npm}.tar.gz
Source1:      http://nodejs.org/download/release/v%{V_node24}/node-v%{V_node24}-headers.tar.gz
Source2:      http://nodejs.org/download/release/v%{V_node22}/node-v%{V_node22}-headers.tar.gz
Source3:      http://nodejs.org/download/release/v%{V_node20}/node-v%{V_node20}-headers.tar.gz
Patch0:       npm.patch

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

%description
    Node Package Manager (NPM) is the package manager for the
    Node.js/JavaScript and Web/JavaScript ecosystems.

%track
    prog npm:npm = {
        version   = %{V_npm}
        url       = https://github.com/npm/cli/releases
        regex     = tree/v(\d+\.\d+\.\d+)[""]
    }
    prog npm:node24 = {
        version   = %{V_node24}
        url       = https://nodejs.org/download/release/
        regex     = v(24\.\d+\.\d+)
    }
    prog npm:node22 = {
        version   = %{V_node22}
        url       = https://nodejs.org/download/release/
        regex     = v(22\.\d+\.\d+)
    }
    prog npm:node20 = {
        version   = %{V_node20}
        url       = https://nodejs.org/download/release/
        regex     = v(20.\d+\.\d+)
    }

%prep
    %setup -q -n cli-%{V_npm}
    %patch -p0

%build
    #   configure NPM
    CC="%{l_cc}" \
    CFLAGS="%{l_cflags -O}" \
    CPPFLAGS="%{l_cppflags}" \
    LDFLAGS="%{l_ldflags}" \
    %{l_bash} ./configure \
        --prefix=%{l_prefix} \
        --disable-shared \
        --disable-nls

    #   ensure the right Node executable is used
    %{l_shtool} subst \
        -e 's;/usr/bin/env node;%{l_prefix}/bin/node;g' \
        bin/npm-cli.js \
        bin/npx-cli.js

%install
    #   create installation hierarchy
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/bin \
        $RPM_BUILD_ROOT%{l_prefix}/lib/node/pkg/npm \
        $RPM_BUILD_ROOT%{l_prefix}/share/npm/node-gyp

    #   install startup wrapper
    (   echo "#!/bin/sh"
        echo "exec %{l_prefix}/bin/node \\%{l_nil}"
        echo "    --max-old-space-size=4096 \\%{l_nil}"
        echo "    %{l_prefix}/lib/node/pkg/npm/bin/npm-cli.js \\%{l_nil}"
        echo "    \${1+\"\$@\"}"
    ) >npm.sh
    %{l_shtool} install -c -m 755 \
        npm.sh $RPM_BUILD_ROOT%{l_prefix}/bin/npm
    (   echo "#!/bin/sh"
        echo "exec %{l_prefix}/bin/node \\%{l_nil}"
        echo "    --max-old-space-size=4096 \\%{l_nil}"
        echo "    %{l_prefix}/lib/node/pkg/npm/bin/npx-cli.js \\%{l_nil}"
        echo "    \${1+\"\$@\"}"
    ) >npx.sh
    %{l_shtool} install -c -m 755 \
        npx.sh $RPM_BUILD_ROOT%{l_prefix}/bin/npx

    #   install node-gyp wrapper
    (   echo "#!/bin/sh"
        echo "NODE_GYP=\"%{l_prefix}/share/npm/node-gyp\""
        echo "export NODE_GYP"
        echo "exec %{l_prefix}/bin/node \\%{l_nil}"
        echo "    --max-old-space-size=4096 \\%{l_nil}"
        echo "    %{l_prefix}/lib/node/pkg/npm/node_modules/node-gyp/bin/node-gyp.js \\%{l_nil}"
        echo "    \${1+\"\$@\"}"
    ) >npm-node-gyp.sh
    %{l_shtool} install -c -m 755 \
        npm-node-gyp.sh $RPM_BUILD_ROOT%{l_prefix}/bin/npm-node-gyp

    #   install NPM sources
    rm -f bin/npm.cmd bin/npm
    rm -rf test
    rm -rf doc
    rm -rf html
    rm -f AUTHORS CHANGELOG.md CONTRIBUTING.md LICENSE README.md
    rm -f node_modules/columnify/node_modules/.bin/strip-ansi
    rm -f node_modules/request/node_modules/har-validator/node_modules/chalk/node_modules/.bin/strip-ansi
    mkdir -p node_modules/@npmcli
    cp -rp workspaces/config node_modules/@npmcli/
    rm -rf workspaces/arborist/test
    cp -rp workspaces/arborist node_modules/@npmcli/
    cp -rp workspaces/lib* node_modules/
    cp -rp * \
        $RPM_BUILD_ROOT%{l_prefix}/lib/node/pkg/npm/

    #   provide an unpacked verson of the Node sources
    (   cd $RPM_BUILD_ROOT%{l_prefix}/share/npm/node-gyp
        %{l_gzip} -d -c %{SOURCE1} | %{l_tar} xf -
        mv node-v%{V_node24} %{V_node24}
        %{l_gzip} -d -c %{SOURCE2} | %{l_tar} xf -
        mv node-v%{V_node22} %{V_node22}
        %{l_gzip} -d -c %{SOURCE3} | %{l_tar} xf -
        mv node-v%{V_node20} %{V_node20}
    ) || exit $?

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT %{l_files_std}

%files -f files

%clean

