#!/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, MODPLUG, LADSPA

PKGNAME=gst-plugins-bad
PKGVERSION=1.16.0
PKGREVISION=1
PKGLICENSE="GPLv20"
PKGSUMMARY="gstreamer bad plugins"
PKGDOWNLOAD="https://gstreamer.freedesktop.org/src/gst-plugins-bad/$PKGNAME-$PKGVERSION.tar.xz"
PKGDEPENDS="gst-plugins-base gstreamer meson:build"
PKGOPTIONS="BLUETOOTH
            BZIP2
            CURL
            DVD
            FAAC
            FAAD
            FLUIDSYNTH
            H264
            H265
            JPEG
            KMS
            MMS
            OPENAL
            OPUS
            SNDFILE
            SSL
            WAYLAND
            WEBCAM
            X"

: ${CC:=clang}
: ${CFLAGS:=-O2}
: ${CXX:=clang++}
: ${CXXFLAGS:=-O2}
: ${LDFLAGS:=}
: ${BLUETOOTH:=yes}
: ${BZIP2:=yes}
: ${CURL:=yes}
: ${DVD:=yes}
: ${FAAC:=yes}
: ${FAAD:=yes}
: ${FLUIDSYNTH:=yes}
: ${H264:=no}           # BROKEN: does not compile with our version of openh264
: ${H265:=yes}
: ${JPEG:=yes}
: ${KMS:=yes}
: ${MMS:=yes}
: ${OPENAL:=yes}
: ${OPUS:=yes}
: ${SNDFILE:=yes}
: ${SSL:=yes}
: ${WAYLAND:=yes}
: ${WEBCAM:=yes}
: ${X:=yes}

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

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

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

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

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

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

if [ "$FLUIDSYNTH" = "yes" ]; then
	PKGDEPENDS="fluidsynth $PKGDEPENDS"
	with_fluidsynth="-D fluidsynth=enabled"
else
	with_fluidsynth="-D fluidsynth=disabled"
fi

if [ "$H264" = "yes" ]; then
	PKGDEPENDS="openh264 $PKGDEPENDS"
	with_h264="-D openh264=enabled"
else
	with_h264="-D openh264=disabled"
fi

if [ "$H265" = "yes" ]; then
	PKGDEPENDS="x265 $PKGDEPENDS"
	with_h265="-D x265=enabled"
else
	with_h265="-D x265=disabled"
fi

if [ "$JPEG" = "yes" ]; then
	PKGDEPENDS="openjpeg $PKGDEPENDS"
	with_jpeg="-D openjpeg=enabled"
else
	with_jpeg="-D openjpeg=disabled"
fi

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

if [ "$MMS" = "yes" ]; then
	PKGDEPENDS="libmms $PKGDEPENDS"
	with_mms="-D libmms=enabled"
else
	with_mms="-D libmms=disabled"
fi

if [ "$OPENAL" = "yes" ]; then
	PKGDEPENDS="openal-soft $PKGDEPENDS"
	with_openal="-D openal=enabled"
else
	with_openal="-D openal=disabled"
fi

if [ "$OPUS" = "yes" ]; then
	PKGDEPENDS="opus $PKGDEPENDS"
	with_opus="-D opus=enabled"
else
	with_opus="-D opus-disabled"
fi

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

if [ "$SSL" = "yes" ]; then
	# HLS support several alternatives (libressl, nettle, gcrypto) but since
	# dtls explicitly require libressl, let's use it for HLS too.
	PKGDEPENDS="libressl $PKGDEPENDS"
	with_ssl="-D dtls=enabled -D hls=enabled -D hls-crypto=openssl"
else
	with_ssl="-D dtls=disabled -Dhls=disabled"
fi

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

if [ "$WEBCAM" = "yes" ]; then
	PKGDEPENDS="eudev libusb $PKGDEPENDS"
	with_webcam="-D uvch264=enabled"
else
	with_webcam="-D uvch264=disabled"
fi


if [ "$X" = "yes" ]; then
	PKGDEPENDS="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 . build \
		--prefix / \
		--buildtype release \
		--default-library shared \
		-D tests=disabled \
		$with_bluetooth \
		$with_bzip2 \
		$with_curl \
		$with_dvd \
		$with_faac \
		$with_faad \
		$with_fluidsynth \
		$with_h264 \
		$with_h265 \
		$with_jpeg \
		$with_kms \
		$with_mms \
		$with_openal \
		$with_opus \
		$with_sndfile \
		$with_ssl \
		$with_wayland \
		$with_webcam \
		$with_x
	CC="$CC" ninja -C build
	DESTDIR=$DESTDIR ninja -C build install
	sed -i -e "s|prefix=/|prefix=|" $DESTDIR/lib/pkgconfig/*.pc

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