#!/bin/bash

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

## AI-Assisted

## Submodules without a matching <branch> on origin keep the SHA
## pin from the parent's index (see ci/checkout-fork-branch).

set -o errexit
set -o nounset
set -o pipefail
set -o errtrace
shopt -s inherit_errexit
shopt -s shift_verbose

if [ "$#" -ne 1 ]; then
   printf '%s\n' "usage: ${BASH_SOURCE[0]} <branch>" >&2
   exit 64
fi

branch="$1"

case "${branch}" in
   -*|*[!a-zA-Z0-9._/-]*|"")
      printf '%s\n' "${BASH_SOURCE[0]}: refusing suspicious branch name: '${branch}'" >&2
      exit 64
      ;;
esac

cd -- "$(dirname -- "$(readlink -f -- "${BASH_SOURCE[0]}")")/.."

## '$toplevel' is foreach's env, expanded by its child shell.
# shellcheck disable=SC2016
FORK_BRANCH="${branch}" \
   git submodule foreach --recursive '"$toplevel/ci/checkout-fork-branch"'
