#!/bin/bash

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

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

## Required to run apt-get dist-upgrade --simulate as user (non-root).
## Required for systemcheck function check_operating_system.
## Exception to run /usr/libexec/helper-scripts/apt-get-update as user
## is defined in /etc/sudoers.d/ and /usr/lib/privleap/conf.d/.

export LC_ALL=C

# shellcheck disable=SC2317
sigterm_trap() {
   if [ "${lastpid}" = "" ]; then
      exit 0
   fi
   if ! ps -p "${lastpid}" >/dev/null 2>&1; then
      ## Already terminated.
      exit 0
   fi
   kill -s sigterm "${lastpid}"
   exit "$?"
}

lastpid=""
trap "sigterm_trap" SIGTERM SIGINT

timeout_after="10"
kill_after="5"

timeout \
   --kill-after="${kill_after}" \
   "${timeout_after}" \
   apt-get dist-upgrade --simulate &

lastpid="$!"
wait "${lastpid}"

exit "$?"
