#!/bin/sh
#
# Copyright (c) 2019-2021 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.
#

# TODO: add MODPLUG, MP3 (libmad|mpg123) option

PKGNAME=sdl2-mixer
PKGVERSION=2.0.4
PKGREVISION=1
PKGLICENSE="ZLIB"
PKGSUMMARY="sound addon for SDL2"
PKGDOWNLOAD="https://www.libsdl.org/projects/SDL_mixer/release/SDL2_mixer-$PKGVERSION.tar.gz"
PKGDEPENDS="sdl2"
PKGOPTIONS="FLAC FLUIDSYNTH MP3 OGG OPUS WAVE"

: ${CHOST:=$(uname -m)-linux-musl}
: ${CBUILD:=$(uname -m)-linux-musl}
: ${CC:=clang}
: ${CFLAGS:=-O2}
: ${LDFLAGS:=}
: ${LIBS:=}
: ${FLAC:=yes}
: ${FLUIDSYNTH:=yes}
: ${MP3:=mad mpg123}
: ${OGG:=yes}
: ${OPUS:=yes}
: ${WAVE:=yes}

if [ "$FLAC" = "yes" ]; then
	PKGDEPENDS="flac $PKGDEPENDS"
	with_flac="--enable-music-flac --disable-music-flac-shared"
else
	with_flac="--disable-music-flac"
fi

if [ "$FLUIDSYNTH" = "yes" ]; then
	PKGDEPENDS="fluidsynth $PKGDEPENDS"
	with_fluidsynth="--enable-music-midi-fluidsynth --disable-music-midi-fluidsynth-shared"
else
	with_fluidsynth="--disable-music-midi-fluidsynth"
fi

if [ -z "${MP3##*mad*}" ] || [ "$MP3" = "yes" ]; then
	PKGDEPENDS="libmad $PKGDEPENDS"
	with_mad="--enable-music-mp3-mad-gpl"
else
	with_mad="--disable-music-mp3-mad-gpl"
fi

if [ -z "${MP3##*mpg123*}" ] || [ "$MP3" = "yes" ]; then
	PKGDEPENDS="mpg123 $PKGDEPENDS"
	with_mpg123="--enable-music-mp3-mpg123 --disable-music-mp3-mpg123-shared"
else
	with_mpg123="--disable-music-mp3-mpg123"
fi

if [ "$OPUS" = "yes" ]; then
	PKGDEPENDS="opus $PKGDEPENDS"
	with_opus="--enable-music-opus --disable-music-opus-shared"
else
	with_opus="--disable-music-opus"
fi

if [ "$OGG" = "yes" ]; then
	PKGDEPENDS="libogg $PKGDEPENDS"
	with_ogg="--enable-music-ogg --disable-music-ogg-shared"
else
	with_ogg="--disable-music-ogg"
fi

if [ "$WAVE" = "yes" ]; then
	with_wave="--enable-music-wave"
else
	with_wave="--disable-music-wave"
fi

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

	CC="$CC" \
	CFLAGS="$CFLAGS" \
	LDFLAGS="$LDFLAGS" \
	LIBS="$LIBS" \
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix= \
		$with_flac \
		$with_fluidsynth \
		$with_mad \
		$with_mpg123 \
		$with_opus \
		$with_ogg \
		$with_wave
	make
	make install DESTDIR=$DESTDIR
	find $DESTDIR -type f -name "*.la" -delete

	cd ..
	rm -rf SDL2_mixer-$PKGVERSION
}
