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

. ./openal-soft.info

: ${CC:=clang}
: ${CFLAGS:=-O2}
: ${CXX:=clang++}
: ${CXXFLAGS:=-O2}
: ${ALSA:=yes}
: ${EXAMPLES:=no}       # BROKEN: does not link.
: ${JACK:=yes}
: ${OSS:=yes}
: ${PULSEAUDIO:=yes}
: ${SDL2:=yes}
: ${UTILS:=yes}

if [ "$ALSA" = "yes" ]; then
	PKGDEPENDS="alsa-lib $PKGDEPENDS"
	with_alsa="-DALSOFT_REQUIRE_ALSA=On"
else
	with_alsa="-DALSOFT_REQUIRE_ALSA=Off"
fi

if [ "$EXAMPLES" = "yes" ]; then
	PKGDEPENDS="sdl2 ffmpeg $PKGDEPENDS"
	with_examples="-DALSOFT_EXAMPLES=On"
else
	with_examples="-DALSOFT_EXAMPLES=Off"
fi

if [ "$JACK" = "yes" ]; then
	PKGDEPENDS="jack2 $PKGDEPENDS"
	with_jack="-DALSOFT_REQUIRE_JACK=On"
else
	with_jack="-DALSOFT_REQUIRE_JACK=Off"
fi

if [ "$OSS" = "yes" ]; then
	with_oss="-DALSOFT_REQUIRE_OSS=On"
else
	with_oss="-DALSOFT_REQUIRE_OSS=Off"
fi

if [ "$PULSEAUDIO" = "yes" ]; then
	PKGDEPENDS="pulseaudio $PKGDEPENDS"
	with_pulseaudio="-DALSOFT_REQUIRE_PULSEAUDIO=On"
else
	with_pulseaudio="-DALSOFT_REQUIRE_PULSEAUDIO=Off"
fi

if [ "$SDL2" = "yes" ]; then
	# Don't register sdl2 twice.
	if [ "$EXAMPLES" != "yes" ]; then
		PKGDEPENDS="sdl2 $PKGDEPENDS"
	fi

	with_sdl2="-DALSOFT_REQUIRE_SDL2=On"
else
	with_sdl2="-DALSOFT_REQUIRE_SDL2=Off"
fi

if [ "$UTILS" = "yes" ]; then
	PKGDEPENDS="qtbase $PKGDEPENDS"
	with_utils="-DALSOFT_UTILS=On"
else
	with_utils="-DALSOFT_UTILS=Off"
fi

rm -rf $PKGNAME-$PKGVERSION
tar xvf $PKGNAME-$PKGVERSION.tar.bz2
cd $PKGNAME-$PKGVERSION

cd build
cmake .. \
	-DBUILD_SHARED_LIBS=On \
	-DCMAKE_BUILD_TYPE=Release \
	-DCMAKE_CXX_COMPILER="$CXX" \
	-DCMAKE_CXX_FLAGS="$CXXFLAGS" \
	-DCMAKE_C_COMPILER="$CC" \
	-DCMAKE_C_FLAGS="$CFLAGS" \
	-DCMAKE_INSTALL_PREFIX=/usr \
	-DCMAKE_INSTALL_LIBDIR=/lib \
	$with_alsa \
	$with_examples \
	$with_jack \
	$with_oss \
	$with_pulseaudio \
	$with_sdl2 \
	$with_utils
CC="$CC" \
CFLAGS="$CFLAGS" \
CXX="$CXX" \
CXXFLAGS="$CXXFLAGS" \
make
make install DESTDIR=$DESTDIR
cd ..

cd ..
rm -rf $PKGNAME-$PKGVERSION
