#!/bin/sh
###
# Wrapper for running calamares on Debian live media
###

# Stale file left behind by live-build that messes with partitioning
if [ -f /etc/fstab ]; then
  sudo mv /etc/fstab /etc/fstab.orig.calamares || true
fi

# 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

QT_QPA_PLATFORMTHEME=qt5ct \
QS_ICON_THEME=Tela-circle-manjaro-dark \
QT_AUTO_SCREEN_SCALE_FACTOR=1 \
xhost +si:localuser:root >/dev/null 2>&1

# Run Calamares as root with the environment variables present for the process.
# In a live session the regular user is typically in sudoers NOPASSWD, so this works.
sudo QT_QPA_PLATFORMTHEME=qt5ct QS_ICON_THEME=Tela-circle-manjaro-dark QT_AUTO_SCREEN_SCALE_FACTOR=1 calamares

# Undo xhost change
xhost -si:localuser:root >/dev/null 2>&1

# Restore stale fstab, for what it's worth
if [ -f /etc/fstab.orig.calamares ]; then
  sudo mv /etc/fstab.orig.calamares /etc/fstab || true
fi
