#!/bin/bash

# Identify the display manager
for dm in sddm gdm lightdm lxdm; do
    if systemctl status $dm &> /dev/null; then
        display_manager=$dm
        break
    fi
done
display_manager=${display_manager:-"unknown"}

# Check the session type and move to the appropriate directory
if [ "$XDG_SESSION_TYPE" == "x11" ]; then
    monitors=$(xrandr | grep -w connected | awk '{print $1}')

    # Get the recommended resolution of the first monitor
    first_monitor=$(echo $monitors | awk '{print $1}')
    resolution_first_monitor=$(xrandr | grep "$first_monitor connected" -A1 | tail -n1 | awk '{print $1}')

    # Create a string to store the xrandr configuration
    xrandr_configuration=""

    # Loop through monitors (excluding the first one) to build the xrandr configuration
    for monitor in $monitors; do
        if [ "$monitor" != "$first_monitor" ]; then
            xrandr_configuration="$xrandr_configuration --output $monitor --same-as $first_monitor --mode $resolution_first_monitor"
        fi
    done

    # Apply the xrandr configuration to duplicate the output on all monitors
    xrandr $xrandr_configuration
fi

# Specific settings for SDDM
if [[ $display_manager == "sddm" ]]; then
    if [ "$(grep 'Autolock=false' ~/.config/kscreenlockerrc)" = "" ]; then
        sed -i '/\[Daemon\]/a\'$'\n''Autolock=false\nLockOnResume=false' ~/.config/kscreenlockerrc
    fi
fi

if [ "$(grep 'Enabled=false' ~/.config/kwalletrc)" = "" ]; then
    echo '[Wallet]
Enabled=false' >> ~/.config/kwalletrc
fi

# Verify cmdline biglinux.bootcmd
eval "kernel_args=( $(cat /proc/cmdline) )"
for arg in "${kernel_args[@]}" ; do
    case "$arg" in
        biglinux.bootcmd=*)
            ${arg#biglinux.bootcmd=}
            exit
            ;;
    esac
done

# Manage the biglinux-dual-gpu-manager-after-display-manager service
cd /usr/share/bigbashview/apps/livecd/
if systemctl -q is-enabled biglinux-dual-gpu-manager-after-display-manager; then
    sudo systemctl start biglinux-dual-gpu-manager-after-display-manager 2>&1 &
fi

# Apply theme
if [[ -e /usr/share/sync-kde-and-gtk-places/sync-gnome-theme-to-qt.sh ]]; then
    /usr/share/sync-kde-and-gtk-places/sync-gnome-theme-to-qt.sh
fi

# Environment settings for QT and run bigbashview
if [ "$XDG_SESSION_TYPE" == "x11" ]; then
        bigbashview -c black -t qt -s $(LANG=C xdpyinfo  | grep 'dimensions:' | sed 's|.*dimensions:||g;s| pixels.*||g') localization.html
else
    if [[ $display_manager == "sddm" ]]; then
        QT_SCALE_FACTOR_ROUNDING_POLICY=RoundPreferFloor kwin_wayland --drm --no-lockscreen --locale1 --exit-with-session '/usr/bin/bigbashview -w fullscreen -c black -t qt -d /usr/share/bigbashview/apps/livecd localization.html'
    elif [[ $display_manager == "gdm" ]]; then
        QT_SCALE_FACTOR_ROUNDING_POLICY=RoundPreferFloor mutter --wayland /usr/bin/gnome-bbv-live-session
    fi
fi

wait

# Language and theme settings
export LANGUAGE=$(cat /tmp/big_language).UTF-8
export LANG=$(cat /tmp/big_language).UTF-8
export LC_MESSAGES=$(cat /tmp/big_language).UTF-8
export LC_ALL=$(cat /tmp/big_language).UTF-8
echo "$(cat /tmp/big_language)" > $HOME/.config/user-dirs.locale
echo -e "[Formats]\nLANG=$(cat /tmp/big_language).UTF-8" > $HOME/.config/plasma-localerc

# Make folders in home
xdg-user-dirs-update
. ~/.config/user-dirs.dirs
cp -f "/usr/share/applications/calamares-biglinux.desktop" "$XDG_DESKTOP_DIR/calamares-biglinux.desktop"
chmod +x "$XDG_DESKTOP_DIR/calamares-biglinux.desktop"

cd ~

# Checks if the display manager is GDM and removes unwanted files.
if [[ $display_manager == "gdm" ]]; then
    # Removes unwanted files
    rm -f "$HOME/Empty Bash" "$HOME/Empty Desktop File.desktop" "$HOME/Empty File"

    # Updates user directories
    xdg-user-dirs-update
    
    # Start Gnome
    /usr/bin/gnome-session

    elif [[ $display_manager == "sddm" || $display_manager == "lxdm" ]]; then
    if [[ "$XDG_SESSION_TYPE" == "x11" ]]; then
        startkde-biglinux
    else
        startkde-biglinux wayland
    fi
elif [[ -e /usr/share/xsessions/xfce.desktop ]]; then
    export XDG_MENU_PREFIX=xfce-
    export DESKTOP_SESSION=xfce
    export XDG_SESSION_DESKTOP=xfce
    export QT_QPA_PLATFORMTHEME=xfce
    export GDMSESSION=xfce
    export XDG_CURRENT_DESKTOP=XFCE
    startxfce4
elif [[ -e /usr/bin/cinnamon-session-cinnamon ]]; then
    export DESKTOP_SESSION=cinnamon
    export XDG_SESSION_DESKTOP=cinnamon
    export XDG_CURRENT_DESKTOP=X-Cinnamon
    export GDMSESSION=cinnamon
    cinnamon-session-cinnamon
fi

sudo systemctl restart $display_manager
