#!/bin/sh
set -e

OPTIONS="--enable-gpl --disable-debug --disable-doc --enable-static --disable-shared"

if [ "$config_build_pic" = yes ]; then
    OPTIONS="$OPTIONS --enable-pic"
fi
if [ "$config_build_host" ]; then
    OPTIONS="$OPTIONS --cross-prefix=$config_build_host-"
fi

# Do FFmpeg's job.
case "$OPTIONS $*" in
*able-gnutls*|*able-libtls*|*able-openssl*|*able-mbedtls*|*able-schannel*|*ble-securetransport*)
    # ffmpeg auto-detect/enable mbedtls/schannel/securetransport, but are not.
    # the user can enable one of those manually to prevent our auto-enable.
    echo "Custom ssl/tls options found. Skipping auto-enable.";;
*)
    if pkg-config gnutls ; then
        OPTIONS="$OPTIONS --enable-gnutls"
        echo "Auto-enabling GnuTLS."
    elif pkg-config libtls ; then
        OPTIONS="$OPTIONS --enable-nonfree --enable-libtls"
        echo "Auto-enabling LibreSSL (creates a non-redistributable binary)."
    elif pkg-config openssl ; then
        OPTIONS="$OPTIONS --enable-nonfree --enable-openssl"
        echo "Auto-enabling OpenSSL (creates a non-redistributable binary)."
    fi;;
esac

if pkg-config aom &&
   ( "$config_ffmpeg_source"/configure --help 2>&1 | grep -q -e ble-libaom ) &&
   ! ( echo "$OPTIONS" "$@" | grep -q -e ble-libaom ) ;
then
    OPTIONS="$OPTIONS --enable-libaom"
    echo "Auto-enabling libaom."
fi

set -x
mkdir -p "$config_local_prefix"/tmp/ffmpeg_build
cd "$config_local_prefix"/tmp/ffmpeg_build
"$config_ffmpeg_source"/configure --prefix="$config_local_prefix" $OPTIONS "$@"
