%global commit 23c39ca87ac28be91210e7c0b04f90e0b06b14d3
%global shortcommit %(c=%{commit}; echo ${c:0:7})
%global snapdate 20220224

# Tests don't seem to work properly
%bcond_with check

Name:           libalternatives
Version:        1.2%{?snapdate:^%{snapdate}.%{shortcommit}}
Release:        1%{?dist}
Summary:        Configurable alternative for update-alternatives

License:        LGPLv3+
URL:            https://github.com/openSUSE/libalternatives
%if %{defined snapdate}
Source0:        %{url}/archive/%{commit}/%{name}-%{shortcommit}.tar.gz
%else
Source0:        %{url}/archive/v%{version}/%{name}-%{version}.tar.gz
%endif

BuildRequires:  cmake >= 3.12
BuildRequires:  pkgconfig(cunit)
BuildRequires:  gcc
BuildRequires:  make

%description
libalternatives is a helper that executes an application based on preferences
of an user, system admin or package maintainer, in this order of preference.
This is accomplished with only a help of config files without the need to
maintain system symlinks states.


%package        devel
Summary:        Development files for %{name}
Requires:       %{name}%{?_isa} = %{version}-%{release}

%description    devel
The %{name}-devel package contains libraries and header files for
developing applications that use %{name}.


%package -n alts
Summary:        Simple utility for alternatives management using %{name}
License:        GPLv3+
Requires:       %{name}%{?_isa} = %{version}-%{release}

%description -n alts
This package contains a default helper and configuration application utility
for libalternatives. libalternatives is a helper that executes an application
based on preferences of an user, system admin or package maintainer, in this
order of preference. This is accomplished with only a help of config files
without the need to maintain system symlinks states.


%prep
%autosetup -p1 %{?snapdate:-n %{name}-%{commit}}


%build
%cmake
%cmake_build


%install
%cmake_install

# Create the directory for alternatives configuration to be stored
mkdir -p -m 0755 %{buildroot}%{_datadir}/%{name}

mkdir -p -m 0755 %{buildroot}%{_datadir}/libalternatives/libalternatives-unit-test-helper
cat > %{buildroot}%{_datadir}/libalternatives/libalternatives-unit-test-helper/10.conf <<EOF
binary=/usr/bin/true
man=true.1
EOF

cat > %{buildroot}%{_bindir}/libalternatives-unit-test-helper.sh <<EOF
#!/bin/bash

(diff <(man libalternatives-unit-test-helper 2> /dev/null) <(man true) >> /dev/null && echo "Everything seems OK && exit")
echo "It seems `man` doesn't display the proper manpage for libalternatives system."
echo "You should see the manpage for true(1) when running"
echo "   man libalternatives-unit-test-helper"
exit 1
EOF
chmod 755 %{buildroot}%{_bindir}/libalternatives-unit-test-helper.sh


%if %{with check}
%check
%ctest
%endif


%files
%license COPYING*
%doc README.md
%{_libdir}/%{name}.so.1{,.*}
%dir %{_datadir}/%{name}

%files devel
%{_includedir}/%{name}.h
%{_libdir}/%{name}.so
%{_libdir}/cmake/%{name}/
%{_libdir}/pkgconfig/%{name}.pc
# Test data files
%{_bindir}/%{name}-unit-test-helper.sh
%{_datadir}/%{name}/%{name}-unit-test-helper/10.conf

%files -n alts
%license COPYING
%doc README.md
%{_bindir}/alts
%{_mandir}/man1/alts.1*


%changelog
* Wed Sep 07 2022 Neal Gompa <ngompa@fedoraproject.org> - 1.2^20220224.23c39ca-1
- Update to git snapshot

* Tue Sep 21 2021 Neal Gompa <ngompa@fedoraproject.org> - 1.2-1
- Initial packaging