#!/usr/bin/env bash

set -euo pipefail
builddir="$(dirname "$0")"

pushd "$builddir"

rm -rf ./build ./build-10 ./build-12

if [ -d "./x265_git" ]; then
  git -C "./x265_git" pull
else
  git clone "https://bitbucket.org/multicoreware/x265_git.git"
fi

cmake -S x265_git/source -B build-12 \
  -DCMAKE_INSTALL_PREFIX='/home/rudism/.local' \
  -DCMAKE_ASM_NASM_FLAGS='-w-macro-params-legacy' \
  -DENABLE_ASSEMBLY='ON' \
  -DENABLE_LIBNUMA='ON' \
  -DHIGH_BIT_DEPTH='ON' \
  -DMAIN12='ON' \
  -DEXPORT_C_API='OFF' \
  -DENABLE_CLI='OFF' \
  -DENABLE_SHARED='OFF' \
  -Wno-dev
make -C build-12

cmake -S x265_git/source -B build-10 \
  -DCMAKE_INSTALL_PREFIX='/home/rudism/.local' \
  -DCMAKE_ASM_NASM_FLAGS='-w-macro-params-legacy' \
  -DENABLE_ASSEMBLY='ON' \
  -DENABLE_LIBNUMA='ON' \
  -DHIGH_BIT_DEPTH='ON' \
  -DEXPORT_C_API='OFF' \
  -DENABLE_CLI='OFF' \
  -DENABLE_SHARED='OFF' \
  -Wno-dev
make -C build-10

cmake -S x265_git/source -B build \
  -DCMAKE_INSTALL_PREFIX:PATH='/home/rudism/.local' \
  -DCMAKE_ASM_NASM_FLAGS='-w-macro-params-legacy' \
  -DENABLE_ASSEMBLY='ON' \
  -DENABLE_SHARED='ON' \
  -DENABLE_LIBNUMA='ON' \
  -DENABLE_HDR10_PLUS='ON' \
  -DEXTRA_LIB='x265_main10.a;x265_main12.a' \
  -DEXTRA_LINK_FLAGS='-L.' \
  -DLINKED_10BIT='ON' \
  -DLINKED_12BIT='ON' \
  -Wno-dev

ln -s ../build-10/libx265.a build/libx265_main10.a
ln -s ../build-12/libx265.a build/libx265_main12.a
make -C build

make -C build install

popd
