%{!?__global_ldflags: %global __global_ldflags -Wl,-z,relro}
%global ldconfig /sbin/ldconfig
%global ldconfig_post %{?ldconfig:%post -p %ldconfig}
%global ldconfig_postun %{?ldconfig:%postun -p %ldconfig}
%global ldconfig_scriptlets(n:) %{?ldconfig:\
%ldconfig_post %{?*} %{-n:-n %{-n*}}\
%end\
%ldconfig_postun %{?*} %{-n:-n %{-n*}}\
%end\
}

# not tagged or released
%global srcver 2.16.1
%global commit 18a9ed25c7ff79a7f4f4bf850c345c72179b8998
%global gitdate 20200316
%global shortcommit %(c=%{commit}; echo ${c:0:7})
%global githubproject l-smash/l-smash

%global libname liblsmash

Name:           l-smash
Version:        %{srcver}
Release:        0.%{gitdate}.git%{shortcommit}%{?dist}.4sunshine
Summary:        A set of isom/mov analysis and manipulation tools

License:        ISC
URL:            http://l-smash.github.io/l-smash/
Source0:        https://github.com/%{githubproject}/archive/%{commit}/%{name}-%{version}-%{shortcommit}.tar.gz
Source1:        http://%{name}.github.io/%{name}/restrictions.html
Patch0:         l-smash-2.16.1-optflags.02.patch
Patch1:         l-smash-2.16.1-preserve.patch
BuildRequires:  gcc

%description
L_SMASH provides a set of tools to analyze and manipulate isom/mov files,
notably of the mp4, mov, 3gp, 3g2, m4a, m4v formats.

%package        devel
Summary:        Development files for %{name}
Group:          Development/Libraries

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

%package        lib
Summary:        The %{name} library
Group:          Development/Libraries

%description    lib
The %{name}-lib package contains the libraries for applications
that use %{name}.

%prep
%setup -q -n %{name}-%{commit}
# obey our build flags
%patch0 -p1 -b .optflags
%patch1 -p1 -b .preserve

%build
# this package doesn't understand our %%configure
LDFLAGS="%{__global_ldflags}" \
    ./configure \
    --extra-cflags="%{optflags}" \
    --prefix=%{_prefix} \
    --exec-prefix=%{_exec_prefix} \
    --bindir=%{_bindir} \
    --includedir=%{_includedir} \
    --libdir=%{_libdir} \
    --enable-shared \
    --disable-static \
    --enable-debug # in order not to strip

make %{?_smp_mflags}

%install
make install DESTDIR=%{buildroot} INSTALL="install -p"
cp -p %{SOURCE1} .

%ldconfig_scriptlets

%{!?_licensedir:%global license %%doc}

%files
%license LICENSE
%doc restrictions.html
%{_bindir}/boxdumper
%{_bindir}/muxer
%{_bindir}/remuxer
%{_bindir}/timelineeditor

%files lib
%license LICENSE
%{_libdir}/%{libname}.so.*

%files devel
%license LICENSE
%{_includedir}/*.h
%{_libdir}/%{libname}.so
%{_libdir}/pkgconfig/%{libname}.pc

%changelog
* Mon Feb 22 2021 Moritz Barsnick <moritz+rpm@barsnick.net> 2.16.1-0.20200316.git18a9ed2.4sunshine
- modify the Makefile patch to also fix library permissions (must be
  executable for find-provides to properly work)

* Mon Apr 27 2020 Moritz Barsnick <moritz+rpm@barsnick.net> 2.16.1-0.20200316.git18a9ed2.3sunshine
- add restrictions.html to %%doc

* Wed Apr 22 2020 Moritz Barsnick <moritz+rpm@barsnick.net> 2.16.1-0.20200316.git18a9ed2.2sunshine
- adapt for F10
- use the %%__global_ldflags macro, and provide a fallback
- provide fallbacks for the %%license and %%ldconfig_scriptlets macros
- explicitly name all binaries

* Tue Apr 21 2020 Moritz Barsnick <moritz+rpm@barsnick.net> 2.16.1-0.20200316.git18a9ed2.1sunshine
- initial RPM