#!/bin/bash

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

## style-ok: allow-errexit-toggle -- 'set +e' is a deliberate global mode
## switch paired with the 'build_all_error_handler' ERR trap, so the handler
## can report and exit rather than errexit killing the script mid-flow.

set -x
set -o errexit
set -o nounset
set -o pipefail
set -o errtrace
shopt -s inherit_errexit
shopt -s shift_verbose
export LC_ALL=C

true "INFO: Currently running script: ${BASH_SOURCE[0]} $*"

MYDIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" && pwd )"

#cd -- "$MYDIR"

## XXX: hardcoded path
derivative_maker_source_code_dir="${HOME}/derivative-maker"
cd -- "${derivative_maker_source_code_dir}"

source "${derivative_maker_source_code_dir}/help-steps/pre"

build_all_error_handler() {
   : printf '%s\n' "
${red}${bold}BASH_COMMAND${reset}: ${BASH_COMMAND}
${red}${bold}ERROR ${BASH_SOURCE[0]}: | caller: $(caller)${reset}
"
   exit 1
}

trap "build_all_error_handler" ERR
set +e

## Debugging.
pwd

if [ "$(id -u)" = "0" ]; then
   error "Do not run this as root!"
fi

if [ ! "$(git status --porcelain)" = "" ]; then
   error "Command git status --porcelain failed at the beginning!"
fi

git fetch --jobs=200 -- origin

git verify-commit refs/remotes/origin/master

git merge --ff-only refs/remotes/origin/master

git submodule sync --recursive >/dev/null

#git submodule update --init --recursive --jobs=200 >/dev/null
git -c merge.verifySignatures=true submodule update --init --recursive --jobs=200 --merge >/dev/null

if [ ! "$(git status --porcelain)" = "" ]; then
   error "Command git status --porcelain failed at the end!"
fi

git describe
