autoload -Uz log_group log_error log_status log_info log_debug

if (( ! ${+commands[curl]} )) {
  log_error 'curl not available.'
  return 2
}

if (( ! ${+commands[jq]} )) {
  log_error 'jq not available.'
  return 2
}

local -a curl_opts=(--show-error --silent --location -O ${@})

pushd ${project_root}

log_group 'Installing obs-studio build dependencies...'

mkdir -p ${project_root}/.deps
local deps_version
local deps_baseurl
local deps_label
local deps_hash

IFS=';' read -r deps_version deps_baseurl deps_label deps_hash deps_revision <<< \
  "$(jq -r --arg target "${target}" '
    .configurePresets[]
    | select(.name=="dependencies")
    | .vendor["obsproject.com/obs-studio"].dependencies["cef"]
    | {version, baseUrl, "label", "hash": .hashes[$target], "revision": .revision[$target]}
    | join(";")
  ' ${project_root}/CMakePresets.json)"

if (( ! deps_version )) {
  log_error 'No valid cef spec found in buildspec.json.'
  return 2
}
log_group 'Setting up pre-built Chromium Embedded Framework...'

pushd ${project_root}/.deps
local _filename="cef_binary_${deps_version}_${target//ubuntu-/linux_}${deps_revision:+"_v${deps_revision}"}.tar.xz"
local _url=${deps_baseurl}/${_filename}
local _target="cef_binary_${deps_version}_${target//ubuntu-/linux_}"
typeset -g CEF_VERSION=${deps_version}

log_debug "Running curl ${curl_opts} ${_url}"
curl ${curl_opts} ${_url} && \
  log_status "Downloaded ${deps_label} for ${target}."

read -r artifact_checksum _ <<< "$(sha256sum ${_filename})"
if [[ ${deps_hash} != ${artifact_checksum} ]] {
  log_error "Checksum of downloaded ${deps_label} does not match specification.
Expected : ${deps_hash}
Actual   : ${artifact_checksum}"
  return 2
}
log_status "Checksum of downloaded ${deps_label} matches."
mkdir -p ${_target} && pushd ${_target}

XZ_OPT=-T0 tar --strip-components 1 -xJf ../${_filename} && log_status "${deps_label} extracted."
popd

log_group 'Installing obs-studio build dependencies from apt...'

sudo apt-get install -y --no-install-recommends \
  build-essential libglib2.0-dev \
  extra-cmake-modules lsb-release dh-cmake \
  libcurl4-openssl-dev \
  libavcodec-dev libavdevice-dev libavfilter-dev libavformat-dev libavutil-dev \
  libswresample-dev libswscale-dev \
  libjansson-dev \
  libx11-xcb-dev \
  libgles2-mesa-dev \
  libwayland-dev \
  libpipewire-0.3-dev \
  libpulse-dev \
  libx264-dev \
  libmbedtls-dev \
  libgl1-mesa-dev \
  libjansson-dev \
  uthash-dev \
  libsimde-dev \
  libluajit-5.1-dev python3-dev \
  libx11-dev libxcb-randr0-dev libxcb-shm0-dev libxcb-xinerama0-dev \
  libxcb-composite0-dev libxinerama-dev libxcb1-dev libx11-xcb-dev libxcb-xfixes0-dev \
  swig libcmocka-dev libxss-dev libglvnd-dev \
  libxkbcommon-dev libatk1.0-dev libatk-bridge2.0-dev libxcomposite-dev libxdamage-dev \
  libasound2-dev libfdk-aac-dev libfontconfig-dev libfreetype6-dev libjack-jackd2-dev \
  libpulse-dev libsndio-dev libspeexdsp-dev libudev-dev libv4l-dev libva-dev libvlc-dev \
  libpci-dev libdrm-dev \
  nlohmann-json3-dev libwebsocketpp-dev libasio-dev libqrcodegencpp-dev \
  libffmpeg-nvenc-dev librist-dev libsrt-openssl-dev \
  qt6-base-dev libqt6svg6-dev qt6-base-private-dev \
  libvpl-dev libvpl2
