#!/bin/bash

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

#set -x
set -o errexit
set -o nounset
set -o errtrace
set -o pipefail

true "$0: START"

source /usr/libexec/helper-scripts/systemd-notify.bsh

exit_handler() {
  ## Avoid error.
  ## > Dec 04 08:59:57 sys-net systemd[1]: sdwdate-gui-qubes@0-1443-1000.service: Failed with result 'protocol'.
  "${systemd_notify[@]}" --ready
  true "$0: END: OK."
}

trap exit_handler EXIT

target_vm="$(/usr/libexec/sdwdate-gui/sdwdate-gui-config-read 'gateway')" || exit 1

"${systemd_notify[@]}" --ready

connect_check_rslt='n'
while true; do
  sleep 1
  connect_check_rslt="$(
    ## We redirect a blank string into this qrexec-client-vm invocation's
    ## stdin, because otherwise it will eat the script's stdin up to this
    ## point, preventing some data from reaching the server.
    qrexec-client-vm "${target_vm}" sdwdate-gui.ConnectCheck <<< "" 2>&1
  )" || connect_check_rslt='n'
  if [ "${connect_check_rslt}" = 'n' ]; then
    continue
  fi
  break
done

if [ "${connect_check_rslt}" != 'y' ]; then
  exit 0
fi

qrexec-client-vm "${target_vm}" sdwdate-gui.Connect || true
