#!/usr/bin/env bash
#
# This wrapper allows allows some enviroment variables to be set properly even when it isn't in the environment
#

ID=$(id -u)
if [[ "$ID" == "0" ]]; then
    export XDG_RUNTIME_DIR=$(mktemp -d)
fi


# extract the value of --xdg-desktop if it is passed and put all other args in params
for i in "$@"; do
    case $i in
        -x=*|--xdg-desktop=*)
            export XDG_CURRENT_DESKTOP="${i#*=}"
            shift
        ;;
        -r=*|--xdg-runtime=*)
            XDG_RUNTIME_DIR="${i#*=}"
            shift
        ;;
        -t=*|--platformtheme=*)
            export QT_QPA_PLATFORMTHEME="${i#*=}"
            shift
        ;;
        -p=*|--platform=*)
            export QT_QPA_PLATFORM="${i#*=}"
            shift
        ;;
        -d=*|--display=*)
            export WAYLAND_DISPLAY="${i#*=}"
            shift
        ;;
        -s=*|--style-override=*)
            export QT_STYLE_OVERRIDE="${i#*=}"
            shift
        ;;        
        *)
            params="${params} ${i}"
            shift
        ;;
    esac
done

btrfs-assistant-bin ${params}
