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

DEBUG=''
while getopts 'dDh' OPT; do
    case $OPT in
        d|D)
            # Shut down when done
            DEBUG='-d'
            ;;
        h)
            echo 'Usage: solydxk-installer [-d]'
            echo '-d: start Calamares in debug mode'
            echo 'Do not close Calamares when finished'
            echo 'but save /root/.cache/calamares/session.log'
            exit 0
            ;;
    esac
done

# Disable xfce4-power-manager: causes unresponsiveness on some laptops
if [ -n "$(which xfce4-power-manager)" ]; then
    xfce4-power-manager --quit
fi

# Make sure drives are not automounted
qdbus org.kde.kded6 /kded org.kde.kded6.unloadModule device_automounter 2>/dev/null
xfconf-query -c thunar-volman -p /automount-drives/enabled -s false 2>/dev/null

# Stale file left behind by live-build that messes with partitioning
sudo mv /etc/fstab /etc/fstab.orig.calamares

# Access control to run calamares as root for xwayland
xhost +si:localuser:root
pkexec calamares $DEBUG
xhost -si:localuser:root

# Restore stale fstab, for what it's worth
sudo mv /etc/fstab.orig.calamares /etc/fstab

# Restore automount
qdbus org.kde.kded6 /kded org.kde.kded6.loadModule device_automounter 2>/dev/null
xfconf-query -c thunar-volman -p /automount-drives/enabled -s true 2>/dev/null

# Enable xfce4-power-manager
if [ -n "$(which xfce4-power-manager)" ]; then
    xfce4-power-manager &
fi

# Show log file if in debug mode
[ -e /root/.cache/calamares/session.log ] && xdg-open /root/.cache/calamares/session.log
