#!/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 AALIB, JACK, LIBCACA, and others options.

PKGNAME=gst-plugins-good
PKGVERSION=1.16.0
PKGREVISION=1
PKGLICENSE="GPLv2"
PKGSUMMARY="gstreamer good plugins"
PKGDOWNLOAD="https://gstreamer.freedesktop.org/src/gst-plugins-good/$PKGNAME-$PKGVERSION.tar.xz"
PKGDEPENDS="dev/meson:build multimedia/gstreamer"
PKGOPTIONS="BZIP2 CAIRO FLAC GTK JPEG LAME LIBSOUP NLS OSS PNG PULSEAUDIO QT X"

: ${CC:=clang}
: ${CFLAGS:=-O2}
: ${CXX:=clang++}
: ${CXXFLAGS:=-O2}
: ${LDFLAGS:=}
: ${BZIP2:=yes}
: ${CAIRO:=yes}
: ${FLAC:=yes}
: ${GTK:=yes}
: ${JPEG:=yes}
: ${LAME:=yes}
: ${LIBSOUP:=yes}
: ${NLS:=yes}
: ${OSS:=yes}
: ${PNG:=yes}
: ${PULSEAUDIO:=yes}
: ${QT:=no}
: ${X:=yes}

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

if [ "$CAIRO" = "yes" ]; then
	PKGDEPENDS="graphics/cairo $PKGDEPENDS"
	with_cairo="-D cairo=enabled"
else
	with_cairo="-D cairo=disabled"
fi

if [ "$FLAC" = "yes" ]; then
	PKGDEPENDS="audio/flac $PKGDEPENDS"
	with_flac="-D flac=enabled"
else
	with_flac="-D flac=disabled"
fi

if [ "$GTK" = "yes" ]; then
	PKGDEPENDS="graphics/gtk graphics/gdk-pixbuf $PKGDEPENDS"
	with_gtk="-D gtk3=enabled -D gdk-pixbuf=enabled"
else
	with_gtk="-D gtk3=disabled -D gdk-pixbuf=disabled"
fi

if [ "$JPEG" = "yes" ]; then
	PKGDEPENDS="graphics/libjpeg-turbo $PKGDEPENDS"
	with_jpeg="-D jpeg=enabled"
else
	with_jpeg="-D jpeg=disabled"
fi

if [ "$LAME" = "yes" ]; then
	PKGDEPENDS="audio/lame $PKGDEPENDS"
	with_lame="-D lame=enabled"
else
	with_lame="-D lame=disabled"
fi

if [ "$NLS" = "yes" ]; then
	PKGDEPENDS="core/gettext:build $PKGDEPENDS"
	with_nls="-D nls=enabled"
else
	with_nls="-D nls=disabled"
fi

if [ "$OSS" = "yes" ]; then
	PKGDEPENDS="lib/linux-headers:build $PKGDEPENDS"
	with_oss="-D oss=enabled"
else
	with_oss="-D oss=disabled"
fi

if [ "$PNG" = "yes" ]; then
	PKGDEPENDS="lib/libpng $PKGDEPENDS"
	with_png="-D png=enabled"
else
	with_png="-D png=disabled"
fi

if [ "$PULSEAUDIO" = "yes" ]; then
	PKGDEPENDS="audio/pulseaudio $PKGDEPENDS"
	with_pulseaudio="-D pulse=enabled"
else
	with_pulseaudio="-D pulse=disabled"
fi

if [ "$QT" = "yes" ]; then
	PKGDEPENDS="qt/qtbase $PKGDEPENDS"
	with_qt="-D qt5=enabled"
else
	with_qt="-D qt5=disabled"
fi

if [ "$LIBSOUP" = "yes" ]; then
	PKGDEPENDS="network/libsoup $PKGDEPENDS"
	with_libsoup="-D soup=enabled"
else
	with_libsoup="-D soup=disabled"
fi

if [ "$X" = "yes" ]; then
	PKGDEPENDS="x11/libx11
	            x11/libxdamage
	            x11/libxext
	            x11/libxfixes $PKGDEPENDS"
	with_x="-D ximagesrc=enabled"
	with_x="-D ximagesrc-xshm=enabled $with_x"
	with_x="-D ximagesrc-xfixes=enabled $with_x"
	with_x="-D ximagesrc-xdmage=enabled $with_x"
else
	with_x="-D ximagesrc=disabled"
	with_x="-D ximagesrc-xshm=disabled $with_x"
	with_x="-D ximagesrc-xfixes=disabled $with_x"
	with_x="-D ximagesrc-xdmage=disabled $with_x"
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 \
		-D tests=disabled \
		$with_bzip2 \
		$with_cairo \
		$with_flac \
		$with_gtk \
		$with_jpeg \
		$with_lame \
		$with_libsoup \
		$with_nls \
		$with_oss \
		$with_png \
		$with_pulseaudio \
		$with_qt \
		$with_x \
		. build
	CC="$CC" ninja -C build
	DESTDIR=$DESTDIR ninja -C build install

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

