#!/bin/bash

REALPKEXEC='/usr/bin/pkexec'
if [ -f "$REALPKEXEC" ]; then
    for ARG in "$@"; do
        # Check if not an argument or that the ARGS variable is already set
        if [ "${ARG:0:1}" != '-' ] || [ ! -z "$ARGS" ]; then
            # Add argument to application
            ARGS="$ARGS $ARG"
        else
            # Add argument to pkexec
            REALPKEXEC="$REALPKEXEC $ARG"
        fi
    done
    if [ ! -z "$ARGS" ]; then
        eval $REALPKEXEC env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY XDG_CURRENT_DESKTOP=$XDG_CURRENT_DESKTOP "$ARGS"
    else
        echo "ERROR: $1 is not executable."
        exit 2
    fi
else
    echo "ERROR: /usr/bin/pkexec not found."
    exit 1
fi
exit 0
