#!/usr/bin/env bash

# Purpose: set default SolydX desktop
# Documentation: https://docs.xfce.org/xfce/xfconf/xfconf-query
# Function: set_property [channel] [property] [type] [value] [optional: force_array]
# Separate array items with pipes: "item1|item2|item3"
#                         strings: '"item 1"|"item 2"|"item 3"'
# List objects: xfconf-query --channel xfce4-panel -l -v

MAX_WAIT=180   # max duration to wait xfconfd for in seconds (3 minutes = 180 seconds)

# Check if not already configured (only run once)
[ "$(ls -A $HOME/.config/xfce4/xfconf/xfce-perchannel-xml 2>/dev/null)" ] && exit

if [ -z "$(which xrandr)" ] || [ -z "$(which xfconf-query)" ]; then
    exit
fi

# Set wallpaper path
WALLPAPER=$1
[ -z "$WALLPAPER" ] && WALLPAPER='/usr/share/images/desktop-base/default'
[ ! -e "$WALLPAPER" ] && exit

#xfconfd must be running
#xfconfd is dbus-activatable, and will be activated and queried with xfconf-query
SECS=0
until xfconf-query --channel xfce4-desktop -l 2>/dev/null | grep -q /; do
    sleep 1
    SECS=$((SECS + 1))
    [ "$SECS" -ge "$MAX_WAIT" ] && exit 1
done

function set_property {
    BASE="xfconf-query --channel $1 --property $2"

    IFS='|' read -ra VALUES <<< "$4"
    SET=''
    for VALUE in "${VALUES[@]}"; do
        if [ "$3" == string ]; then
            SET="$SET --type $3 --set \"$VALUE\""
        else
            SET="$SET --type $3 --set $VALUE"
        fi
    done
    unset IFS

    # Force an array even if there is only one item
    if [ "$5" == 'true' ]; then
        SET="--force-array $SET"
    fi

    # Create the property if it does not exist
    if ! $BASE >/dev/null 2>&1; then
        SET="--create $SET"
    fi

    eval "$BASE $SET"
}

# Set all workspace wallpapers
SCREENS=$(xfconf-query --channel xfce4-desktop --property /backdrop --list | grep -Eo 'screen[^/]+' | uniq)
MONITORS=$(xrandr --query | awk '/ connected/{print $1}')
WSPCNT=$(xfconf-query --channel xfwm4 --property /general/workspace_count 2>/dev/null)
[ -z "$WSPCNT" ] && WSPCNT=4
for SCR in $SCREENS; do
    for MON in $MONITORS; do
        echo "Set wallpaper for monitor: $MON"
        for ((WSP=0; WSP < $WSPCNT; WSP++)); do
            set_property xfce4-desktop /backdrop/${SCR}/monitor${MON}/workspace${WSP}/last-image string "$WALLPAPER"
            set_property xfce4-desktop /backdrop/${SCR}/monitor${MON}/workspace${WSP}/image-style int 5
            set_property xfce4-desktop /backdrop/${SCR}/monitor${MON}/workspace${WSP}/color-style int 0
            set_property xfce4-desktop /backdrop/${SCR}/monitor${MON}/workspace${WSP}/rgba1 double "0.956863|0.721569|0.498039|1"
        done
    done
done

# Make sure to hide the default desktop icons
set_property xfce4-desktop /desktop-icons/file-icons/show-filesystem bool false
set_property xfce4-desktop /desktop-icons/file-icons/show-home bool false
set_property xfce4-desktop /desktop-icons/file-icons/show-removable bool false
set_property xfce4-desktop /desktop-icons/file-icons/show-trash bool false

# xsettings
set_property xsettings /Net/ThemeName string "Breeze-X"
set_property xsettings /Net/IconThemeName string "evolvere-2"
set_property xsettings /Gtk/FontName string "Clear Sans 12"
set_property xsettings /Gtk/MonospaceFontName string "Monospace 12"
set_property xsettings /Gtk/CursorThemeName string "Breeze_Light"
set_property xsettings /Gtk/CursorThemeSize int 24

# Xfwm4
set_property xfwm4 /general/title_font string "Clear Sans 14"
set_property xfwm4 /general/theme string "Breeze-X"

# Thunar
set_property thunar /misc-date-style string "THUNAR_DATE_STYLE_ISO"
set_property thunar /misc-single-click bool false
set_property thunar /last-view string "ThunarDetailsView"

# Terminal
set_property xfce4-terminal /misc-show-unsafe-paste-dialog bool false
set_property xfce4-terminal /color-foreground string "#000000"
set_property xfce4-terminal /color-background string "#ffffff"
set_property xfce4-terminal /color-cursor string "#f5f579790000"
set_property xfce4-terminal /color-cursor-use-default bool false
set_property xfce4-terminal /color-palette string "rgb(70,70,70);rgb(178,24,24);rgb(16,124,16);rgb(201,101,0);rgb(24,24,178);rgb(135,18,135);rgb(18,140,140);rgb(160,160,160);rgb(104,104,104);rgb(255,84,84);rgb(0,192,0);rgb(255,128,0);rgb(73,117,174);rgb(176,58,176);rgb(0,192,192);rgb(195,195,195)"

# Panel
mkdir -p $HOME/.config/xfce4/panel/launcher-4
cp -f /usr/share/applications/thunar.desktop $HOME/.config/xfce4/panel/launcher-4/ 2>/dev/null
chown -R $USER:$USER $HOME/.config/xfce4/panel 2>/dev/null
[ -n "$(pidof xfce4-panel)" ] && xfce4-panel -q
set_property xfce4-panel /panels --reset --recursive
set_property xfce4-panel /plugins --reset --recursive
set_property xfce4-panel /panels int 0 true
set_property xfce4-panel /panels/dark-mode bool false
set_property xfce4-panel /panels/panel-0/position string "p=8;x=960;y=923"
set_property xfce4-panel /panels/panel-0/size uint 46
set_property xfce4-panel /panels/panel-0/position-locked bool true
set_property xfce4-panel /panels/panel-0/length double 100
set_property xfce4-panel /panels/panel-0/enter-opacity uint 85
set_property xfce4-panel /panels/panel-0/leave-opacity uint 85
set_property xfce4-panel /panels/panel-0/plugin-ids int "1|2|3|4|5|6|7|8|9|10|11"
set_property xfce4-panel /panels/panel-0/icon-size uint 0
set_property xfce4-panel /panels/panel-0/border-width uint 4
set_property xfce4-panel /panels/panel-0/nrows uint 1
set_property xfce4-panel /plugins/plugin-1 string "whiskermenu"
set_property xfce4-panel /plugins/plugin-1/button-icon string "solydx"
set_property xfce4-panel /plugins/plugin-1/favorites string '"firefox-esr.desktop"|"thunderbird.desktop"|"libreoffice-startcenter.desktop"|"synaptic.desktop"|"xfce-settings-manager.desktop"|"xfce4-terminal-emulator.desktop"'
set_property xfce4-panel /plugins/plugin-1/menu-height int 500
set_property xfce4-panel /plugins/plugin-1/hover-switch-category bool true
#set_property xfce4-panel /plugins/plugin-1/position-categories-alternate bool true
#set_property xfce4-panel /plugins/plugin-1/position-search-alternate bool true
#set_property xfce4-panel /plugins/plugin-1/position-commands-alternate bool true
set_property xfce4-panel /plugins/plugin-2 string "separator"
set_property xfce4-panel /plugins/plugin-3 string "showdesktop"
set_property xfce4-panel /plugins/plugin-4 string "launcher"
set_property xfce4-panel /plugins/plugin-4/items string "thunar.desktop" true
set_property xfce4-panel /plugins/plugin-5 string "tasklist"
set_property xfce4-panel /plugins/plugin-6 string "separator"
set_property xfce4-panel /plugins/plugin-6/expand bool true
set_property xfce4-panel /plugins/plugin-6/style uint 0
set_property xfce4-panel /plugins/plugin-7 string "systray"
set_property xfce4-panel /plugins/plugin-7/single-row bool true
set_property xfce4-panel /plugins/plugin-7/symbolic-icons bool false
set_property xfce4-panel /plugins/plugin-7/icon-size int 0
set_property xfce4-panel /plugins/plugin-8 string "xfce4-clipman-plugin"
set_property xfce4-panel /plugins/plugin-9 string "pulseaudio"
set_property xfce4-panel /plugins/plugin-10 string "separator"
set_property xfce4-panel /plugins/plugin-11 string "clock"
set_property xfce4-panel /plugins/plugin-11/digital-layout uint 3
set_property xfce4-panel /plugins/plugin-11/digital-time-font string "Sans 16"
xfce4-panel &
