#!/bin/bash

## Copyright (C) 2024 - 2025 ENCRYPTED SUPPORT LLC <adrelanos@whonix.org>
## See the file COPYING for copying conditions.

## Keep the command trace: it is this script's diagnostic output.
set -x
set -o errexit
set -o nounset
set -o pipefail
set -o errtrace
shopt -s inherit_errexit
shopt -s shift_verbose

if ! test -r /var/lib/repository-dist/derivative_apt_repository_opts ; then
  exit 0
fi

if test -e /var/lib/repository-dist/initializer.done ; then
  exit 0
fi

repository_dist_arguments="$(cat -- /var/lib/repository-dist/derivative_apt_repository_opts)"

## Deliberate word splitting: the file holds an option STRING, and the whole
## point is that it reaches repository-dist as separate arguments.
##
## 'set -o noglob' first, because an unquoted expansion is subject to PATHNAME
## EXPANSION as well as splitting -- a '*' or '?' in the options file would be
## replaced by matching filenames from the current directory. Splitting is
## wanted here; globbing never is.
##
## Note the inherent limit of this mechanism, which is not a defect to fix
## here: the file cannot express an option whose VALUE contains a space, since
## quotes in it are not honoured by word splitting.
set -o noglob
# shellcheck disable=SC2086
repository-dist ${repository_dist_arguments}
set +o noglob

touch -- /var/lib/repository-dist/initializer.done
