#!/bin/bash

# MX Linux mx-installer pkexec wrapper to retain QT environment
# Usage:
#        minstall-launcher (formerly minstall-pkexec)
# based on mx-pkexec wrapper

#exit early if not live or parameter given

if [ -n "$1" ] || ! grep -sq '^overlay /live/aufs' /proc/mounts; then
    echo ""
    echo "INFORMATION: Non-live enviroment or options not supported"
    echo "             use alternate authentication to launch installer"
    echo "             sudo minstall [OPTIONS]"
    echo ""
    exit 1
fi

##launch installer

if [ "$EUID" != 0 ]; then
    # normal user

    ##disable Xfce automount features

    INITIALAUTOMOUNT=false
    if command -v xfconf-query >/dev/null && \
       xfconf-query --channel thunar-volman --property /automount-drives/enabled 1>/dev/null 2>&1; then
       INITIALAUTOMOUNT=$(xfconf-query --channel thunar-volman --property /automount-drives/enabled)
       echo "INITIALAUTOMOUNT =" "$INITIALAUTOMOUNT"
       if [ "$INITIALAUTOMOUNT" = "true" ]; then
          echo "$INITIALAUTOMOUNT" > /tmp/minstall-auto
          xfconf-query --channel thunar-volman --property /automount-drives/enabled --set false
       fi
    fi

    ###end Xfce automount feature
    # wayland fix (chkboom)
    if [ x"$WAYLAND_DISPLAY" != "x" ] && [ -n "${WAYLAND_DISPLAY##/*}" ]; then
      export WAYLAND_DISPLAY=$XDG_RUNTIME_DIR/$WAYLAND_DISPLAY
    fi

    AUTHENTICATION="$(grep AUTHENTICATION /usr/share/gazelle-installer-data/installer.conf |cut -d= -f2)"
    if [ -z "$AUTHENTICATION" ]; then
        AUTHENTICATION="su-to-root -X -c"
    fi
    
    $AUTHENTICATION /usr/bin/minstall-launcher

    ##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
        xfconf-query --channel thunar-volman --property /automount-drives/enabled --set true
    fi

    ##end Xfce automount feaure

else
    # root user

    # set XDG_RUNTIME_DIR - create a valid runtime dir
    if [ "$XDG_RUNTIME_DIR" != "/run/user/0" ]; 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
    # add /usr/local/bin as first entry if not already
    [ -z "${PATH##/usr/local/bin:*}" ] || PATH="/usr/local/bin:$PATH"

    # put pattern list of environment variables we want get from users environment into array
    __ENVIRONEMENT_PATTERN__=(
        DESKTOP_SESSION
        KDE_FULL_SESSION=
        LANG=
        LANGUAGE=
        LC_
        PWD=
        QT_
        XDG_CURRENT_DESKTOP=
        XDG_SESSION_TYPE
        WAYLAND_
        )

    # combine array into a string of space separated entries 
    __ENVIRONEMENT_PATTERN__="${__ENVIRONEMENT_PATTERN__[*]}"
    # replace spaces with pipe-symbole as pattern alternative
    __ENVIRONEMENT_PATTERN__="^(${__ENVIRONEMENT_PATTERN__// /|})"
    # read environment variables from users process environement table
    while read -r; do  
        IFS='=' read -r  k v  <<<"$REPLY" 
        # remove any 'bad' special char's like back-quotes and dollar sign
        v="${v//[\`\$]/}"
        # change to user working dir
        [ -z "${k##PWD*}" ] && cd "$v" && continue
        export $k="$v"  
    done < <( xargs -0 -L1 -a /proc/$PPID/environ \
            | grep -E "${__ENVIRONEMENT_PATTERN__}")

    unset k v
    unset __ENVIRONEMENT_PATTERN__

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

exit
