#!/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.
#

# 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 OGG OPUS WAVE"

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

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

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

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

if [ "$WAVE" = "yes" ]; then
	PKGDEPENDS="audio/wave $PKGDEPENDS"
	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_opus \
		$with_ogg \
		$with_wave
	make
	make install DESTDIR=$DESTDIR
	find $DESTDIR -type f -name "*.la" -delete

	cd ..
	rm -rf SDL2_mixer-$PKGVERSION
}
