#!/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 CHROMAPRINT, LADSPA, MMS, MODPLUG, OPENAL, H264, JPEG (openjpeg)

PKGNAME=gst-plugins-bad
PKGVERSION=1.16.0
PKGREVISION=1
PKGLICENSE="GPLv2"
PKGSUMMARY="gstreamer bad plugins"
PKGDOWNLOAD="https://gstreamer.freedesktop.org/src/gst-plugins-bad/$PKGNAME-$PKGVERSION.tar.xz"
PKGDEPENDS="dev/meson:build multimedia/gstreamer"
PKGOPTIONS="BLUETOOTH BZIP2 CURL DVD FAAC FAAD KMS SNDFILE WAYLAND X"

: ${CC:=clang}
: ${CFLAGS:=-O2}
: ${CXX:=clang++}
: ${CXXFLAGS:=-O2}
: ${LDFLAGS:=}
: ${BLUETOOTH:=yes}
: ${BZIP2:=yes}
: ${CURL:=yes}
: ${DVD:=yes}
: ${FAAC:=yes}
: ${FAAD:=yes}
: ${KMS:=yes}
: ${SNDFILE:=yes}
: ${WAYLAND:=yes}
: ${X:=yes}

if [ "$BLUETOOTH" = "yes" ]; then
	PKGDEPENDS="bluetooth/bluez $PKGDEPENDS"
	with_bluetooth="-D bluez=enabled"
else
	with_bluetooth="-D bluez=disabled"
fi

if [ "$BZIP2" = "yes" ]; then
	PKGDEPENDS="compression/bzip2 $PKGDEPENDS"
	with_bzip2="-D bz2=enabled"
else
	with_bzip2="-D bz2=disabled"
fi

if [ "$CURL" = "yes" ]; then
	PKGDEPENDS="network/curl $PKGDEPENDS"
	with_curl="-D curl=enabled"
else
	with_curl="-D curl=disabled"
fi

if [ "$DVD" = "yes" ]; then
	PKGDEPENDS="multimedia/libdvdnav multimedia/libdvdread $PKGDEPENDS"
	with_dvd="-D resindvd=enabled"
else
	with_dvd="-D resindvd=disabled"
fi

if [ "$FAAC" = "yes" ]; then
	PKGDEPENDS="audio/faac $PKGDEPENDS"
	with_faac="-D faac=enabled"
else
	with_faac="-D faac=disabled"
fi

if [ "$FAAD" = "yes" ]; then
	PKGDEPENDS="audio/faad2 $PKGDEPENDS"
	with_faac="-D faad=enabled"
else
	with_faac="-D faad=disabled"
fi

if [ "$KMS" = "yes" ]; then
	PKGDEPENDS="graphics/libdrm $PKGDEPENDS"
	with_kms="-D kms=enabled"
else
	with_kms="-D kms=disabled"
fi

if [ "$SNDFILE" = "yes" ]; then
	PKGDEPENDS="audio/libsndfile $PKGDEPENDS"
	with_sndfile="-D sndfile=enabled"
else
	with_sndfile="-D sndfile=disabled"
fi

if [ "$WAYLAND" = "yes" ]; then
	PKGDEPENDS="graphics/wayland graphics/wayland-protocols $PKGDEPENDS"
	with_wayland="-D wayland=enabled"
else
	with_wayland="-D wayland=disabled"
fi

if [ "$X" = "yes" ]; then
	PKGDEPENDS="x11/libx11 $PKGDEPENDS"
	with_x="-D x11=enabled"
else
	with_x="-D x11=disabled"
fi

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

	CC="$CC" \
	CFLAGS="$CFLAGS" \
	CXX="$CXX" \
	CXXFLAGS="$CXXFLAGS" \
	LDFLAGS="$LDFLAGS" \
	meson \
		--prefix / \
		--buildtype release \
		--default-library shared \
		. build \
		-D tests=disabled \
		$with_bluetooth \
		$with_bzip2 \
		$with_curl \
		$with_dvd \
		$with_faac \
		$with_faad \
		$with_kms \
		$with_sndfile \
		$with_wayland \
		$with_x
	CC="$CC" ninja -C build
	DESTDIR=$DESTDIR ninja -C build install

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

