##
##  schnack.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_schnack_base  0.1.4
%define       V_schnack_snap  20180213

#   package information
Name:         schnack
Summary:      Website Commenting System
URL:          https://schnack.cool/
Vendor:       Webkid, Gregor Aisch
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Web
License:      MIT
Version:      %{V_schnack_base}.%{V_schnack_snap}
Release:      20180213

#   list of sources
Source0:      http://download.openpkg.org/components/versioned/schnack/schnack-%{V_schnack_base}-%{V_schnack_snap}.tar.xz
Source1:      schnack.sh
Source2:      rc.schnack
Patch0:       schnack.patch

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

%description
    Schnack is a Disqus-like easy to use and Open Source commenting
    system written in JavaScript.

%track
    prog schnack = {
        version   = %{version}
        url       = https://github.com/schn4ck/schnack/releases
        regex     = (\d+\.\d+\.\d+)\.tar\.gz
    }

%prep
    %setup -q -n schnack
    %patch -p0

%build
    #   replace variables
    %{l_shtool} subst %{l_value -s -a} \
         config.tpl.json

    #   build native Node.js modules
    (   cd node_modules/sqlite3
        npm run install
    ) || exit $?

    #   build package
    npm run build

    #   trim down Node.js modules
    %{l_prefix}/bin/node-openpkg squeeze

    #   adjust config
    mv config.tpl.json config.json

%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/schnack \
        $RPM_BUILD_ROOT%{l_prefix}/lib/schnack \
        $RPM_BUILD_ROOT%{l_prefix}/var/schnack/db \
        $RPM_BUILD_ROOT%{l_prefix}/var/schnack/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/schnack/log

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

    #   install Node.js modules
    cp -rp * \
        $RPM_BUILD_ROOT%{l_prefix}/lib/schnack/
    mv  $RPM_BUILD_ROOT%{l_prefix}/lib/schnack/config.json \
        $RPM_BUILD_ROOT%{l_prefix}/etc/schnack/
    ln -s ../../etc/schnack/config.json \
        $RPM_BUILD_ROOT%{l_prefix}/lib/schnack/config.json

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

%files -f files

%clean

%post
    if [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} schnack status 2>/dev/null`
        [ ".$schnack_active" = .yes ] && %{l_rc} schnack restart
    fi
    exit 0

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

