#!/bin/bash

# Slackware build script for gst-plugins-bad-nonfree

# Copyright 2013-2021  Robby Workman, Tuscaloosa, Alabama, USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# 20220307 bkw: this is a rename and rework of the old SBo
# gst-plugins-bad. Here we build only the portions of gst-plugins-bad
# that are missing from Slackware's gst-plugins-bad-free. Despite
# the -nonfree in the name, not all of these are encumbered (some
# just aren't included in Slackware because it lacks the optional
# dependencies).

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=gst-plugins-bad-nonfree
VERSION=${VERSION:-1.18.5}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

SRCNAM=gst-plugins-bad

# 20220307 bkw: libsrtp is disabled by default because it currently breaks
# the build.
[ "${SRTP:-no}" = "no" ] && SRTPOPT="-Dsrtp=disabled"

# 20220307 bkw: nvcodec is disabled by default, per Robby's README.
[ "${CUDA:-no}" = "no" ] && CUDAOPT="-Dnvcodec=disabled"

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $SRCNAM-$VERSION
tar xvf $CWD/$SRCNAM-$VERSION.tar.xz
cd $SRCNAM-$VERSION
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

# 20220307 bkw: the massive list of -Dfoo=disabled below took a minute
# to put together. Notice that shm is *not* in the list, though it
# should be. This is because the build fails with -Dshm=disabled.
# So we build it, then remove it from $PKG after it's installed.

mkdir build
cd build
  CFLAGS="$SLKCFLAGS" \
  CXXFLAGS="$SLKCFLAGS" \
  meson .. \
    --buildtype=release \
    --infodir=/usr/info \
    --libdir=/usr/lib${LIBDIRSUFFIX} \
    --localstatedir=/var \
    --mandir=/usr/man \
    --prefix=/usr \
    --sysconfdir=/etc \
    -Dexamples=disabled \
    -Dopencv=disabled \
    $SRTPOPT \
    $CUDAOPT \
    -Daccurip=disabled \
    -Dadpcmdec=disabled \
    -Dadpcmenc=disabled \
    -Daiff=disabled \
    -Dasfmux=disabled \
    -Daudiobuffersplit=disabled \
    -Daudiofxbad=disabled \
    -Daudiomixmatrix=disabled \
    -Daudiolatency=disabled \
    -Daudiovisualizers=disabled \
    -Dautoconvert=disabled \
    -Dbayer=disabled \
    -Dcamerabin2=disabled \
    -Dcoloreffects=disabled \
    -Ddebugutils=disabled \
    -Ddvbsubenc=disabled \
    -Dfaceoverlay=disabled \
    -Dfestival=disabled \
    -Dfieldanalysis=disabled \
    -Dfreeverb=disabled \
    -Dfrei0r=disabled \
    -Dgaudieffects=disabled \
    -Dgdp=disabled \
    -Dgeometrictransform=disabled \
    -Did3tag=disabled \
    -Dinter=disabled \
    -Dinterlace=disabled \
    -Divfparse=disabled \
    -Divtc=disabled \
    -Djp2kdecimator=disabled \
    -Djpegformat=disabled \
    -Dlibrfb=disabled \
    -Dmidi=disabled \
    -Dmpegdemux=disabled \
    -Dmpegpsmux=disabled \
    -Dmpegtsdemux=disabled \
    -Dmpegtsmux=disabled \
    -Dmxf=disabled \
    -Dnetsim=disabled \
    -Drtp=disabled \
    -Dpcapparse=disabled \
    -Dpnm=disabled \
    -Dproxy=disabled \
    -Drawparse=disabled \
    -Dremovesilence=disabled \
    -Drist=disabled \
    -Drtmp2=disabled \
    -Drtp=disabled \
    -Dsdp=disabled \
    -Dsegmentclip=disabled \
    -Dsmooth=disabled \
    -Dspeed=disabled \
    -Dsubenc=disabled \
    -Dswitchbin=disabled \
    -Dtimecode=disabled \
    -Dvideoframe_audiolevel=disabled \
    -Dvideofilters=disabled \
    -Dvideoparsers=disabled \
    -Dvideosignal=disabled \
    -Dvmnc=disabled \
    -Dy4m=disabled \
    -Dbluez=disabled \
    -Ddecklink=disabled \
    -Ddvb=disabled \
    -Dfbdev=disabled \
    -Dipcpipeline=disabled \
    -Dkms=disabled \
    -Duvch264=disabled \
    -Dv4l2codecs=disabled \
    -Dva=disabled \
    -Dbz2=disabled \
    -Dclosedcaption=disabled \
    -Dcolormanagement=disabled \
    -Dcurl=disabled \
    -Ddash=disabled \
    -Ddtls=disabled \
    -Dfluidsynth=disabled \
    -Dhls=disabled \
    -Dneon=disabled \
    -Donvif=disabled \
    -Dopenal=disabled \
    -Dopencv=disabled \
    -Dopenexr=disabled \
    -Dopenjpeg=disabled \
    -Dopus=disabled \
    -Dresindvd=disabled \
    -Drsvg=disabled \
    -Dsbc=disabled \
    -Dsctp=disabled \
    -Dsmoothstreaming=disabled \
    -Dsndfile=disabled \
    -Dttml=disabled \
    -Dtranscode=disabled \
    -Dvulkan=disabled \
    -Dwayland=disabled \
    -Dwebrtc=disabled \
    -Dwebp=disabled
  "${NINJA:=ninja}"
  DESTDIR=$PKG $NINJA install
cd ..

# 20220307 bkw: remove all the things that already exist in Slackware.
rm -rf $PKG/usr/lib*/gstreamer-*/libgstshm.so \
       $PKG/usr/lib*/*.so* \
       $PKG/usr/lib*/girepository* \
       $PKG/usr/lib*/pkgconfig \
       $PKG/usr/share \
       $PKG/usr/include \
       $PKG/usr/bin

strip $PKG/usr/lib*/gstreamer-*/*.so

# 20220308 bkw: don't even include the docs. they already exist in Pat's
# gst-plugins-bad-free package.
PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC
cat $CWD/README_docs.txt > $PKGDOC/README_docs.txt
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
