#!/bin/bash

###
## Wrapper for running calamares on live media
###

set -x

true "$0: START"

set -e

kernel_cmdline="$(cat /proc/cmdline)"
if getent passwd sysmaint >/dev/null; then
   if ! [[ "${kernel_cmdline}" =~ 'boot-role=sysmaint' ]] \
      && ! [[ "${kernel_cmdline}" =~ 'boot-role=unrestricted-admin' ]]; then
      true "$0: **Boot Using \"Install system\" Boot Option To Proceed**

You are currently booted into live mode, which does not provide root access. To install the system, reboot and select \"Install system\" at the boot menu.
"

      true "$0: END"
      true "Feel free to close this window."
      sleep 86400
      exit 0
   fi
fi

if ! [ -x '/usr/bin/pkexec' ] || ! [ -x '/usr/bin/sudo' ]; then
   true "$0: **Unable To Elevate To Root**

The pkexec or sudo command is not executable by the current user. Installation cannot proceed. You may need to log in as user 'sysmaint' to resolve this.
"

   true "$0: END"
   true "Feel free to close this window."
   sleep 86400
   exit 0
fi

true "$0: **Important Information for Users**

**User Credentials:**

- **Default Username:** user
- **Default Password:** No password required. (Passwordless login.)

**Please Keep This Window Open:**

- **Installation Failure:** If the installation process encounters any issues, the information displayed in this window will be crucial for troubleshooting and support requests.
- **Installation Success:** Once the installation is complete, this window might not be necessary anymore. However, it's advisable to wait until you're certain that the installation has successfully finished before closing it.

**Determining the Installation Outcome:**

- **Success Indicators:**
  - Absence of error messages.
  - The system boots into the newly installed environment without issues.
- **Failure Indicators:**
  - Presence of error popups.
  - The system is unbootable.

**Credits:**

- The information displayed in the terminal is a from Kicksecure.
- The graphical installer interface is powered by Calamares.
"

sleep 3

error_handler() {
  true "##############################"
  true "## ERROR detected!"
  true "## BASH_COMMAND: $BASH_COMMAND"
  true "## Please report this bug!"
  true "##############################"
  sleep 86400
  exit 1
}

trap "error_handler" ERR

# Allow Calamares to scale the window for hidpi displays
# This is fixed in the Calamares 3.3.0 series, so we can remove this
# once we switch to that
# Upstream commit that will make this obsolete:
#     https://github.com/calamares/calamares/commit/e9f011b686a0982fb7828e8ac02a8e0784d3b11f
# Upstream bug:
#     https://github.com/calamares/calamares/issues/1945
# Debian bug:
#     https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=992162
export QT_AUTO_SCREEN_SCALE_FACTOR=1

sudo --non-interactive install-host-root

## Access control to run calamares as root for xwayland
xhost +si:localuser:root

## Calamares has no native Wayland support yet.
## Requires X11 or XWayland.
## Rootless Calamares not possible yet.
## https://github.com/calamares/calamares/issues/2268
##
## Not easy to pass extra options here. Doing so inside the wrapper.
pkexec install-host-calamares-wrapper

xhost -si:localuser:root

true "$0: END"
true "Feel free to close this window."

sleep 86400
