#!/bin/bash

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

#### meta start
#### project Whonix and Kicksecure
#### category usability
#### description
## Helper script. Supposed to be run by `easo` (easyorca). Not supposed to be
## run directly.
##
## * Disable autostart of setup-wizard-dist GUI.
## * Enable orca autostart.
## * Enable orca-kill-at-shutdown.service systemd unit.
## * Accept setup-wizard-dist legal notices using `setup-dist`(`-noninteractive`).
## * Enable Tor public network using `setup-dist`(`-noninteractive`).
## * Does not start `orca` because this script is supposed to run as root while
## `orca` should run as user, not root. Starting `orca` is expected to be done
## `easo` or the user.
#### meta end

set -x

if [ "$(id -u)" = "0" ]; then
    echo "ERROR: This must NOT be run as root (sudo)!"
    echo "INFO: You can start $0 by entering..."
    echo "      $0"
    exit 1
fi

## Disable autostart of setup-wizard-dist GUI since it does not have
## accessibility support yet.
rm -f /etc/xdg/autostart/setup-wizard-dist.desktop

if type -P orca-enable-autostart >/dev/null ; then
   ## From package usability-misc.
   orca-enable-autostart
fi

systemctl enable orca-kill-at-shutdown.service
systemctl start  orca-kill-at-shutdown.service
## Debugging.
systemctl status --no-pager orca-kill-at-shutdown.service

setup-dist-noninteractive 1

## Terminate Setup Wizard Dist GUI.
## Using sigkill instead of sigterm.
## Sigterm would be nicer. But sigkill is safer.
## Otherwise if above command went wrong, this would
## lead to poweroff because that is what setup-wizard(-dist) does when it gets
## closed without legal agreements having accepted.
pkill -f -kill setup-wizard-dist
