#!/usr/bin/env bash

ui=""
extra_args=()
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"

if [[ $script_dir == "/usr/bin" ]]; then
    script_dir="/usr/share/bakery"
fi

while [[ $# -gt 0 ]]; do
    case $1 in
        --gui|--tui)
            ui="${1#--}"
            shift
            ;;
        --simple)
            ui="tui"
            extra_args+=("--simple")
            shift
            ;;
        *)
            echo "Error: Unknown argument $1"
            exit 1
            ;;
    esac
done

ignore_vars=(
    "SHELL" "COLORTERM" "LANGUAGE" "PWD" "LOGNAME" "HOME" "LANG"
    "TERM" "USER" "SHLVL" "PKEXEC_UID" "LC_CTYPE" "PATH" "_"
)

save_env() {
    tmp_env=$(mktemp)
    printenv | while IFS='=' read -r key value; do
        skip=0
        for ignore in "${ignore_vars[@]}"; do
            if [[ "$key" == "$ignore" ]]; then
                skip=1
                break
            fi
        done
        if [[ $skip -eq 0 ]]; then
            # Escape double quotes and dollar signs
            esc_value="${value//\"/\\\"}"
            esc_value="${esc_value//\$/\\\$}"
            echo "export $key=\"$esc_value\"" >> "$tmp_env"
        fi
    done
    echo "$tmp_env"
}

if [[ $ui == "gui" || $ui == "tui" ]]; then
    script_name="bakery-$ui.py"
    if [[ -f "$script_dir/$script_name" ]]; then
        if [[ $ui == "gui" ]]; then
            #check if mesa-panfork/mesa-panfork-git is installed then export LIBGL_ALWAYS_SOFTWARE=1
            if pacman -Q mesa-panfork &>/dev/null || pacman -Q mesa-panfork-git &>/dev/null; then
                command="export LIBGL_ALWAYS_SOFTWARE=1;"
            else
                command=""
            fi
        fi

        command+=" python $script_dir/$script_name ${extra_args[*]}"
    else
        echo "Error: bakery-$ui is not installed"
        echo "Please install bakery-$ui with 'sudo pacman -S bakery-$ui'"
        exit 1
    fi
else
    echo "Error: No UI specified."
    exit 1
fi

if [[ $EUID -ne 0 ]]; then
    tmp_env_file=$(save_env)
    exec pkexec bash -c "source \"$tmp_env_file\"; rm -f \"$tmp_env_file\"; cd $script_dir; $command"
fi