#!/bin/bash

#flags to comment out parts of the build

build_ffmpeg=true
build_redist=true

#--------------------------------------------------------
# command line parameters 
#--------------------------------------------------------

# build_type: debug. release
# bitness: 32, 64
# toolchain: mingw, linux

build_type=$1
bitness=$2
toolchain=$3

#--------------------------------------------------------
# FOLDERS CONFIG
#--------------------------------------------------------

fldr_build=$PWD/build
fldr_redist=$PWD/redist

fldr_src_ffmpeg=$PWD/ffmpeg-6.0

if [ "$bitness" = "32" ]
then
   bld_arch=x86
   lib_arch=X86
   flg_output=linux32 
else
   bld_arch=x86_64
   lib_arch=X64
   flg_output=linux64
fi
if [ "$bitness" = "arm64" ]
then
   bld_arch=aarch64
   lib_arch=aarch64
   flg_output=linuxARM64
fi


#--------------------------------------------------------
# TOOLCHAIN CONFIG
#--------------------------------------------------------

if [ "$toolchain" == "linux" ]
then
  fftoolchain=
  extralinkflags='--extra-ldflags=-Wl,-rpath=$ORIGIN'
#  extralinkflags=--extra-ldflags=-static-libgcc
  fldr_toolchain=linux
  extracflags=
  thread_flag=pthreads
  shared_lib_ext=so.*
  exe_ext=
  rpath=--enable-rpath
  sndio=--disable-sndio
else
  fftoolchain=
  extralinkflags=--extra-ldflags=-static-libgcc
  fldr_toolchain=mingw
  extracflags=
  thread_flag=w32threads
  shared_lib_ext=dll
  exe_ext=exe
  rpath=
  sndio=
fi

if test "$build_type" == "release"
then
   echo ++release
   debug_flags=--disable-debug

else
   echo ++debug
fi

#--------------------------------------------------------
# full paths
#--------------------------------------------------------

fld_build_full_path=$fldr_build/ffmpeg-$fldr_toolchain-$flg_output/$build_type
fld_redist_full_path=$fldr_redist/ffmpeg-$fldr_toolchain-$flg_output/$build_type

#--------------------------------------------------------
# trace
#--------------------------------------------------------


echo build_type=$build_type
echo bitness=$bitness
echo lib_arch=$lib_arch
echo fldr_build=$fldr_build
echo debug_flags=$debug_flags
echo fftoolchain=$fftoolchain
echo extracflags=$extracflags
echo fld_build_full_path=$fld_build_full_path
echo fld_redist_full_path=$fld_redist_full_path


#--------------------------------------------------------
# build FFMPEG
#--------------------------------------------------------

if [ "$build_ffmpeg" = "true" ]
then

#export PKG_CONFIG_PATH=/local/lib/pkgconfig
#export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:$fldr_src_ffmpeg/libzlib"

mkdir -p $fld_build_full_path

cd $fld_build_full_path

make clean

#MSYS2 Win build

$fldr_src_ffmpeg/configure $fftoolchain --arch=$bld_arch --enable-shared --enable-$thread_flag --enable-version3 --enable-libaom --logfile=log.txt $debug_flags $extracflags $extralinkflags $rpath $sndio

# do not use -j6 - conflict writing PDBs
if [ "$toolchain" == "linux" ]
then
  make -j6
else
  make
fi

fi #build_ffmpeg=true

#--------------------------------------------------------
# Package FFMPEG
#--------------------------------------------------------

if [ "$build_redist" = "true" ]
then

fldr_include=$fld_redist_full_path/include
fldr_lib=$fld_redist_full_path/lib
fldr_bin=$fld_redist_full_path/bin

echo include: $fldr_include

mkdir -p $fldr_include
mkdir -p $fldr_lib
mkdir -p $fldr_bin

cd $fldr_src_ffmpeg
find ./ -type f -name "*.h" -exec cp --parents {}  $fldr_include/"$name" \;
cd $fld_build_full_path
find ./ -name "*.$shared_lib_ext" -type f -exec cp {}  $fldr_bin \;
if [ "$toolchain" == "linux" ]
then
  find ./ -name "ffmpeg" -type f -exec cp {}  $fldr_bin \;
else 
  find ./ -name "*.$exe_ext" -type f -exec cp {}  $fldr_bin \;
  find ./ -name "*.pdb" -type f -exec cp {}  $fldr_bin \;
  find ./ -name "*.lib" -type f -exec cp {}  $fldr_lib \;
fi
cd $fld_build_full_path
find ./ -type f -name "*.h" -exec cp --parents {}  $fldr_include/"$name" \;

fi # build_redist=true

