#!/bin/bash -e
# vim: set ts=4 sw=4 sts=4 et :
#
# qubes-whonixsetup - Called by /etc/xdg/autostart/qubes-whonixsetup.desktop
#
# Performs various Whonix 'setup' related tasks which will notify or prompt the
# user if any required configurations need attention and also warn the user the
# VM is going to be powered off if a failure of one of the configurations was
# detected.
#
# Copyright (C) 2014 - 2015 Jason Mehring <nrgaway@gmail.com>
# License: GPL-2+
# Authors: Jason Mehring
#
#   This program is free software; you can redistribute it and/or
#   modify it under the terms of the GNU General Public License
#   as published by the Free Software Foundation; either version 2
#   of the License, or (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.

if test -f /run/qubes/this-is-templatevm ; then

   timeout_seconds=120

   for (( i=1 ; i<=$timeout_seconds ; i++)); do
      if [ -e '/run/updatesproxycheck/whonix-secure-proxy' ]; then
         break
      fi
      sleep 1 &
      wait "$!"
   done

   if [ ! -e '/run/updatesproxycheck/whonix-secure-proxy' ]; then
      ## Display warning that the Template is not connected to a Tor update proxy.
      /usr/libexec/msgcollector/alert update /usr/lib/qubes-whonix/messages.yaml
   fi
fi
