#!/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=alsa-tools
PKGVERSION=1.1.7
PKGREVISION=1
PKGLICENSE="GPLv20+ LGPLv21+"
PKGSUMMARY="ALSA tools"
PKGDOWNLOAD="ftp://ftp.alsa-project.org/pub/tools/$PKGNAME-$PKGVERSION.tar.bz2"
PKGDEPENDS="alsa-lib"
PKGOPTIONS="TOOLS"

: ${CHOST:=$(uname -m)-linux-musl}
: ${CBUILD:=$(uname -m)-linux-musl}
: ${CC:=clang}
: ${CFLAGS:=-O2}
: ${CXX:=clang++}
: ${CXXFLAGS:=-O2}
: ${LDFLAGS:=}
: ${LIBS:=}
: ${TOOLS:=yes} # Note: space separated list of subdirectories.

if [ "$TOOLS" != "yes" ]; then
	with_tools="PROJECTS=$TOOLS"
fi

if [ -z "${TOOLS##*echomixer*}" ] || [ "$TOOLS" = "yes" ]; then
	PKGDEPENDS="gtk2 $PKGDEPENDS"
fi

if [ -z "${TOOLS##*envy24control*}" ] || [ "$TOOLS" = "yes" ]; then
	PKGDEPENDS="gtk2 $PKGDEPENDS"
fi

if [ -z "${TOOLS##*hdajackretask*}" ] || [ "$TOOLS" = "yes" ]; then
	PKGDEPENDS="gtk $PKGDEPENDS"
fi

if [ -z "${TOOLS##*hdajacksensetest*}" ] || [ "$TOOLS" = "yes" ]; then
	PKGDEPENDS="glib $PKGDEPENDS"
fi

if [ -z "${TOOLS##*hdspconf*}" ] || [ "$TOOLS" = "yes" ]; then
	PKGDEPENDS="fltk $PKGDEPENDS"
fi

if [ -z "${TOOLS##*hdspmixer*}" ] || [ "$TOOLS" = "yes" ]; then
	PKGDEPENDS="fltk $PKGDEPENDS"
fi

if [ -z "${TOOLS##*rmedigicontrol*}" ] || [ "$TOOLS" = "yes" ]; then
	PKGDEPENDS="gtk2 $PKGDEPENDS"
fi

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

#
# The SUBDIRS variable is used in recursive projects so we can't specify
# it directly otherwise it will be propagated to the underlying
# projects. Instead, rename the variable to PROJECTS.
#
# Also fix many errors that prevents correct handling of configure
# arguments and disable qlo10k1 which requires a deprecated Qt version.
#
patch -p0 < ../patch-makefile.patch

CC="$CC" \
CFLAGS="$CFLAGS" \
CXX="$CXX" \
CXXFLAGS="$CXXFLAGS" \
LDFLAGS="$LDFLAGS" \
LIBS="$LIBS" \
make \
	CONFIGURE_ARGS="--prefix= --bindir=/bin --sbindir=/bin" \
	DESTDIR=$DESTDIR \
	$with_tools \
	configure install
find $DESTDIR -type f -name "*.la" -delete

cd ..
rm -rf $PKGNAME-$PKGVERSION
