##
##  sinopia.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_sinopia_base  1.4.0
%define       V_sinopia_snap  20180105

#   package information
Name:         sinopia
Summary:      NPM Private Registry
URL:          https://github.com/rlidwka/sinopia/
Vendor:       Alex Kocharin
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Networking
License:      WTFPL
Version:      %{V_sinopia_base}
Release:      20180105

#   list of sources
Source0:      http://download.openpkg.org/components/versioned/sinopia/sinopia-%{V_sinopia_base}-%{V_sinopia_snap}.tar.xz
Source1:      sinopia.sh
Source2:      sinopia.yaml
Source3:      rc.sinopia

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

%description
    Sinopa is a private/local/caching/proxy NPM repository server which
    allows you to have a local NPM registry with zero configuration.

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

%prep
    %setup -q -n sinopia

%build
    #   rebuild native Node modules
    (   cd node_modules/crypt3
        npm-node-gyp clean
        npm-node-gyp rebuild
    ) || exit $?
    (   cd node_modules/fs-ext
        npm-node-gyp clean
        npm-node-gyp rebuild
    ) || exit $?
    rm -rf node_modules/dtrace-provider

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

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

    #   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 sinopia.sh} $RPM_BUILD_ROOT%{l_prefix}/sbin/sinopia

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

    #   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.sinopia} $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/sinopia/etc \
        $RPM_BUILD_ROOT%{l_prefix}/var/sinopia/pkg \
        $RPM_BUILD_ROOT%{l_prefix}/var/sinopia/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/sinopia/run

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

%files -f files

%clean

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

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

