#!/bin/bash
#
# Wrapper around 'sudo' for Myrlyn that can open a graphical password prompt
# and that can keep the connection to the display (X11 or Wayland) alive
# as well as some well-defined environment variables.
#
# Author:  Stefan Hundhammer <Stefan.Hundhammer@gmx.de>
# License: GPL V2

MYRLYN_ARGS=$*
MYRLYN_SUDO_CONF=$HOME/.config/openSUSE/myrlyn-sudo.conf

if [ -f $MYRLYN_SUDO_CONF ]; then
    # echo "Reading variables from $MYRLYN_SUDO_CONF"
    source $MYRLYN_SUDO_CONF
fi

# Environment variables to keep for the 'sudo' call
ENV_KEEP="DISPLAY WAYLAND_DISPLAY XAUTHORITY XDG_RUNTIME_DIR \
QT_QPA_PLATFORMTHEME QT_ENABLE_HIGHDPI_SCALING QT_SCALE_FACTOR \
LANG LANGUAGE LC_MESSAGES LC_COLLATE LC_NUMERIC LC_TIME LC_ALL"

# You can set any of them in ~/.config/openSUSE/myrlyn-sudo.conf like this:
#
#  QT_QPA_PLATFORMTHEME=qt6ct
#  QT_ENABLE_HIGHDPI_SCALING=0
#  QT_SCALE_FACTOR=1


# Build an environment for use with /usr/bin/env from the above variables:
# DISPLAY=:0.0 LANG=de_DE.utf8 ...

ENV=""

for VAR in $ENV_KEEP; do
    VALUE=${!VAR}
    if [ -n "$VALUE" ]; then
        # Uncomment for debugging
        # echo "$VAR=$VALUE"
        ENV="$ENV $VAR=$VALUE"
    fi
done

# Uncomment for debugging
# echo $ENV

# Use our own askpass binary to for a graphical password prompt
ASKPASS=/usr/bin/myrlyn-askpass

# Using /usr/bin/env to set up an environment inside the 'sudo' call,
# not relying on what 'env_keep' in /etc/sudoers might allow or not.
#
# The main command that is called here is /usr/bin/env which builds
# the environment and then calls myrlyn with $MYRLYN_ARGS.

SUDO_ASKPASS=$ASKPASS sudo -A /usr/bin/env $ENV /usr/bin/myrlyn $MYRLYN_ARGS
