autoload -Uz log_error log_status log_info mkcd

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

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

if (( ! ${+project_root} )) {
  log_error "'project_root' not set. Please set before running ${0}."
  return 2
}

if (( ! ${+target} )) {
  log_error "'target' not set. Please set before running ${0}."
  return 2
}

local -a curl_opts=()
if (( ! ${+CI} )) {
    curl_opts+=(--progress-bar)
} else {
    curl_opts+=(--show-error --silent)
}
curl_opts+=(--location -O ${@})

pushd ${project_root}

local _qt_version
local _deployment_target
read -r _qt_version _deployment_target <<< \
  "$(jq -r --arg target "${target}" \
    '.platformConfig[$target] | { qtVersion, deploymentTarget } | join (" ")' \
    ${buildspec_file})"

typeset -g QT_VERSION=${_qt_version}
typeset -g DEPLOYMENT_TARGET=${_deployment_target}

if (( ! (${skips[(Ie)all]}  + ${skips[(Ie)deps]}) )) {
  mkdir -p ${project_root:h}/obs-build-dependencies

  local dependency
  local deps_version
  local deps_baseurl
  local deps_label
  local deps_hash
  local _filename
  local _url
  local _target
  local artifact_checksum

  for dependency ('prebuilt' "qt${QT_VERSION}") {
    IFS=';' read -r deps_version deps_baseurl deps_label deps_hash <<< \
      "$(jq -r --arg dependency "${dependency}" --arg target "${target}" \
        '.dependencies[$dependency] | {version, baseUrl, "label", "hash": .hashes[$target]} | join(";")' \
        ${buildspec_file})"

    if [[ -z "${deps_version}" ]] {
      log_error "No ${dependency} spec found in ${buildspec_file}."
      return 2
    }
    log_info "Setting up ${deps_label}..."

    pushd ${project_root:h}/obs-build-dependencies

    case ${dependency} {
      prebuilt)
        _filename="macos-deps-${deps_version}-${target##*-}.tar.xz"
        _url="${deps_baseurl}/${deps_version}/${_filename}"
        _target="plugin-deps-${deps_version}-qt${QT_VERSION}-${target##*-}"
        typeset -g OBS_DEPS_VERSION=${deps_version}
        ;;
      qt*)
        if (( ${+CI} )) {
          _filename="macos-deps-qt${QT_VERSION}-${deps_version}-universal.tar.xz"
          deps_hash="$(jq -r --arg dependency "${dependency}" \
            '.dependencies[$dependency].hashes["macos-universal"]' \
            ${buildspec_file})"
        } else {
          _filename="macos-deps-qt${QT_VERSION}-${deps_version}-${target##*-}.tar.xz"
        }
        _url="${deps_baseurl}/${deps_version}/${_filename}"
        _target="plugin-deps-${deps_version}-qt${QT_VERSION}-${target##*-}"
        ;;
    }

    if [[ ! -f ${_filename} ]] {
      log_debug "Running curl ${curl_opts} ${_url}"
      curl ${curl_opts} ${_url} && \
      log_status "Downloaded ${deps_label} for ${target}."
    } else {
      log_status "Found downloaded ${deps_label}"
    }

    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."

    if (( ! (${skips[(Ie)all]} + ${skips[(Ie)unpack]}) )) {
      mkdir -p ${_target} && pushd ${_target}

      XZ_OPT=-T0 tar -xzf ../${_filename} && log_status "${deps_label} extracted."
      popd
    }
  }

  popd
  pushd ${project_root:h}/obs-build-dependencies
  xattr -r -d com.apple.quarantine *
  log_status 'Removed quarantine flag from downloaded dependencies...'
  popd
} else {
  local deps_version
  read -r deps_version <<< \
    "$(jq -r '.dependencies.prebuilt.version' ${buildspec_file})"

  typeset -g OBS_DEPS_VERSION=${deps_version}
}
