#!/bin/bash

## Copyright (C) 2012 - 2020 ENCRYPTED SUPPORT LP <adrelanos@riseup.net>
## See the file COPYING for copying conditions.

## Required to run apt-get dist-upgrade --simulate as user (non-root).
## Required for whonixcheck function check_operating_system.
## Exception to run /usr/bin/apt-get-update as user
## is defined in /etc/sudoers.d/.

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