%define apiver      2.0
%define major       0

%if 0%{?cmake_build:1}
%global buildsystem cmake
%else
%global buildsystem autotools
%endif

# The naming scheme in non-Mandriva-based distros
%if 0%{!?mklibname:1}
%define mklibname(ds)  lib%{1}%{?2:%{2}}%{-s:-static}%{-d:-devel}
%endif
%if 0%{!?mkrel:1}
%define mkrel(c) %{1}%{?dist}
%endif

%define libname     %mklibname sdl %{apiver} %{major}
%define develname   %mklibname sdl %{apiver} -d

# CMake's build can't handle separate dynamic and static libraries, so only use
# it in autotools builds.
%if "%{buildsystem}" == "automake"
%define develsname  %mklibname sdl %{apiver} -d -s
%endif

Name:       sdl2
Summary:    Simple DirectMedia Layer 2
Version:    2.30.12
Release:    %mkrel 1
License:    Zlib
Group:      System/Libraries
URL:        https://libsdl.org/
Source0:    https://libsdl.org/release/SDL2-%{version}.tar.xz

%if "%{buildsystem}" == "cmake"
BuildRequires:  cmake
%else
BuildRequires:  autoconf, automake, libtool
%endif

BuildRequires:  pkgconfig(alsa)
BuildRequires:  pkgconfig(egl)
BuildRequires:  pkgconfig(gl)
BuildRequires:  pkgconfig(libudev)
BuildRequires:  pkgconfig(x11)
BuildRequires:  pkgconfig(xcursor)
BuildRequires:  pkgconfig(xext)
BuildRequires:  pkgconfig(xi)
BuildRequires:  pkgconfig(xinerama)
BuildRequires:  pkgconfig(xrandr)
BuildRequires:  pkgconfig(xscrnsaver)
BuildRequires:  pkgconfig(xxf86vm)
BuildRequires:  pkgconfig(libunwind)
BuildRequires:  pkgconfig(vulkan)
BuildRequires:  pkgconfig(wayland-client)
BuildRequires:  pkgconfig(wayland-cursor)
BuildRequires:  pkgconfig(wayland-egl)
BuildRequires:  pkgconfig(wayland-protocols)
BuildRequires:  pkgconfig(wayland-scanner)
BuildRequires:  pkgconfig(xkbcommon)
BuildRequires:  pkgconfig(libdrm)
BuildRequires:  pkgconfig(gbm)
BuildRequires:  pkgconfig(libdecor-0)
BuildRequires:  libnas-devel
BuildRequires:  pkgconfig(dbus-1)
BuildRequires:  pkgconfig(ibus-1.0)
BuildRequires:  pkgconfig(jack)
BuildRequires:  pkgconfig(libpulse-simple)
BuildRequires:  pkgconfig(libpipewire-0.3) >= 0.3.20
BuildRequires:  pkgconfig(samplerate)
BuildRequires:  pkgconfig(sndio)
Recommends:     libdecor

%description
This is the Simple DirectMedia Layer 2, a generic API that provides low
level access to audio, keyboard, mouse, and display framebuffer across
multiple platforms.

SDL fully supports high end 3D graphics using Direct3D and OpenGL,
and has a 2D graphics API for emulators and classic games.

%package -n %{libname}
Summary:    Main library for %{name}
Group:      System/Libraries

%description -n %{libname}
This package contains the library needed to run programs dynamically
linked with %{name}.

%package -n %{develname}
Summary:    Headers for developing programs that use %{name}
Group:      Development/C
Requires:   %{libname} = %{version}-%{release}
Provides:   %{name}-devel = %{version}-%{release}
Provides:   SDL2-devel = %{version}-%{release}
Requires:   alsa-lib-devel
Requires:   pkgconfig(gl)
Requires:   pkgconfig(glu)
Requires:   pkgconfig(egl)

%description -n %{develname}
This package contains the headers that programmers will need to develop
applications which will use %{name}.

%if "%{buildsystem}" == "autotools"
%package -n %{develsname}
Summary:    Static library for developing static programs that use %{name}
Group:      Development/C
Requires:   %{develname} = %{version}-%{release}
Provides:   %{name}-static-devel = %{version}-%{release}
Provides:    SDL2-static-devel = %{version}-%{release}

%description -n %{develsname}
This package contains the static library that programmers will need
to develop static applications which are using %{name}.
%endif

%prep
%autosetup -p1 -n SDL2-%{version}

%build

%if "%{buildsystem}" == "cmake"
%cmake \
    -DSDL_DLOPEN=ON \
    -DSDL_VIDEO_KMSDRM=ON \
    -DSDL_ARTS_SHARED=ON \
    -DSDL_DIRECTFB_SHARED=ON \
    -DSDL_ESD_SHARED=ON \
    -DSDL_FUSIONSOUND_SHARED=ON \
    -DSDL_NAS_SHARED=ON \
    -DSDL_LIBSAMPLERATE_SHARED=ON \
    -DSDL_SNDIO_SHARED=ON \
    -DSDL_PULSEAUDIO_SHARED=ON \
    -DSDL_JACK_SHARED=ON \
    -DSDL_PIPEWIRE_SHARED=ON \
    -DSDL_ALSA_SHARED=ON \
    -DSDL_VIDEO_WAYLAND=ON \
    -DSDL_LIBDECOR_SHARED=ON \
    -DSDL_VIDEO_VULKAN=ON \
    -DSDL_SSE3=OFF \
    -DSDL_RPATH=OFF \
    -DSDL_STATIC=ON \
    -DSDL_STATIC_PIC=ON
%cmake_build
%else
%configure \
	--enable-video-kmsdrm \
	--enable-alsa-shared \
	--enable-esd-shared \
	--enable-pipewire-shared \
	--enable-pulseaudio-shared \
	--enable-jack-shared \
	--enable-arts-shared \
	--enable-nas-shared \
	--enable-fusionsound-shared \
	--enable-sndio-shared \
	--enable-x11-shared \
	--enable-directfb-shared \
	--enable-fusionsound-shared \
	--enable-video-wayland \
	--enable-libdecor-shared \
	--enable-video-vulkan \
	--disable-rpath

%make_build
%endif

%install
%if "%{buildsystem}" == "cmake"
%cmake_install
rm -f %{buildroot}/%{_datadir}/licenses/SDL2/LICENSE.txt
%else
%make_install
%endif

%files -n %{libname}
%license LICENSE.txt
%doc BUGS.txt CREDITS.txt README-SDL.txt
%{_libdir}/libSDL2-%{apiver}.so.%{major}
%{_libdir}/libSDL2-%{apiver}.so.%{major}.*

%files -n %{develname}
%doc README.md WhatsNew.txt
%{_bindir}/*-config
%{_datadir}/aclocal/%{name}.m4
%{_includedir}/SDL2/
%{_libdir}/cmake/SDL2/
%{_libdir}/libSDL2.so
%{_libdir}/pkgconfig/%{name}.pc
%if "%{buildsystem}" == "cmake"
%{_libdir}/libSDL2-%{apiver}.so
%else

%files -n %{develsname}
%endif
%{_libdir}/libSDL2.a
%{_libdir}/libSDL2main.a
%{_libdir}/libSDL2_test.a