#!/usr/bin/env bash
set -euo pipefail

repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
source_root="$repo_root/build/dependencies-m1/source"
install_root="$repo_root/artifacts/m1"
log_root="$repo_root/build/dependencies-m1/logs"
mkdir -p "$install_root" "$log_root"

command -v make >/dev/null || { echo "make is required" >&2; exit 2; }
command -v nasm >/dev/null || { echo "nasm is required" >&2; exit 2; }
command -v cmake >/dev/null || { echo "cmake is required" >&2; exit 2; }

export PKG_CONFIG_PATH="$install_root/lib/pkgconfig"
export PATH="$install_root/bin:$PATH"
# The desktop host may itself run under GNU Make and expose a jobserver pipe
# which is not valid in this separate MSYS2 process tree.
unset MAKEFLAGS MFLAGS MAKELEVEL

if [ "${SMARTCUT_SKIP_X26X:-0}" != "1" ]; then
echo "Building x264 from the pinned source tree"
cd "$source_root/x264"
./configure --host=x86_64-w64-mingw32 --prefix="$install_root" \
  --enable-shared --disable-static --disable-cli --enable-pic \
  2>&1 | tee "$log_root/x264-configure.log"
make -j"${NUMBER_OF_PROCESSORS:-2}" 2>&1 | tee "$log_root/x264-build.log"
make install 2>&1 | tee "$log_root/x264-install.log"

echo "Building combined 8/10-bit x265 from the pinned source tree"
cd "$source_root/x265/source"
if grep -q "cmake_policy(SET CMP0025 OLD)" CMakeLists.txt; then
  sed -i "/if(POLICY CMP0025)/,/endif()/d" CMakeLists.txt
  sed -i "/if(POLICY CMP0054)/,/endif()/d" CMakeLists.txt
  sed -i "s/cmake_minimum_required (VERSION 2.8.8)/cmake_minimum_required (VERSION 3.5)/" CMakeLists.txt
fi
cd "$repo_root"
x265_10_build="$repo_root/build/dependencies-m1/x265-main10"
x265_8_build="$repo_root/build/dependencies-m1/x265-main"
rm -rf "$x265_10_build" "$x265_8_build"
cmake -S "$source_root/x265/source" -B "$x265_10_build" \
  -G "MSYS Makefiles" -DCMAKE_BUILD_TYPE=Release \
  -DCMAKE_INSTALL_PREFIX="$install_root" -DENABLE_SHARED=OFF -DENABLE_CLI=OFF -DHIGH_BIT_DEPTH=ON -DMAIN12=OFF -DEXPORT_C_API=OFF \
  -DCMAKE_C_COMPILER=x86_64-w64-mingw32-gcc \
  -DCMAKE_CXX_COMPILER=x86_64-w64-mingw32-g++ \
  2>&1 | tee "$log_root/x265-main10-configure.log"
cmake --build "$x265_10_build" --parallel 8 2>&1 | tee "$log_root/x265-main10-build.log"
cp "$x265_10_build/libx265.a" "$x265_10_build/libx265_main10.a"

cmake -S "$source_root/x265/source" -B "$x265_8_build" \
  -G "MSYS Makefiles" -DCMAKE_BUILD_TYPE=Release \
  -DCMAKE_INSTALL_PREFIX="$install_root" -DENABLE_SHARED=ON -DENABLE_CLI=OFF -DHIGH_BIT_DEPTH=OFF -DEXPORT_C_API=ON \
  -DEXTRA_LIB=x265_main10.a -DEXTRA_LINK_FLAGS="-L$x265_10_build" -DLINKED_10BIT=ON \
  -DCMAKE_C_COMPILER=x86_64-w64-mingw32-gcc \
  -DCMAKE_CXX_COMPILER=x86_64-w64-mingw32-g++ \
  2>&1 | tee "$log_root/x265-main-configure.log"
cmake --build "$x265_8_build" --parallel 8 2>&1 | tee "$log_root/x265-main-build.log"
cmake --install "$x265_8_build" 2>&1 | tee "$log_root/x265-main-install.log"
fi

echo "Building libaom with AV1 encoder and decoder"
aom_build="$repo_root/build/dependencies-m1/libaom"
rm -rf "$aom_build"
cmake -S "$source_root/libaom" -B "$aom_build" -G "MSYS Makefiles" \
  -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$install_root" \
  -DCMAKE_SYSTEM_NAME=Windows -DCMAKE_C_COMPILER=x86_64-w64-mingw32-gcc \
  -DCMAKE_CXX_COMPILER=x86_64-w64-mingw32-g++ -DAOM_TARGET_CPU=x86_64 \
  -DBUILD_SHARED_LIBS=OFF -DENABLE_DOCS=OFF -DENABLE_EXAMPLES=OFF \
  -DENABLE_TESTS=OFF -DENABLE_TOOLS=OFF -DCONFIG_AV1_ENCODER=1 -DCONFIG_AV1_DECODER=1 \
  2>&1 | tee "$log_root/libaom-configure.log"
cmake --build "$aom_build" --parallel 8 2>&1 | tee "$log_root/libaom-build.log"
cmake --install "$aom_build" 2>&1 | tee "$log_root/libaom-install.log"

echo "Building libvpx with VP9 high-bit-depth encoder and decoder"
cd "$source_root/libvpx"
make distclean >/dev/null 2>&1 || true
./configure --target=x86_64-win64-gcc --prefix="$install_root" \
  --disable-shared --enable-static --disable-vp8 --disable-examples --disable-tools \
  --disable-docs --disable-unit-tests --enable-vp9 --enable-vp9-highbitdepth \
  2>&1 | tee "$log_root/libvpx-configure.log"
make -j"${NUMBER_OF_PROCESSORS:-2}" 2>&1 | tee "$log_root/libvpx-build.log"
make install 2>&1 | tee "$log_root/libvpx-install.log"

echo "Codec build stage complete: $install_root"


