#!/usr/bin/env bash
#
#   Script name: gkpkexec
#   Description: A graphical frontend for pkexec that uses PolicyKit
#   GitLab: https://gitlab.ublinux.ru/
#   Author: Dmitry Razumov asmeron@ublinux.com
#   Contributors: asmeron@ublinux.com
#
#   Copyright (c) 2021-2023 UBLinux Development Team <support@ublinux.ru>
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.

VERSION_SCRIPT=1.1

if [[ ${EUID} -ne 0 ]]; then
    exec pkexec /usr/bin/gkpkexec "$@"
    exit 1
fi

# Restore some variables from parent process
while IFS= read -rd '' var; do export "$var"; done < <(grep --null-data -ae "^\(XDG_CURRENT_DESKTOP\|WAYLAND_DISPLAY\|XDG_RUNTIME_DIR\|XDG_SESSION_TYPE\|XCURSOR_SIZE\|LC_.*\|LANG\|LANGUAGE\|QT_WAYLAND_FORCE_DPI\|\)=.*\$" /proc/${PPID}/environ)
if [[ -v WAYLAND_DISPLAY ]]; then
    export WAYLAND_DISPLAY="${XDG_RUNTIME_DIR}/${WAYLAND_DISPLAY}"
fi
export XDG_RUNTIME_DIR="/run/user/0"
exec "$@"
