#
# spec file for package libalternatives
#
# Copyright (c) 2023 SUSE LLC
#
# All modifications and additions to the file contributed by third parties
# remain the property of their copyright owners, unless otherwise agreed
# upon. The license for this file, and modifications and additions to the
# file, is the same license as for the pristine package itself (unless the
# license for the pristine package is not an Open Source License, in which
# case the license is the MIT License). An "Open Source License" is a
# license that conforms to the Open Source Definition (Version 1.9)
# published by the Open Source Initiative.

# Please submit bugfixes or comments via https://bugs.opensuse.org/
#


%define sover 1

Name:           libalternatives
Version:        1.2+30.a5431e9
Release:        1.3
Summary:        Helper for executing preferred application based on user preferences
License:        Apache-2.0
URL:            https://github.com/openSUSE/libalternatives
Source0:        libalternatives-v%{version}.tar.zst
BuildRequires:  cmake > 3.8
BuildRequires:  cunit-devel
BuildRequires:  gcc
BuildRequires:  zstd

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

%package -n alts
Summary:        Helper for executing preferred application based on user preferences
License:        Apache-2.0

%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 a user, system admin or package maintainer, in this
order of preference. This is accomplished with only the help of config files
and without the need to maintain system symlinks states.

%package devel
Summary:        Development headers for libalternatives
License:        Apache-2.0
Requires:       libalternatives%sover = %version

%description devel
This package contains development headers and library for libalternatives.

%package -n libalternatives%sover
Summary:        Runtime for libalternatives
License:        Apache-2.0

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

%package unit-test-helper
Summary:        Verification helper for libalternatives
License:        Apache-2.0
Requires:       alts = %version
BuildArch:      noarch

%description unit-test-helper
This is a testing-only installation that may be used to verify that successful
integration with manual pages.

%prep
%autosetup -n libalternatives-v%version

%build
%cmake
%cmake_build

%install
%cmake_install

mkdir -p -m 0755 %buildroot/%_datadir/libalternatives

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

%check
%ctest

%post -n libalternatives%sover -p /sbin/ldconfig
%postun -n libalternatives%sover -p /sbin/ldconfig

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

%files devel
%_includedir/libalternatives.h
%{_libdir}/libalternatives.so
%{_libdir}/cmake/libalternatives
%{_libdir}/pkgconfig/libalternatives.pc

%files -n libalternatives%sover
%dir %_datadir/libalternatives
%{_libdir}/libalternatives.so.%sover
%{_libdir}/libalternatives.so.%sover.*

%files unit-test-helper
%dir %_datadir/libalternatives
%dir %_datadir/libalternatives/libalternatives-unit-test-helper
%_datadir/libalternatives/libalternatives-unit-test-helper/10.conf
%_bindir/libalternatives-unit-test-helper.sh

%changelog
* Tue Apr 25 2023 bogdan.lezhepekov@suse.com
- Update to version v1.2+30.a5431e9: (bsc#1191692)
  * Change license to less restrictive Apache 2.0
  * doc: fixing a few typos
  * Adds option to display target executable only
  * Makefiles and cmake: rework for reproducible build
  * Improve Makefile
  * libalts_exec_default: fix memory leak on error condition
  * libalts_write_binary_configured_priority_to_file: fix memory leak
  * saveConfigData(): fix file descriptor leak in while loop error case
  * loadConfigData(): use goto exit label to prevent file descriptor leaks
  * libalts_load_available_binaries: use goto err: label to fix leaks
  * loadAlternativeForBinary: goto-assisted error handling to avoid leaks
  * checkGroupConsistencies(): explicitly ignore unused `flags`
  * lib: refactor error handling of findAltConfig()
  * utils: fix possible memory leaks on error conditions
  * docs: fix some typos and grammar
  * Update README.md
  * lib: generally open[at] with O_CLOEXEC
  * Fix logic in options parser
  * Add basic Makefile for buidling without cmake
  * Added description for options=KeepArgv0
  * cmake: Express the dependency on CUnit correctly for building tests
  * cmake: Build and install CMake and PkgConfig files
  * cmake: Fix setup of shared linker flags
  * config.h: Fix the version to match the current latest tag
* Mon Nov 14 2022 dmueller@suse.com
- switch to a manual service rather than a buildtime tar service
  which introduces a bootstrap cycle between python and tar_scm
* Tue Sep 14 2021 adam.majer@suse.de
- Update to version v1.2+3.b848aad:
  * tests: fix array overrun in unit tests
* Tue Sep 14 2021 Jan Engelhardt <jengelh@inai.de>
- Use noun phrase for the summary; fix grammar in description.
* Mon Sep 13 2021 adam.majer@suse.de
- Update to version v1.2:
  * Add config option=KeepArgv0 to keep original argument during
    exec() call
* Mon Jun 21 2021 adam.majer@suse.de
- Update to version v1.1:
  * docs: Add broken manpage
  * tests: unit tests configs
  * tests: fix memory leak during tests
  * list_binaries: initialize pointer that is later used in free
  * libalts_exec_default() sets errno on not found
* Wed Jun  9 2021 adam.majer@suse.de
- Version v1.0.0:
  * Prefix symbols with libalts_ to avoid symbol clashes
  * Add symbol visibility and versioning
  * Decamelize public symbols and rename