function exit_error {
  for msg in "$@"
  do
    echo "$msg" > /dev/stderr
  done
  exit 1
}

function var_is_defined {
  for var in "$@"
  do
    test -n "${!var}" || exit_error "$var is not defined (see set-config* files)"
  done
}

function check_update_responses_repository_dir {
  test "$SIGNING_PROJECTNAME" = 'torvpn' && return 0
  if test -z "$update_responses_repository_dir" || ! test -d "$update_responses_repository_dir"
  then
    cat << 'EOF' > /dev/stderr
$update_responses_repository_dir is not defined, or the directory does not exist
You should clone git@gitlab.torproject.org:tpo/applications/tor-browser-update-responses.git
and set $update_responses_repository_dir in set-config.update-responses
EOF
    exit 1
  fi
}

function is_project {
  test "$1" = "$SIGNING_PROJECTNAME"
}

function generate_config {
  local tbb_version_type=$("$rbm" showconf browser var/browser_default_channel \
                           --target "$SIGNING_PROJECTNAME")
  local tbb_version=$("$rbm" showconf browser var/torbrowser_version \
                      --target "$SIGNING_PROJECTNAME" --target \
                      "$tbb_version_type")
  local tbb_version_build=$("$rbm" showconf browser var/torbrowser_build \
                            --target "$SIGNING_PROJECTNAME" --target \
                            "$tbb_version_type")
  local p1=$("$rbm" showconf browser var/project-name --target \
       "$SIGNING_PROJECTNAME")
  local p2=$("$rbm" showconf browser var/Project_Name --target \
             "$SIGNING_PROJECTNAME")
  local p3=$("$rbm" showconf browser var/ProjectName --target \
             "$SIGNING_PROJECTNAME")
  local p4=$("$rbm" showconf browser var/display_name --target \
       "$SIGNING_PROJECTNAME"  --target "$tbb_version_type")
  echo "SIGNING_PROJECTNAMES=(\"$p1\" \"$p2\" \"$p3\" \"$p4\")" > \
       "$script_dir/set-config.generated-config"
  for v in tbb_version_type tbb_version tbb_version_build; do
    echo "$v=$(shell-quote "${!v}")" >> "$script_dir/set-config.generated-config"
  done
}

function project-name {
  echo "${SIGNING_PROJECTNAMES[0]}"
}

function Project_Name {
  echo "${SIGNING_PROJECTNAMES[1]}"
}

function ProjectName {
  echo "${SIGNING_PROJECTNAMES[2]}"
}

function display_name {
  echo "${SIGNING_PROJECTNAMES[3]}"
}

function rbm_showconf {
  "$rbm" showconf release "$1" --target "$SIGNING_PROJECTNAME" \
                   --target "$tbb_version_type"
}

function rbm_showconf_boolean {
  local res=$(rbm_showconf "$1")
  if [ -z "$res" ] || [ "a$res" = "a0" ]; then
    return
  fi
  echo '1'
}

function setup_dmg_tools {
  dmgtools_dir=$(mktemp -d)
  trap "rm -Rf $dmgtools_dir" EXIT
  local libdmg_file="$script_dir/../../out/libdmg-hfsplus/libdmg-hfsplus-d6287b5afc24-6f206c.tar.zst"
  test -f "$libdmg_file" || \
    exit_error "$libdmg_file is missing." \
      "You can build it with:" \
      "  ./rbm/rbm build --target no_containers libdmg-hfsplus" \
      "See var/deps in projects/libdmg-hfsplus/config for the list of build dependencies"
  local hfstools_file="$script_dir/../../out/hfsplus-tools/hfsplus-tools-540.1.linux3-2acaa4.tar.zst"
  test -f "$hfstools_file" || \
    exit_error "$hfstools_file is missing." \
      "You can build it with:" \
      "  ./rbm/rbm build --target no_containers hfsplus-tools" \
      "You will need the clang and uuid-dev packages installed"
  tar -C "$dmgtools_dir" -xf "$libdmg_file"
  tar -C "$dmgtools_dir" -xf "$hfstools_file"
  export PATH="$PATH:$dmgtools_dir/libdmg-hfsplus:$dmgtools_dir/hfsplus-tools"
}

. "$script_dir/set-config"
