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 {
  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'
}

. "$script_dir/set-config"
