#!/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 the following options once the dependencies are available:
#
# OPENAL
# CHROMAPRINT
# FREI0R
# LADSPA
# AOM
# CACA
# CELT
# CDIO
# AAC (libfdk-aac)
# FLITE
# GME
# GSM
# MODPLUG
# OPENCV
# H264
# JPEG (openjpeg)
# SAMBA
# SNAPPY
# SOX
# TENSORFLOW
# MP2
# V4L
# VPX
# WAVPACK
# X264
# SDL2

PKGNAME=ffmpeg
PKGVERSION=4.2
PKGREVISION=1
PKGLICENSE="LGPLv21+"
PKGSUMMARY="cross-platform solution to convert audio and video"
PKGDOWNLOAD="https://ffmpeg.org/releases/$PKGNAME-4.2.tar.bz2"
PKGDEPENDS=""
PKGOPTIONS="ALSA
            BLURAY
            BZIP2
            CRYPTO
            FONTCONFIG
            FRIBIDI
            GMP
            JACK
            LZMA
            MP3
            OPENGL
            OPUS
            PULSEAUDIO
            SDL
            SPEEX
            SSH
            SSL
            SVG
            THEORA
            TTF
            VORBIS
            WEBP
            X
            X265
            XML
            ZLIB"

: ${ALSA:=yes}
: ${BLURAY:=no}
: ${BZIP2:=yes}
: ${CRYPTO:=no}
: ${FONTCONFIG:=no}
: ${FRIBIDI:=no}
: ${GMP:=no}
: ${JACK:=no}
: ${LZMA:=yes}
: ${MP3:=yes}
: ${OPENGL:=no}
: ${OPUS:=no}
: ${PULSEAUDIO:=no}
: ${SDL:=no}
: ${SPEEX:=no}
: ${SSH:=no}
: ${SSL:=no}
: ${SVG:=no}
: ${THEORA:=no}
: ${TTF:=no}
: ${VORBIS:=no}
: ${WEBP:=no}
: ${X265:=no}
: ${X:=no}
: ${XML:=no}
: ${ZLIB:=no}

if [ "$ALSA" = "yes" ]; then
	PKGDEPENDS="alsa-lib $PKGDEPENDS"
	with_alsa="--enable-alsa"
else
	with_alsa="--disable-alsa"
fi

if [ "$BLURAY" = "yes" ]; then
	PKGDEPENDS="libbluray $PKGDEPENDS"
	with_bluray="--enable-libbluray"
else
	with_bluray="--disable-libbluray"
fi

if [ "$BZIP2" = "yes" ]; then
	PKGDEPENDS="bzip2 $PKGDEPENDS"
	with_bzip2="--enable-bzlib"
else
	with_bzip2="--disable-bzlib"
fi

if [ "$CRYPTO" = "yes" ]; then
	PKGDEPENDS="libgcrypt $PKGDEPENDS"
	with_crypto="--enable-gcrypt"
else
	with_crypto="--disable-gcrypt"
fi

if [ "$FONTCONFIG" = "yes" ]; then
	PKGDEPENDS="fontconfig $PKGDEPENDS"
	with_fontconfig="--enable-fontconfig"
else
	with_fontconfig="--disable-fontconfig"
fi

if [ "$FRIBIDI" = "yes" ]; then
	PKGDEPENDS="fonts/fribidi $PKGDEPENDS"
	with_fribidi="--enable-libfribidi"
else
	with_fribidi="--disable-libfribidi"
fi

if [ "$GMP" = "yes" ]; then
	PKGDEPENDS="gmp $PKGDEPENDS"
	with_gmp="--enable-gmp"
else
	with_gmp="--disable-gmp"
fi

if [ "$JACK" = "yes" ]; then
	PKGDEPENDS="audio/jack $PKGDEPENDS"
	with_jack="--enable-libjack"
else
	with_jack="--disable-libjack"
fi

if [ "$LZMA" = "yes" ]; then
	PKGDEPENDS="xz $PKGDEPENDS"
	with_lzma="--enable-lzma"
else
	with_lzma="--disable-lzma"
fi

if [ "$MP3" = "yes" ]; then
	PKGDEPENDS="lame $PKGDEPENDS"
	with_mp3="--enable-libmp3lame"
else
	with_mp3="--disable-libmp3lame"
fi

if [ "$OPENGL" = "yes" ]; then
	PKGDEPENDS="mesa $PKGDEPENDS"
	with_opengl="--enable-opengl"
else
	with_opengl="--disable-opengl"
fi

if [ "$OPUS" = "yes" ]; then
	PKGDEPENDS="opus $PKGDEPENDS"
	with_opus="--enable-libopus"
else
	with_opus="--disable-libopus"
fi

if [ "$PULSEAUDIO" = "yes" ]; then
	PKGDEPENDS="pulseaudio $PKGDEPENDS"
	with_pulseaudio="--enable-libpulse"
else
	with_pulseaudio="--disable-libpulse"
fi

if [ "$SDL" = "yes" ]; then
	PKGDEPENDS="sdl2 $PKGDEPENDS"
	with_sdl="--enable-sdl2"
else
	with_sdl="--disable-sdl2"
fi

if [ "$SPEEX" = "yes" ]; then
	PKGDEPENDS="speex $PKGDEPENDS"
	with_speex="--enable-libspeex"
else
	with_speex="--disable-libspeex"
fi

if [ "$SSH" = "yes" ]; then
	PKGDEPENDS="libssh $PKGDEPENDS"
	with_ssh="--enable-libssh"
else
	with_ssh="--disable-libssh"
fi

if [ "$SSL" = "yes" ] || [ "$SSL" = "libressl" ]; then
	PKGDEPENDS="libressl $PKGDEPENDS"
	with_ssl="--enable-libtls"
elif [ "$SSL" = "gnutls" ]; then
	PKGDEPENDS="gnutls $PKGDEPENDS"
	with_ssl="--enable-gnutls"
else
	with_ssl="--disable-gnutls --disable-libtls --disable-openssl --disable-mbedtls"
fi

if [ "$SVG" = "yes" ]; then
	PKGDEPENDS="librsvg $PKGDEPENDS"
	with_svg="--enable-librsvg"
else
	with_svg="--disable-librsvg"
fi

if [ "$THEORA" = "yes" ]; then
	PKGDEPENDS="libtheora $PKGDEPENDS"
	with_theora="--enable-libtheora"
else
	with_theora="--disable-libtheora"
fi

if [ "$TTF" = "yes" ]; then
	PKGDEPENDS="freetype $PKGDEPENDS"
	with_ttf="--enable-libfreetype"
else
	with_ttf="--disable-libfreetype"
fi

if [ "$VORBIS" = "yes" ]; then
	PKGDEPENDS="libvorbis $PKGDEPENDS"
	with_vorbis="--enable-libvorbis"
else
	with_vorbis="--disable-libvorbis"
fi

if [ "$WEBP" = "yes" ]; then
	PKGDEPENDS="libwebp $PKGDEPENDS"
	with_webp="--enable-libwebp"
else
	with_webp="--disable-libwebp"
fi

if [ "$X265" = "yes" ]; then
	PKGDEPENDS="x265 $PKGDEPENDS"
	with_x265="--enable-libx265"
else
	with_x265="--disable-libx265"
fi

if [ "$X" = "yes" ]; then
	PKGDEPENDS="libx11 libxcb $PKGDEPENDS"
	with_x="--enable-xlib"
	with_x="--enable-libxcb $with_x"
	with_x="--enable-libxcb-shm $with_x"
	with_x="--enable-libxcb-xfixes $with_x"
	with_x="--enable-libxcb-shape $with_x"
else
	with_x="--disable-xlib"
	with_x="--disable-libxcb $with_x"
	with_x="--disable-libxcb-shm $with_x"
	with_x="--disable-libxcb-xfixes $with_x"
	with_x="--disable-libxcb-shape $with_x"
fi

if [ "$XML" = "yes" ]; then
	PKGDEPENDS="libxml2 $PKGDEPENDS"
	with_xml="--enable-libxml2"
else
	with_xml="--disable-libxml2"
fi

if [ "$ZLIB" = "yes" ]; then
	PKGDEPENDS="zlib $PKGDEPENDS"
	with_zlib="--enable-zlib"
else
	with_zlib="--disable-zlib"
fi

: ${CC:=clang}
: ${CFLAGS:=-O2}
: ${CXX:=clang++}
: ${CXXFLAGS:=-O2}
: ${LDFLAGS:=}
: ${LIBS:=}

rm -rf $PKGNAME-$PKGVERSION
tar xvf $PKGNAME-$PKGVERSION.tar.bz2
cd $PKGNAME-$PKGVERSION

sed -i -e 's|prefix_default="/usr/local"|prefix_default=|' configure
CC="$CC" \
CFLAGS="$CFLAGS" \
CXX="$CXX" \
CXXFLAGS="$CXXFLAGS" \
LDFLAGS="$LDFLAGS" \
LIBS="$LIBS" \
./configure \
	--enable-shared \
	--logfile=/dev/null \
	$with_alsa \
	$with_bluray \
	$with_bzip2 \
	$with_crypto \
	$with_fontconfig \
	$with_fribidi \
	$with_gmp \
	$with_jack \
	$with_lzma \
	$with_mp3 \
	$with_opengl \
	$with_opus \
	$with_pulseaudio \
	$with_sdl \
	$with_speex \
	$with_ssh \
	$with_ssl \
	$with_svg \
	$with_theora \
	$with_ttf \
	$with_vorbis \
	$with_webp \
	$with_x265 \
	$with_x \
	$with_xml \
	$with_zlib
make
make install DESTDIR=$DESTDIR

cd ..
rm -rf $PKGNAME-$PKGVERSION
