#!/bin/bash

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

## 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 /etc/privleap/conf.d/.

export LC_ALL=C

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

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 "$?"
