#!/bin/bash

# MX Linux mx-installer pkexec wrapper to retain QT environment
# Usage:
#        mx-installer-pkexec
# based on mx-pkexec wrapper

##launch installer

if test "$EUID" != 0; then
    # normal user
    ##disable Xfce automount features

	INITIALAUTOMOUNT=$(command -v xfconf-query && xfconf-query --channel thunar-volman --property /automount-drives/enabled)
	echo "INITIALAUTOMOUNT =" "$INITIALAUTOMOUNT"
	echo "$INITIALAUTOMOUNT" >> /tmp/minstall-auto
	command -v xfconf-query && xfconf-query --channel thunar-volman --property /automount-drives/enabled --set false
	###end Xfce automount feature
    
    QTENV="/tmp/mx-installer-qtenv.$EUID.$PPID.$$.$RANDOM$RANDOM"
    env | grep -E '^QT_|^LANG='  | sed -e 's/^/export /' 2>/dev/null  > "$QTENV"
    chmod +r "$QTENV"
    pkexec /usr/bin/minstall-pkexec "--qtenv" "$QTENV" "${@}"
    if [ -f "$QTENV" ] ; then
        rm  $QTENV
    fi
    ##renable Xfce automount feature, if it was enabled in the first place
	if [ -e "/tmp/minstall-auto" ]; then
		rm /tmp/minstall-auto
	fi
	
	if [ "$INITIALAUTOMOUNT" = "true" ]; then
		command -v xfconf-query && xfconf-query --channel thunar-volman --property /automount-drives/enabled --set true
	fi

##end Xfce automount feaure
fi

if test "$EUID" == 0; then
    # root user
    # set XDG_RUNTIME_DIR - do nothing if already set
    
    if [ x"$XDG_RUNTIME_DIR" = "x" ]; then
        XDG_RUNTIME_DIR=/run/user/0
        export XDG_RUNTIME_DIR
        [ -d $XDG_RUNTIME_DIR ] || mkdir -p $XDG_RUNTIME_DIR
        chmod 700 $XDG_RUNTIME_DIR
        chown 0:0 $XDG_RUNTIME_DIR
        fi

    if  [ "x$1" = "x--qtenv" ]; then
        QTENV="$2"
        shift
        shift
        if [ -r "$QTENV" -a  "$QTENV" != "${QTENV#/tmp/mx-installer-qtenv.}" ] ; then
            . $QTENV
            rm  $QTENV
        fi

        PATH="/usr/local/bin:$PATH"
    fi

    RUN="/sbin/minstall"
    echo Starting  "$RUN"
    command -v "$RUN" >/dev/null || { echo "mx-installer: Command '$RUN' not found"; exit 1; }
    exec "$RUN" "${@}"
fi

exit
