#!/bin/bash


STARTPLASMA=startplasma-x11

# Bolder font rendering
#export FREETYPE_PROPERTIES="cff:no-stem-darkening=0 autofitter:no-stem-darkening=0"

# Fix to open/save dialog em gtk apps
#export QT_NO_XDG_DESKTOP_PORTAL=1

RESOLUTION="$(xdpyinfo | awk '/dimensions/ {print $2}')"
SCREEN="$(xrandr | grep 'connected primary' | cut -f1 -d" ")"
WIDTH=${RESOLUTION//x*}
HEIGHT=${RESOLUTION//*x}


# To libreoffice use gtk theme
export SAL_USE_VCLPLUGIN=gtk3_kde5

if [ "$1" = "wayland" ]; then
    STARTPLASMA=startplasma-wayland
    # export EGL_PLATFORM=wayland
    # export GDK_BACKEND=wayland
    # export QT_QPA_PLATFORM=wayland-egl
    # export SDL_VIDEODRIVER=wayland
    # export EGL_PLATFORM=wayland
    # export GDK_BACKEND=wayland
    # export KWIN_OPENGL_INTERFACE=egl
    # export QT_QPA_PLATFORM=wayland-egl
    export SDL_VIDEODRIVER=wayland
    # export QT_XCB_GL_INTEGRATION=xcb_egl
    export QSG_RENDER_LOOP=basic
    export MOZ_ENABLE_WAYLAND=1
    export KWIN_EFFECTS_FORCE_ANIMATIONS=1
    exec --no-startup-id /usr/lib/pam_kwallet_init

else
    export GDK_BACKEND=x11
    export GSK_RENDERER=cairo
    export KWIN_EFFECTS_FORCE_ANIMATIONS=1
    export MOZ_USE_XINPUT2=1
    # Nvidia tweak
    if [ "$(lsmod | grep nvidia_drm)" != "" ]; then
        export __GL_MaxFramesAllowed=1
    fi
    #
    #export KWIN_TRIPLE_BUFFER=0
    #export __GL_YIELD=NOTHING
    # export __GL_YIELD=USLEEP

fi

# Force language
if [ "$LANGUAGE" = "" ]; then
export LANGUAGE=$LANG
fi

# Checks if the shell has already been changed
if [ ! -f "$HOME/.biglinux-shell-changed" ]; then
    biglinux-change-default-shell bash-power
    touch "$HOME/.biglinux-shell-changed"
fi

# Use KDE dialog in some GTK apps like Firefox
export GTK_USE_PORTAL=1

#Faster AMD GPU
if [ $HEIGHT -gt 1400 ]  2>/dev/null; then
export RADV_FORCE_VRS=2x2
else
export RADV_FORCE_VRS=1x1
fi
export RADV_PERFTEST=sam,nggc,ext_ms

#Best wine scale
# export WINE_FULLSCREEN_FSR=1
# export WINE_FULLSCREEN_FSR_STRENGTH=3

# Vulkan ICD folder
export ICD_DIR=/usr/share/vulkan/icd.d

#OBS Studio game capture with vulkan
if [ -e "/usr/bin/obs-vkcapture" ]; then
    export OBS_USE_EGL=1
    export OBS_VKCAPTURE=1
    export VK_INSTANCE_LAYERS=VK_LAYER_OBS_vkcapture_64:VK_LAYER_VALVE_steam_overlay_64 #Fix MangoHUD overlay
fi

#Keyboard LED
if [ -e "$HOME/.config/ledkeyboard" ]; then
   change-keyboard-led on
fi

#More cache to 3D
__GL_SHADER_DISK_CACHE=1
__GL_SHADER_DISK_CACHE_SKIP_CLEANUP=1

#Fix to samba in dolphin
if [ ! -e "$HOME/.config/kioslaverc" ]; then
    echo '[Browser Settings/SMBro]
Encoding=iso 8859-1
Password=
ShowHiddenShares=true
User=.
Workgroup=' > "$HOME/.config/kioslaverc"
fi

#Fix to disable-fsync
if [ "$(grep '^/usr/lib/disable-fsync.so' /etc/ld.so.preload)" != "" ]; then
    export LD_PRELOAD='/$LIB/disable-fsync.so'
fi


# Virtualization fix open
if [ "$(lspci | grep -i vga | grep -i vmware)" != "" ]; then
    kwriteconfig5 --file ~/.config/kded5rc --group Module-kscreen --key autoload false
fi
# Virtualization fix close

# Theme in first login
first-login-theme


# Verify to add konsole list of commands
if [ ! -e "$HOME/.config/konsolequickcommandsconfig" ] && [ ! -e "/usr/share/biglinux/konsolequickcommandsconfig-${LANG%%.*}" ]; then
    cp -f "/usr/share/biglinux/konsolequickcommandsconfig-${LANG%%.*}" "$HOME/.config/konsolequickcommandsconfig" 
fi



if [ ! -e "$HOME/.kdebiglinux/lastlogin" ]; then
    change-plasma-theme --apply $(cut -f2 -d" " /etc/big_desktop_changed) quiet
fi


nowlogin="$(cat "$HOME/.kdebiglinux/lastused")"
#ultimo login
lastlogin=$(cat "$HOME/.kdebiglinux/lastlogin")


# Copy config from skel if needed
# if [ ! -e "$HOME/.kdebiglinux/$nowlogin" ]; then
#     cp -Rf "/etc/skel/.kdebiglinux/$nowlogin" "$HOME/.kdebiglinux/$nowlogin"
#     mkdir -p "$HOME/.kdebiglinux/$nowlogin"
# fi

#Get id from last wallpaper
ID=$(grep -Eo '\[Containments\]\[[0-9]{1,5}\]\[Wallpaper\]\[org.kde.image\]' ~/.config/plasma-org.kde.plasma.desktop-appletsrc|head -n1|sed 's|\[||g;s|\]| |g'|cut -f2 -d' ')

#Get path from last wallpaper
WALLPAPER=$(kreadconfig5 --file ~/.config/plasma-org.kde.plasma.desktop-appletsrc --group Containments --group $ID --group Wallpaper --group org.kde.image --group General --key Image|sed 's|file://||')

#Link from wallpaper to ksplash
ln -sf "$WALLPAPER" /tmp/bigksplash.jpg
chmod 777 /tmp/bigksplash.jpg

feh --bg-fill "/tmp/bigksplash.jpg" &

# Fix full screen when switch desktop
if [ "$nowlogin" != "$lastlogin" ]; then
    if [ "$(cat "$HOME/.config/kwin_maximized_disable")" = "1" ]; then
        sed -i 's|"custom_chrome_frame":true|"custom_chrome_frame":false|g' ~/.config/chromium/Default/Preferences ~/.config/google-chrome/Default/Preferences ~/.config/BraveSoftware/Brave-Browser/Default/Preferences
        sed -i 's|user_pref("browser.tabs.InTitlebar", 1);|user_pref("browser.tabs.InTitlebar", 0);|g' ~/.mozilla/firefox/*.default/prefs.js
        #Kwin
        sed -i '/BorderlessMaximizedWindows=false/d' ~/.config/kwinrc
    else
        #Chromium Brave Chrome
        sed -i 's|"custom_chrome_frame":false|"custom_chrome_frame":true|g' ~/.config/chromium/Default/Preferences ~/.config/google-chrome/Default/Preferences ~/.config/BraveSoftware/Brave-Browser/Default/Preferences
        #Firefox
        sed -i 's|user_pref("browser.tabs.InTitlebar", 0);|user_pref("browser.tabs.InTitlebar", 1);|g' ~/.mozilla/firefox/*.default/prefs.js
        #Menubar to kde apps
        sed -i '/MenuBar=Disabled/d' ~/.config/okularrc
        #Kwin
        sed -i '/BorderlessMaximizedWindows=true/d' ~/.config/kwinrc
    fi
fi

echo "$nowlogin" > "$HOME/.kdebiglinux/lastlogin"

if [ ! -e "$HOME/.big_performance" ]; then
    echo "0" > "$HOME/.big_performance"

    # Animation 1
    kwriteconfig5 --file ~/.config/kwinrc --group Compositing --key AnimationSpeed 3
    kwriteconfig5 --file ~/.config/kdeglobals --group KDE --key AnimationDurationFactor ""
    kwriteconfig5 --file ~/.config/klaunchrc --group BusyCursorSettings --key Blinking false
    kwriteconfig5 --file ~/.config/klaunchrc --group BusyCursorSettings --key Bouncing true

    # Composition on
    kwriteconfig5 --file ~/.config/kwinrc --group Compositing --key Enabled true
fi

# Alert if free space is low than 5000
if [ ! -e "$HOME/.config/freespacenotifierrc" ] && [ ! -e "/livefs-pkgs.txt" ]; then
    echo '[General]
minimumSpace=5000' > "$HOME/.config/freespacenotifierrc"
fi


if [ "$(grep -i 'SingleClick=' ~/.config/kdeglobals)" = "" ]; then
    kwriteconfig5 --group KDE --key SingleClick true
fi

# Fix ssh agent
if [ -z "$SSH_AGENT_PID" ]; then
  eval "$(ssh-agent -s)"
fi


# Fix dolphin and konsole size
WINDOW_HEIGHT="$(($HEIGHT - $(($HEIGHT / 3))))"

WINDOW_WIDTH="$(($WIDTH - $(($WIDTH / 3))))"

for i in "$HOME/.config/konsolerc" "$HOME/.config/dolphinrc"; do

    if [ -e "$i" ] && [ "$(grep "$RESOLUTION" "$i")" = "" ]; then

        kwriteconfig5 --group "MainWindow" --key "$SCREEN Height $RESOLUTION" --file "$i" "$WINDOW_HEIGHT"

        kwriteconfig5 --group "MainWindow" --key "$SCREEN Width $RESOLUTION" --file "$i" "$WINDOW_WIDTH"

        kwriteconfig5 --group "MainWindow" --key "$SCREEN YPosition $RESOLUTION" --file "$i" "$WINDOW_HEIGHT"

        kwriteconfig5 --group "MainWindow" --key "$SCREEN XPosition $RESOLUTION" --file "$i" "$WINDOW_WIDTH"

    fi

done


# Fix to GTK4 theme
#export GTK_THEME=
# if [ "$(grep gtk-application-prefer-dark-theme=true ~/.config/gtk-3.0/settings.ini)" != "" ]; then
# 
#     if [ -e "/usr/share/themes/${GTK_THEME}-Dark" ]; then
#         export GTK_THEME=${GTK_THEME}-Dark
#     elif  [ -e "/usr/share/themes/${GTK_THEME}-dark" ]; then
#         export GTK_THEME=${GTK_THEME}-Dark
#     elif  [ "$(echo $GTK_THEME | grep -i dark)" != "" ]; then
#         :
#     else
#         export GTK_THEME=Adwaita-dark
#     fi
# fi


# Change way to autostart latte
> $HOME/.config/autostart/org.kde.latte-dock.desktop
# Remove link to sistray tweaks to latte
if [ -L "$HOME/.local/share/plasma/plasmoids/org.kde.plasma.systemtray" ]; then
  rm -f "$HOME/.local/share/plasma/plasmoids/org.kde.plasma.systemtray"
  rm -f "$HOME/.local/share/plasma/plasmoids/org.kde.plasma.private.systemtray"
fi



# Less transparent Fluent plasma theme if dont have 3d accel
if [ "$(glxinfo | grep llvmpipe)" = "" ]; then
    rm -Rf ~/.local/share/plasma/desktoptheme/Fluent/
    rm -Rf ~/.local/share/plasma/desktoptheme/Fluent-round/
    #export KWIN_EFFECTS_FORCE_ANIMATIONS=1
else
#     export QT_QUICK_BACKEND=software
#     export QSG_RENDER_LOOP=basic
#     export MESA_NO_ERROR=1
#     export MESA_SHADER_CACHE_DISABLE=true
#     export MESA_XSYNC=0
#     export LIBGL_ALWAYS_SOFTWARE=1
#     export DRI_NO_MSAA=1
#     export MESA_GL_VERSION_OVERRIDE=4.4
#     export MESA_GLSL_VERSION_OVERRIDE=120
    #export GALLIUM_DRIVER=swrast
    mkdir -p ~/.local/share/plasma/desktoptheme/Fluent
    echo '[Desktop Entry]
Name=Fluent
Comment=Fluent theme for plasma

X-KDE-PluginInfo-Author=Vince Liuice
X-KDE-PluginInfo-Email=vinceliuice@hotmail.com
X-KDE-PluginInfo-Name=Fluent
X-KDE-PluginInfo-Version=5.78.0
X-KDE-PluginInfo-Website=https://plasma.kde.org
X-KDE-PluginInfo-Category=
X-KDE-PluginInfo-License=LGPL
X-KDE-PluginInfo-EnabledByDefault=true
X-Plasma-API=5.0

[ContrastEffect]
enabled=true
contrast=0.25
intensity=1
saturation=5

[AdaptiveTransparency]
enabled=true' > ~/.local/share/plasma/desktoptheme/Fluent/metadata.desktop

    mkdir -p ~/.local/share/plasma/desktoptheme/Fluent-round

    echo '[Desktop Entry]
Name=Fluent-round
Comment=Fluent theme for plasma

X-KDE-PluginInfo-Author=Vince Liuice
X-KDE-PluginInfo-Email=vinceliuice@hotmail.com
X-KDE-PluginInfo-Name=Fluent-round
X-KDE-PluginInfo-Version=5.78.0
X-KDE-PluginInfo-Website=https://plasma.kde.org
X-KDE-PluginInfo-Category=
X-KDE-PluginInfo-License=LGPL
X-KDE-PluginInfo-EnabledByDefault=true
X-Plasma-API=5.0

[ContrastEffect]
enabled=true
contrast=0.25
intensity=1
saturation=5

[AdaptiveTransparency]
enabled=true' > ~/.local/share/plasma/desktoptheme/Fluent-round/metadata.desktop

fi

#portal-kde-verify.sh &


QSG_RENDER_LOOP=basic exec $STARTPLASMA
