#!/bin/sh
#
# Copyright (c) 2019 David Demelier <markand@malikania.fr>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

PKGNAME=sdl2
PKGVERSION=2.0.10
PKGREVISION=1
PKGLICENSE="ZLIB"
PKGSUMMARY="Simple Directmedia Layer"
PKGDOWNLOAD="https://www.libsdl.org/release/SDL2-$PKGVERSION.tar.gz"
PKGOPTIONS="ALSA
            ATOMIC
            AUDIO
            DBUS
            DSO
            EVENTS
            FILE
            FILESYSTEM
            GLES1
            GLES2
            GLES3
            HAPTIC
            JACK
            JOYSTICK
            KMS
            OPENGL
            OSS
            POWER
            PULSEAUDIO
            RENDER
            SENSOR
            THREADS
            TIMER
            UDEV
            VIDEO
            VULKAN
            WAYLAND
            X"

# NAS, SAMPLERATE, IBUS

: ${CHOST:=$(uname -m)-linux-musl}
: ${CBUILD:=$(uname -m)-linux-musl}
: ${CC:=clang}
: ${CFLAGS:=-O2}
: ${CXX:=clang++}
: ${CXXFLAGS:=-O2}
: ${LDFLAGS:=}
: ${LIBS:=}
: ${ALSA:=yes}
: ${ATOMIC:=yes}
: ${AUDIO:=yes}
: ${DBUS:=yes}
: ${DSO:=yes}
: ${EVENTS:=yes}
: ${FILE:=yes}
: ${FILESYSTEM:=yes}
: ${GLES1:=yes}
: ${GLES2:=yes}
: ${GLES3:=yes}
: ${HAPTIC:=yes}
: ${JACK:=yes}
: ${JOYSTICK:=yes}
: ${KMS:=yes}
: ${OPENGL:=yes}
: ${OSS:=yes}
: ${POWER:=yes}
: ${PULSEAUDIO:=yes}
: ${RENDER:=yes}
: ${SENSOR:=yes}
: ${THREADS:=yes}
: ${TIMER:=yes}
: ${UDEV:=yes}
: ${VIDEO:=yes}
: ${VULKAN:=yes}
: ${WAYLAND:=yes}
: ${X:=yes}

if [ "$ALSA" = "yes" ]; then
	PKGDEPENDS="alsa-lib $PKGDEPENDS"
	with_alsa="--enable-alsa"
else
	with_alsa="--disable-alsa"
fi

if [ "$ATOMIC" = "yes" ]; then
	with_atomic="--enable-atomic"
else
	with_atomic="--disable-atomic"
fi

if [ "$AUDIO" = "yes" ]; then
	with_audio="--enable-audio"
else
	with_audio="--disable-audio"
fi

if [ "$DBUS" = "yes" ]; then
	PKGDEPENDS="network/dbus $PKGDEPENDS"
	with_dbus="--enable-dbus"
else
	with_dbus="--disable-dbus"
fi

if [ "$DSO" = "yes" ]; then
	with_dso="--enable-loadso"
else
	with_dso="--disable-loadso"
fi

if [ "$EVENTS" = "yes" ]; then
	with_events="--enable-events"
else
	with_events="--disable-events"
fi

if [ "$FILE" = "yes" ]; then
	with_file="--enable-file"
else
	with_file="--disable-file"
fi

if [ "$FILESYSTEM" = "yes" ]; then
	with_filesystem="--enable-filesystem"
else
	with_filesystem="--disable-filesystem"
fi

if [ "$GLES1" = "yes" ]; then
	with_gles1="--enable-video-opengles1"
else
	with_gles1="--disable-video-opengles1"
fi

if [ "$GLES2" = "yes" ]; then
	with_gles2="--enable-video-opengles2"
else
	with_gles2="--disable-video-opengles2"
fi

if [ "$GLES3" = "yes" ]; then
	with_gles3="--enable-video-opengles"
else
	with_gles3="--disable-video-opengles"
fi

if [ "$HAPTIC" = "yes" ]; then
	with_haptic="--enable-haptic"
else
	with_haptic="--disable-haptic"
fi

if [ "$JACK" = "yes" ]; then
	PKGDEPENDS="audio/jack $PKGDEPENDS"
	with_jack="--enable-jack"
else
	with_jack="--disable-jack"
fi

if [ "$JOYSTICK" = "yes" ]; then
	with_joystick="--enable-joystick"
else
	with_joystick="--disable-joystick"
fi

if [ "$KMS" = "yes" ]; then
	PKGDEPENDS="graphics/libdrm $PKGDEPENDS"
	with_kms="--enable-video-kmsdrm"
else
	with_kms="--disable-video-kmsdrm"
fi

if [ "$OPENGL" = "yes" ]; then
	PKGDEPENDS="graphics/mesa $PKGDEPENDS"
	with_opengl="--enable-video-opengl"
else
	with_opengl="--disable-video-opengl"
fi

if [ "$OSS" = "yes" ]; then
	with_oss="--enable-oss"
else
	with_oss="--disable-oss"
fi

if [ "$POWER" = "yes" ]; then
	with_power="--enable-power"
else
	with_power="--disable-power"
fi

if [ "$PULSEAUDIO" = "yes" ]; then
	PKGDEPENDS="audio/pulseaudio $PKGDEPENDS"
	with_pulseaudio="--enable-pulseaudio"
else
	with_pulseaudio="--disable-pulseaudio"
fi

if [ "$RENDER" = "yes" ]; then
	with_render="--enable-render"
else
	with_render="--disable-render"
fi

if [ "$SENSOR" = "yes" ]; then
	with_sensor="--enable-sensor"
else
	with_sensor="--disable-sensor"
fi

if [ "$THREADS" = "yes" ]; then
	with_threads="--enable-threads"
else
	with_threads="--disable-threads"
fi

if [ "$UDEV" = "yes" ]; then
	with_udev="--enable-libudev"
else
	with_udev="--disable-libudev"
fi

if [ "$VIDEO" = "yes" ]; then
	with_video="--enable-video"
else
	with_video="--disable-video"
fi

if [ "$VULKAN" = "yes" ]; then
	with_vulkan="--enable-video-vulkan"
else
	with_vulkan="--disable-video-vulkan"
fi

if [ "$X" = "yes" ]; then
	PKGDEPENDS="x11/libxext
	            x11/libxcursor
	            x11/libxinerama
	            x11/libxrandr
	            x11/libxscrnsaver
	            x1/libxi $PKGDEPENDS"
	with_x="--enable-video-x11"
	with_x="--enable-video-x11-scrnsaver $with_x"
	with_x="--enable-video-x11-xcursor $with_x"
	with_x="--enable-video-x11-xinerama $with_x"
	with_x="--enable-video-x11-xinput $with_x"
	with_x="--enable-video-x11-xrandr $with_x"
	with_x="--enable-video-x11-xshape $with_x"
else
	with_x="--disable-video-x11"
	with_x="--disable-video-x11-scrnsaver $with_x"
	with_x="--disable-video-x11-xcursor $with_x"
	with_x="--disable-video-x11-xdbe $with_x"
	with_x="--disable-video-x11-xinerama $with_x"
	with_x="--disable-video-x11-xinput $with_x"
	with_x="--disable-video-x11-xrandr $with_x"
	with_x="--disable-video-x11-xshape $with_x"
fi

build()
{
	rm -rf SDL2-$PKGVERSION
	tar xvf SDL2-$PKGVERSION.tar.gz
	cd SDL2-$PKGVERSION

	CC="$CC" \
	CFLAGS="$CFLAGS" \
	CXX="$CXX" \
	CXXFLAGS="$CXXFLAGS" \
	LDFLAGS="$LDFLAGS" \
	LIBS="$LIBS" \
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix= \
		$with_alsa \
		$with_atomic \
		$with_audio \
		$with_dbus \
		$with_dso \
		$with_events \
		$with_file \
		$with_filesystem \
		$with_gles1 \
		$with_gles2 \
		$with_gles3 \
		$with_haptic \
		$with_jack \
		$with_joystick \
		$with_kms \
		$with_opengl \
		$with_oss \
		$with_power \
		$with_pulseaudio \
		$with_render \
		$with_sensor \
		$with_threads \
		$with_timer \
		$with_udev \
		$with_video \
		$with_vulkan \
		$with_wayland \
		$with_x
	make
	make install DESTDIR=$DESTDIR
	find $DESTDIR -type f -name "*.la" -delete

	cd ..
	rm -rf SDL2-$PKGVERSION
}
