#   ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
#   File: vdb-rpm-macros.spec
#   Copyright 🄯 2021—2025 Van de Bugger.
#   SPDX-License-Identifier: FSFAP
#   ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――

%{lua:
    package.path = rpm.expand( '%{_source' .. 'dir}' ) .. '/?.lua;' .. package.path
        -- Look into source directory for Lua modules.
    local vdb = require "vdb"
}

%global fonts_lua       %{_rpmluadir}/fedora/srpm/fonts.lua
%global statedir        %{_localstatedir}/lib/rpm-state/%{name}
%global patchcmd(RNi:)  %{__patch} -p1 -s -r - -d %{_rpmluadir}/fedora/srpm %** || { \
    status=$?; \
    echo "Can’t patch ‘%{fonts_lua}’: patch exited with non-zero status $status." >&2; \
    false; \
}

Name:           vdb-rpm-macros
Version:        0.0.3
Release:        0.vdb.1%{?dist}
URL:            https://example.org/
Summary:        Lua functions used in VdB RPMs
License:        GPL-3.0-or-later
BuildArch:      noarch

Source0:        vdb.lua
Source1:        fedora-srpm-fonts.lua.patch
Source2:        macros.vdb
Source3:        harness.lua
Source4:        tests.lua

BuildRequires:  rpm >= 4.14.0    %{rem: `rpm.undefine` appears in rpm 4.14.0.}
Requires:       rpm >= 4.14.0    %{rem: Also, rpm 4.11.3 fails few tests.}

%description
TODO

%{lua:
    local harness = require "harness"
    local tests   = require "tests"
}

%prep
# Do nothing.

%build
# Do nothing.

%install
%{__install} -D -m u=rw,go=r -t %{buildroot}%{_rpmluadir}    %{S:0}
%{__install} -D -m u=rw,go=r -t %{buildroot}%{_pkgdatadir}   %{S:1}
%{__install} -D -m u=rw,go=r -t %{buildroot}%{_rpmmacrodir}  %{S:2}

%check
# Actually, the tests are run early, this code just shows the result.
: ------------------------------
: $tests_total tests run
: $tests_passed tests passes
: $tests_failed tests failed
: ------------------------------
if (( $tests_failed > 0 )); then
    cat %{_topdir}/tests.log
fi
(( $tests_total > 0 ))
(( $tests_passed + $tests_failed == $tests_total ))
(( $tests_failed == 0 ))

%files
        %{_rpmluadir}/vdb.lua
%dir    %{_pkgdatadir}
        %{_pkgdatadir}/fedora-srpm-fonts.lua.patch
        %{_rpmmacrodir}/macros.vdb

%pre    %{rem: New package before installing.}
if [[ $1 == 2 ]]; then  # Upgrading.
    # We are going to upgrade existing package. The old patch will be overwritten with a new one.
    # Let's save the old patch, so preun of the old package can use it to restore the files.
    if [[ -f %{_pkgdatadir}/fedora-srpm-fonts.lua.patch ]]; then
        %{__mkdir_p} %{statedir}
        %{__cp} %{_pkgdatadir}/fedora-srpm-fonts.lua.patch %{statedir}
    fi
fi

%post   %{rem: New package after install or upgrade.}
%{__mkdir_p} %{statedir}
echo > %{statedir}/need-to-apply-patch.flag

%preun  %{rem: Old package before uininstalling.}
if [[ $1 == 1 ]]; then  # Upgrading.
    # We are in the middle of upgrade. Patch is already replaced with new one, but we have to
    # use old patch, which is saved in state dir.
    if [[ -f %{statedir}/fedora-srpm-fonts.lua.patch ]]; then
        if [[ -f %{fonts_lua} ]]; then
            %patchcmd -R -i %{statedir}/fedora-srpm-fonts.lua.patch
        fi
        %{__rm} -f %{statedir}/fedora-srpm-fonts.lua.patch
    fi
else                    # Uninstalling.
    if [[ -f %{_pkgdatadir}/fedora-srpm-fonts.lua.patch ]]; then
        if [[ -f %{fonts_lua} ]]; then
            %patchcmd -R -i %{_pkgdatadir}/fedora-srpm-fonts.lua.patch
        fi
    fi
fi

%filetriggerin -- %{fonts_lua}
%patchcmd -N -i %{_pkgdatadir}/fedora-srpm-fonts.lua.patch

%posttrans
if [[ -e %{statedir}/need-to-apply-patch.flag ]]; then
    if [[ -f %{fonts_lua} ]]; then
        %patchcmd -N -i %{_pkgdatadir}/fedora-srpm-fonts.lua.patch
    fi
    %{__rm} -f %{statedir}/need-to-apply-patch.flag
fi

%changelog

* Sun Mar 23 2025 Van de Bugger - 0.0.3-0.vdb.1
- "BuildRequires: vdb-rpm-macros" is not required any more.

* Thu Mar 20 2025 Van de Bugger - 0.0.2-0.vdb.1
- Test-only spec converted to real package.
- Many changes in vdb.lua.

* Sun Nov  7 2021 Van de Bugger - 0.0.1-0.vdb.1
- Initial release.

# end of file #