#   ---------------------------------------------------------------------- copyright and license ---
#
#   File: etc/gnome-shell-extension-agism.spec.in
#
#   Copyright 🄯 2022, 2023 Van de Bugger.
#
#   This file is part of Agism.
#
#   Agism is free software: you can redistribute it and/or modify it under the terms of the GNU
#   General Public License as published by the Free Software Foundation, either version 3 of the
#   License, or (at your option) any later version.
#
#   Agism is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
#   the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#   General Public License for more details.
#
#   You should have received a copy of the GNU General Public License along with Agism. If not, see
#   <https://www.gnu.org/licenses/>.
#
#   SPDX-License-Identifier: GPL-3.0-or-later
#
#   ---------------------------------------------------------------------- copyright and license ---

#   In OpenSuse documentation files are expected to be in `/usr/share/doc/packages/${name}`, while
#   `make` by default installs documents to `/usr/share/doc/${name}`. `rpmbuild` fails because it
#   can't find files it expected location.
#
#   I would expect OpenSuse's `$configure` macro passes `--docdir` option to `configure` command,
#   bit it does not. Let's fix it:
#
%if 0%{?suse_version}
    %global configure %{configure} --docdir=%{_docdir}/%{name}
%endif

%{lua:
    --[[--
        TODO: Use vdb.lua here?
    --]]--

    function defined( name )
        return rpm.expand( "%{?" .. name .. ":1}" ) == "1"
    end

    function say( format, ... )
        io.stderr:write( string.format( format, ... ) .. "\n" )
    end

    function die( format, ... )
        say( format, ... )
        error( "oops" )
    end

    function spec( text )
        print( rpm.expand( text ) )
    end

    function rpm_version( required )
        if _rpm_version_ == nil then
            local program = rpm.expand( "%{_bindir}/rpm" )
            local output = rpm.expand( "%( " .. program .. " --version )" )
            if output == "" then
                die( "Can't find version of '%s' program.", program )
            end
            local version = string.match( output, "^RPM version ([-0-9a-z.]+)$" )
                -- RPM version could be like `4.0.16` or `4.1.0-alpha1`.
            if version == nil then
                die( "Can't parse '%s' program output: '%s'", program, output )
            end
            _rpm_version_ = version
        end
        return rpm.vercmp( _rpm_version_, required ) >= 0
    end

    function gets( name, default )
        return defined( name ) and rpm.expand( "%{" .. name .. "}" ) or default
    end

    function getn( name )
        return tonumber( gets( name, "0" ) )
    end

    arch     = gets( "_arch" )
    centos   = getn( "centos" )
    eln      = getn( "eln" )
    epel     = getn( "epel" )
    euler    = getn( "openEuler" )
    fedora   = getn( "fedora" )
    mageia   = getn( "mageia" )
    mandriva = getn( "omvver" )
    rhel     = getn( "rhel" )
    suse     = getn( "suse_version" )

    if centos > 0 or epel > 0 or eln > 0 then
        rhel = 0
    end

    --[[--
        platform                   original macros      fixed macros
        -------------------------------------------------------------
        centos-stream+epel-next    centos, epel, rhel   centos + epel
        centos-stream              centos, rhel         centos
        epel                       epel, rhel           epel
        openeuler-22.03            euler=2              euler
        fedora                     fedora               fedora
        fedora-eln                 eln, rhel            eln
        mageia                     mageia=8             mageia
        mageia-cauldron            mageia=9             mageia
        openmandriva-cooker        mandriva=22090000    mandriva
        openmandriva-rolling       mandriva=22012000    mandriva
        opensuse-leap              suse=1500            suse
        opensuse-tumbleweed        suse=1599            suse
        rhel                       rhel                 rhel
        -------------------------------------------------------------
    --]]--

    if mandriva > 0 then
        rpm.define( "__perl %{_bindir}/perl" )
    end
    if suse > 0 then
        rpm.define( "_metainfodir %{_datadir}/metainfo" )
    end
}

%bcond_without          pandoc
%bcond_without          gnome_45

%if %{with pandoc}
    %global enable_man  1
%endif

%global ver             4
%global rel             0.vdb.1.t
%global uuid            agism@io.sourceforge.agism
%global _extdir         %{_datadir}/gnome-shell/extensions
%global _licdir         %{_datadir}/licenses
%global debug_package   %{nil}

Name:                   gnome-shell-extension-agism
Version:                %{ver}
Release:                %{rel}%{?dist}
Summary:                (D-Bus) API for GNOME Input Source Manager
License:                GPLv3+
URL:                    https://agism.sourceforge.io/
Source0:                https://sourceforge.net/projects/vdb-sandbox/files/4/agism-%{ver}-%{rel}.tar.gz
BuildArch:              noarch

# Fedora packaging guidelines say dependency on coreutils, gzip, shadow-utils should not be
# declared.
BuildRequires:      make
%if %{with pandoc}
BuildRequires:      pandoc
%endif
BuildRequires:      m4
BuildRequires:      %{__perl}
    # Perl package name varies: either `perl-interpreter` or `perl-base`.
    # Let's require `perl` executable to avoid conditional constructs.
BuildRequires:      perl(JSON)
BuildRequires:      yajl

%if %{with gnome_45}
Requires:           gnome-shell >= 45~
%else
Requires:           gnome-shell < 45~
%endif

%description

Agism allows 3rd party applications (specifically keyboard layout switchers)
to enumerate and activate input sources (aka keyboard layouts).

If this package is a binary RPM, it is built for GNOME %{?with_gnome_45:⩾}%{!?with_gnome_45:<}45.
If this package is a source RPM, it is generic: use the `--with gnome_45`
or `--without gnome_45` options to build the appropriate binary RPM.

%prep
%setup -q -n agism-%{ver}-%{rel}
%{__mkdir} _build
%{__ln_s} ../conf''igure _build/conf''igure
    # rpmlint complains "configure-without-libdir-spec". Let's hide configure from rpmlint.

%build
cd _build
%{configure}                                \
    %{?with_gnome_45:--enable-gnome-45}     \
    %{!?with_gnome_45:--disable-gnome-45}   \
    %{?enable_man:--enable-man}             \
    %{!?enable_man:--disable-man}           \
    --disable-author-testing                \
    --disable-rpm                           \
    --disable-html                          \
    --disable-dependency-tracking           \
    --srcdir=..
%{make_build}

%install
cd _build
%{make_install}
%if 0%{?fedora}
%{__rm} -rf %{buildroot}%{_licdir}/%{name}
%endif

%check
cd _build
# Non-UTF-8 locale may cause exception when printing non-ASCII characters.
LANG=C.UTF-8 %{__make} check

%files
%defattr( -, root, root, - )
%dir    %{_extdir}/%{uuid}
%doc    %{_extdir}/%{uuid}/*
%if 0%{?fedora}
    %license    LICENSE.md LICENSES/*
%else
    %dir        %{_licdir}/%{name}
                %{_licdir}/%{name}/*
%endif
%if 0%{?enable_man}
    %{_mandir}/man7/*
%endif

%changelog
* Fri Sep 15 2023 Van de Bugger - 3-0.vdb.2
- gnome-shell < 45 required.

* Fri Apr 28 2023 Van de Bugger - 3-0.vdb.1
- Agism v3.
- Spec file does not guess `pandoc` presense any more, it is expected to be. If not,
  `--without pandoc` option must be used explicitly.

* Sat Dec 31 2022 Van de Bugger - 2-0.vdb.2
- OS detection ported from Tapper.

* Thu Sep 22 2022 Van de Bugger - 2-0.vdb.1
- Agism v2.

* Thu Jul 28 2022 Van de Bugger - 1-0.vdb.3
- Few files added to source tarball.

* Wed Jun 01 2022 Van de Bugger - 1-0.vdb.2
- Upload link fixed in m4 macros.
- Few download links fixed.

* Wed Jun 01 2022 Van de Bugger - 1-0.vdb.1
- Agism v1.

# end of file #