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}

typeset -g QT_VERSION
read -r QT_VERSION <<< \
  "$(jq -r --arg target "${target}" \
    '.platformConfig[$target] | { qtVersion } | join(" ")' \
    ${buildspec_file})"

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}" \
    '.dependencies["cef"] | {version, baseUrl, "label", "hash": .hashes[$target], "revision": .revision[$target]} | join(";")' \
    ${buildspec_file})"

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//-/_}${deps_revision:+"_v${deps_revision}"}.tar.xz"
local _url=${deps_baseurl}/${_filename}
local _target="cef_binary_${deps_version}_${target//-/_}"
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

local suffix
if [[ ${CPUTYPE} != ${target##*-} ]] {
  local -A arch_mappings=(
    aarch64 arm64
    x86_64 amd64
  )

  suffix=":${arch_mappings[${target##*-}]}"
  sudo apt-get install -y --no-install-recommends \
    gcc-${${target##*-}//_/-}-linux-gnu g++-${${target##*-}//_/-}-linux-gnu
}

sudo apt-get install -y --no-install-recommends \
  build-essential \
  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 libgles2-mesa \
  libwayland-dev \
  libpipewire-0.3-dev \
  libpulse-dev \
  libx264-dev \
  libmbedtls-dev \
  libgl1-mesa-dev \
  libjansson-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 \
  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 libvpl-dev libvpl2 libqrcodegencpp-dev

if (( UBUNTU_2210_OR_LATER )) sudo apt-get install -y --no-install-recommends librist-dev libsrt-openssl-dev

local -a _qt_packages=()

if (( QT_VERSION == 6 )) {
  _qt_packages+=(
    qt6-base-dev
    libqt6svg6-dev
    qt6-base-private-dev
  )
} else {
  log_error "Unsupported Qt version '${QT_VERSION}' specified."
  return 2
}

sudo apt-get install -y --no-install-recommends ${_qt_packages}
