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

PKGNAME=cdrdao
PKGVERSION=1.2.4
PKGREVISION=1
PKGLICENSE="GPL-2.0-only"
PKGSUMMARY="write audio/data CD-Rs in disk-at-once mode"
PKGDOWNLOAD="https://sourceforge.net/projects/cdrdao/files/$PKGNAME-$PKGVERSION.tar.bz2"
PKGDEPENDS=""
PKGOPTIONS="MP3 OGG"

: ${CHOST:=$(uname -m)-linux-musl}
: ${CBUILD:=$(uname -m)-linux-musl}
: ${CC:=clang}
: ${CFLAGS:=-O2}
: ${CXX:=clang++}
: ${CXXFLAGS:=-O2}
: ${LDFLAGS:=}
: ${LIBS:=}
: ${MP3:=lame mad}      # Note: can be lame, mad or yes (both).

if [ "$OGG" = "yes" ]; then
	PKGDEPENDS="libvorbis $PKGDEPENDS"
	with_ogg="--with-ogg-support"
else
	with_ogg="--without-ogg-support"
fi

if [ -z "${MP3##*mad*}" ] || [ "$MP3" = "yes" ]; then
	PKGDEPENDS="libmad $PKGDEPENDS"
	with_mad="--with-mp3-support"
else
	with_mad="--without-mp3-support"
fi

if [ -z "${MP3##*lame*}" ] || [ "$MP3" = "yes" ]; then
	PKGDEPENDS="lame $PKGDEPENDS"
	with_lame="--with-lame"
else
	with_lame="--without-lame"
fi

build()
{
	rm -rf $PKGNAME-$PKGVERSION
	tar xvf $PKGNAME-$PKGVERSION.tar.bz2
	cd $PKGNAME-$PKGVERSION

	# TODO: --without-gcdmaster: missing dependencies.
	CC="$CC" \
	CFLAGS="$CFLAGS" \
	CXX="$CXX" \
	CXXFLAGS="$CXXFLAGS" \
	LDFLAGS="$LDFLAGS" \
	LIBS="$LIBS" \
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix= \
		--without-gcdmaster \
		$with_ogg \
		$with_mad \
		$with_lame
	make
	make install DESTDIR=$DESTDIR

	cd ..
	rm -rf $PKGNAME-$PKGVERSION
}
