#!/bin/bash
set -eu

if [ -f mk -a -d ffmpeg ]; then
	echo "Cowardly refusing to configure in root ffmpegbuild directory.  Run again from an empty directory."
	exit
fi

builddir=$(pwd)
top=$(cd $(dirname $0); pwd)

### Config

# Set to yes if you want ffmpeg.exe/ffprobe.exe that work with typical input files. Useful for testing.
# For official builds we want this disabled to minimize redistributable DLL size.
enablecmdline=
buildtype=release
pkgonly=

for arg in "$@"; do
	case $arg in
		32|64) ;; # relying on Platform set by vsvars.bat
		debug|release) buildtype=$arg ;;
		tools) enablecmdline=yes ;;
		pkg) pkgonly=yes ;;
		*) echo "Unknown argument '$arg'"; exit 1 ;;
	esac
done

if [ "$Platform" = "x64" ]; then
	arch=x86_64
	os=win64
	mfxlib=x64
else
	arch=x86
	os=win32
	mfxlib=win32
fi
minimumcpu=core2


package()
{
	local zip="$1"
	local wrapdir="${zip/.zip/}"
	local outdir="$builddir/pkg"
	local tmpdir=$outdir/$wrapdir

	[ -f "$zip" ] && rm -rf "$zip"
	[ -e "$outdir" ] && rm -rf "$outdir"
	mkdir -p "$tmpdir"

	cp "$top/ffmpeg/COPYING.LGPLv2.1" "$tmpdir/FFmpeg-license.txt"
	cp "$top/meson.build.in" "$tmpdir/meson.build"
	mkdir -p "$tmpdir/lib"
	mkdir -p "$tmpdir/include/libavcodec"
	mkdir -p "$tmpdir/include/libavutil"
	mkdir -p "$tmpdir/include/libavformat"
	cp -r $top/ffmpeg/libavcodec/*.h   "$tmpdir/include/libavcodec/"
	cp -r $top/ffmpeg/libavutil/*.h    "$tmpdir/include/libavutil/"
	cp -r $top/ffmpeg/libavformat/*.h  "$tmpdir/include/libavformat/"
	cp -r libavutil/*.h  "$tmpdir/include/libavutil/"
	for f in libavcodec/avcodec{.lib,-59.def,-59.dll,-59.pdb} libavutil/avutil{.lib,-57.def,-57.dll,-57.pdb}; do
		cp "$f" "$tmpdir/lib"
	done
	cd "$outdir"
	zip -q -r "$zip" .
	echo "Wrote $zip"
}

artifactory_upload()
{
	local plat=windows-$arch
	local arturl=https://artifactory.opentext.net/artifactory/etx/ffmpeg
	while [ true ]; do
		echo -n "FFmpeg build (YYYY-MM-DD_NN) where NN starts at 01 and increments: "
		read ffmpegbuild
		echo $ffmpegbuild|grep "^20[0-9][0-9]-[0-1][0-9]-[0-3][0-9]_[0-9][0-9]$" > /dev/null && break
		echo "Bad format, try again"
	done
	local artzip=ffmpeg-$ffmpegbuild-$plat-$buildtype.zip
	package "$artzip"
	echo -n "Artifactory credential string (e.g. user:CN83lakb8qmvXY): "
	read creds
	echo "Will upload $artzip"
	echo -n "Continue? [y/N]: "
	read continue
	if [ "$continue" != "y" ]; then
		exit
	fi
	curl -# "-u$creds" -T "$artzip" --url "$arturl/$artzip"

	echo "Uploaded to $arturl/$artzip"
}

# PATH settings are not enough; we need something more from vcvars.bat
#export PATH="/c/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.24.28314/bin/Hostx64/x64":$PATH

mkdir -p "$top/pkgconfig"

sed "s|@@PREFIX@@|$(cygpath -m $(cd $top/nv-codec-headers; pwd))|" $top/nv-codec-headers/ffnvcodec.pc.in > $top/pkgconfig/ffnvcodec.pc
sed "s|@@PREFIX@@|$(cygpath -m $(cd $top/mfx; pwd))|" $top/mfx/libmfx.pc.in > $top/pkgconfig/libmfx.pc

export PKG_CONFIG_PATH="$top/pkgconfig"

amfdir=$(cygpath -m $top/amf)
mfxdir=$(cygpath -m $top/mfx)

# libmfx.pc requires a Libs line with .lib in it, which ffmpeg/configure:test_ld() doesn't filter out,
# so it ends up calling cl.exe with linker arguments, and fails.  Rather than modify upstream ffmpeg,
# we use the non-pkg-config method to find libmfx.
mfxflags="--extra-cflags=-I$mfxdir/include --extra-ldflags=-LIBPATH:$mfxdir/lib/$mfxlib --extra-ldflags=legacy_stdio_definitions.lib"

extraflags="--disable-programs --disable-autodetect --disable-everything"
if [ "$enablecmdline" = "yes" ]; then
	extraflags=--enable-protocol=file
fi

if [ "$buildtype" = "debug" ]; then
	extraflags="$extraflags --extra-cflags=-Zi --extra-ldflags=-DEBUG --disable-optimizations --enable-debug"
fi

### Run

if [ -n "$pkgonly" ]; then
	artifactory_upload
	exit
else
	which cl
	which link
	which yasm
fi

time $top/ffmpeg/configure \
	--target-os=$os \
	--arch=$arch \
	--toolchain=msvc \
	--enable-asm \
	--enable-x86asm \
	--disable-static \
	--enable-shared \
	--extra-cflags="-I$(cygpath -m $top/amf/include)" \
	$mfxflags \
	--disable-doc \
	$extraflags \
	--enable-w32threads \
	--enable-encoder=h264_amf,h264_nvenc_h264_qsv,nvenc_h264,nvenc_ \
	--enable-decoder=h264_,h264_cuvid,h264_qsv,nv \
	--enable-hwaccel=h264_d3d11va,h264_d3d11va2,h264_dxva2,h264_nvdec \
	--enable-parser=h264 \
	--enable-amf \
	--enable-d3d11va \
	--enable-dxva2 \
	--enable-ffnvcodec \
	--enable-libmfx \
	--enable-nvdec \
	--enable-nvenc \


time make -j8

artifactory_upload

# Interesting options
# --enable-mmal           # ARM acceleration
# --cpu=$minimumcpu       # Better perf if we can put a higher-than-default lower limit
# --enable-openh264       # Compare with video lib's own openh264; maybe can delete if comparable
