#!/bin/bash

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

#set -x

printf '%s\n' "$0: START"

set -e

kernel_cmdline=''
if [ -f /proc/cmdline ]; then
  kernel_cmdline="$(cat -- /proc/cmdline)"
elif [ -f /proc/1/cmdline ]; then
  kernel_cmdline="$(cat -- /proc/1/cmdline)"
fi

if getent passwd sysmaint >/dev/null; then
   if ! [[ "${kernel_cmdline}" =~ 'boot-role=sysmaint' ]] \
      && ! [[ "${kernel_cmdline}" =~ 'boot-role=unrestricted-admin' ]]; then
      printf '%s\n' "$0: ERROR: **Boot Using \"LIVE Mode | SYSMAINT Session\" Boot Option To Proceed**

You are currently booted into 'LIVE Mode | USER Session', which does not provide administrative ('root') access. To install the system, reboot and select \"LIVE Mode | SYSMAINT Session\" at the boot menu.
"

      printf '%s\n' "$0: END"
      printf '%s\n' "INFO: Feel free to close this window."
      sleep 86400
      exit 0
   fi
fi

if ! [ -x '/usr/bin/pkexec' ] || ! [ -x '/usr/bin/sudo' ]; then
   printf '%s\n' "$0: ERROR: **Unable To Elevate To Root**

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

   printf '%s\n' "$0: END"
   printf '%s\n' "INFO: Feel free to close this window."
   sleep 86400
   exit 0
fi

printf '%s\n' "$0: INFO: **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 from Kicksecure.
- The graphical installer interface is powered by Calamares.
"

sleep 3

error_handler() {
  printf '%s\n' "##############################"
  printf '%s\n' "## ERROR detected!"
  printf '%s\n' "## BASH_COMMAND: $BASH_COMMAND"
  printf '%s\n' "## Please report this bug!"
  printf '%s\n' "##############################"
  sleep 86400
  exit 1
}

trap "error_handler" ERR

sudo --non-interactive install-host-root

export DISPLAY=:0
xhost +si:localuser:root >/dev/null

## Not easy to pass extra options here. Doing so inside the wrapper.
sudo --non-interactive --preserve-env install-host-calamares-wrapper

xhost -si:localuser:root >/dev/null

printf '%s\n' "$0: END"
printf '%s\n' "Feel free to close this window."

sleep 86400
