#!/bin/bash

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

# shellcheck source=../../../../helper-scripts/usr/libexec/helper-scripts/check_runtime.bsh
source /usr/libexec/helper-scripts/check_runtime.bsh

if was_executed "${BASH_SOURCE[0]}"; then
   set -o errexit
   set -o nounset
   set -o errtrace
   set -o pipefail
   shopt -s inherit_errexit
   shopt -s shift_verbose
fi

## shellcheck source=../../../../helper-scripts/usr/libexec/helper-scripts/log_run_die.sh
source /usr/libexec/helper-scripts/log_run_die.sh

main() {
   tb_updater_uid="$(id -u tb-updater)"
   if [ -z "${tb_updater_uid}" ]; then
      log error "Unable to get UID of account 'tb-updater'! Exiting."
      exit 1
   fi
   tb_updater_gid="$(id -g tb-updater)"
   if [ -z "${tb_updater_gid}" ]; then
      log error "Unable to get GID of account 'tb-updater'! Exiting."
      exit 1
   fi

   log notice "Executing under account tb-updater: '$*'"
   setpriv --reuid "${tb_updater_uid}" --regid "${tb_updater_gid}" \
     --init-groups --reset-env -- "$@"
}

if was_executed "${BASH_SOURCE[0]}"; then
   main "$@"
fi
