#!/usr/bin/env bash
# Codemagic supplies these variables from the private_git_credentials group.
set +x
set -eu

: "${FORGEJO_USERNAME:?Set FORGEJO_USERNAME in private_git_credentials}"
: "${FORGEJO_TOKEN:?Set FORGEJO_TOKEN in private_git_credentials}"

# Keep credentials out of clone URLs and files. The helper applies only to this
# HTTPS host and is inherited by Git's recursive submodule clone processes.
GIT_TERMINAL_PROMPT=0 git \
  -c credential.helper= \
  -c 'credential.https://git.friedcutlet.com.helper=!f() {
    if [ "$1" = get ]; then
      printf "username=%s\npassword=%s\n" "$FORGEJO_USERNAME" "$FORGEJO_TOKEN"
    fi
  }; f' \
  submodule update --init --recursive
