##
##  verdaccio.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_verdaccio_opkg  4.8.1
%define       V_verdaccio_base  4.8.1
%define       V_verdaccio_snap  20200807

#   package information
Name:         verdaccio
Summary:      NPM Private Registry
URL:          https://verdaccio.org/
Vendor:       Alex Kocharin
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Networking
License:      WTFPL
Version:      %{V_verdaccio_opkg}.%{V_verdaccio_snap}
Release:      20200807

#   list of sources
Source0:      http://download.openpkg.org/components/versioned/verdaccio/verdaccio-%{V_verdaccio_base}-%{V_verdaccio_snap}.tar.xz
Source1:      verdaccio.sh
Source2:      verdaccio.yaml
Source3:      rc.verdaccio
Source4:      verdaccio-tls.sh

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

%description
    Verdaccio is a private/local/caching/proxy NPM repository
    server which allows you to have a local NPM registry with zero
    configuration. It is a fork of Sinopia.

%track
    prog verdaccio = {
        version   = %{V_verdaccio_base}
        url       = https://github.com/verdaccio/verdaccio/releases
        regex     = v(__VER__)\.tar\.gz
    }

%prep
    %setup -q -n verdaccio

%build
    #   rebuild native Node modules
    rm -rf node_modules/dtrace-provider

    #   build verdaccio
    HOME=`pwd`
    export HOME
    npm run code:build

    #   remove problematic files
    rm -f "assets/fonts/Apache License.txt"

    #   prune files
    %{l_prefix}/bin/node-openpkg squeeze

%install
    #   install program
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/lib/verdaccio
    cp -rp * \
        $RPM_BUILD_ROOT%{l_prefix}/lib/verdaccio/

    #   install server wrapper
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/sbin
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE verdaccio.sh} $RPM_BUILD_ROOT%{l_prefix}/sbin/verdaccio

    #   install server configuration
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/verdaccio
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE verdaccio.yaml} $RPM_BUILD_ROOT%{l_prefix}/etc/verdaccio/

    #   install helper program
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        -e 's;@l_bash@;%{l_bash};g' \
        %{SOURCE verdaccio-tls.sh} $RPM_BUILD_ROOT%{l_prefix}/sbin/verdaccio-tls

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

    #   create run-time directories
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var/verdaccio/etc \
        $RPM_BUILD_ROOT%{l_prefix}/var/verdaccio/pkg \
        $RPM_BUILD_ROOT%{l_prefix}/var/verdaccio/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/verdaccio/run

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

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   on initial installation, generate TLS certificate/key pair
        echo "Generating TLS Certificates/Keys" | %{l_rpmtool} msg -b -t notice
        $RPM_INSTALL_PREFIX/sbin/verdaccio-tls localhost 127.0.0.1
        ( echo "Verdaccio was configured with a standard TLS certificate/key pair."
          echo "for \"localhost\" and \"127.0.0.1\". For production use, you usually let"
          echo "Verdaccio listen on an external IP address. For this the TLS"
          echo "certificate/key pair should be regenerated with for instance:"
          echo "    \$ $RPM_INSTALL_PREFIX/sbin/verdaccio-tls www.example.com 192.168.0.1"
        ) | %{l_rpmtool} msg -b -t notice
    elif [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} verdaccio status 2>/dev/null`
        [ ".$verdaccio_active" = .yes ] && %{l_rc} verdaccio restart
    fi
    exit 0

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

