#!/usr/bin/bash

set -e

if [ "$1" = "started-by-init" ]; then
    true "INFO: Started by systemd unit (timer.) Continuing..."
else
    true "INFO: Not started by systemd unit (timer.) Probably started by package manager hook script."
    if test -e /run/qubes/persistent-full; then
        true "INFO: Running inside Template and Standalone. Continuing..."
    else
        true "INFO: Probably running inside App Qube. Stop."
        exit 0
    fi
fi

script_arg=
if [ "$1" = "skip-refresh" ]; then
    script_arg=skip-refresh
fi

upgrades_installed="$(/usr/lib/qubes/upgrades-installed-check $script_arg)"

if [ "$upgrades_installed" = "true" ]; then
    /usr/lib/qubes/qrexec-client-vm dom0 qubes.NotifyUpdates /bin/sh -c 'echo 0'
elif [ "$upgrades_installed" = "false" ]; then
    /usr/lib/qubes/qrexec-client-vm dom0 qubes.NotifyUpdates /bin/sh -c 'echo 1'
fi
