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

DEBUG=''
while getopts 'dh' OPT; do
    case $OPT in
        d)
            # Shut down when done
            DEBUG='-d'
            ;;
        h)
            echo 'Usage: install-solydxk [-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 [ ! -z "$(which xfce4-power-manager)" ]; then
    xfce4-power-manager --quit
fi

# Make sure drives are not automounted
qdbus org.kde.kded5 /kded org.kde.kded5.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.kded5 /kded org.kde.kded5.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 [ ! -z "$(which xfce4-power-manager)" ]; then
    xfce4-power-manager &
fi
