#!/bin/bash

set -e          # exit on error
set -o pipefail # exit on pipeline error
set -u          # treat unset variable as error

. /minioslib
. /minios_build.conf

SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"

case "$DISTRIBUTION" in
trixie | sid)
    rm -rf /usr/share/backgrounds
    rm -rf /usr/share/pixmaps
    : 'rm -f /etc/skel/.config/xfce4/xfconf/xfce-perchannel-xml/xfce4-desktop.xml
    cat <<EOF >/etc/skel/.config/xfce4/xfconf/xfce-perchannel-xml/xfce4-desktop.xml
<?xml version="1.1" encoding="UTF-8"?>

<channel name="xfce4-desktop" version="1.0">
  <property name="desktop-icons" type="empty">
    <property name="file-icons" type="empty">
      <property name="show-removable" type="bool" value="false"/>
      <property name="show-home" type="bool" value="true"/>
      <property name="show-filesystem" type="bool" value="false"/>
      <property name="show-trash" type="bool" value="true"/>
      <property name="show-mounts" type="bool" value="false"/>
    </property>
  </property>
  <property name="last-settings-migration-version" type="uint" value="1"/>
</channel>
EOF'
    ;;
*)
    rm -rf /usr/share/backgrounds
    rm -rf /usr/share/pixmaps
    ;;
esac

/condinapt -l "${SCRIPT_DIR}/packages.list" -c "${SCRIPT_DIR}/minios_build.conf" -m "${SCRIPT_DIR}/condinapt.map"
if [ $? -ne 0 ]; then
    echo "Failed to install packages."
    exit 1
fi

echo "export QT_QPA_PLATFORMTHEME=gtk2" >>/etc/environment

case "$LOCALE" in
"en_US" | "C")
    xmlstarlet ed -L \
        -u '/channel/property/property[@name="XkbOptions"]/property[@name="XkbLayout"]/@value' \
        -v "us" \
        "/etc/skel/.config/xfce4/xfconf/xfce-perchannel-xml/keyboard-layout.xml"
    ;;
*)
    xmlstarlet ed -L \
        -u '/channel/property/property[@name="XkbOptions"]/property[@name="XkbLayout"]/@value' \
        -v "us,$LAYOUTID" \
        "/etc/skel/.config/xfce4/xfconf/xfce-perchannel-xml/keyboard-layout.xml"
    ;;
esac

echo "nodm nodm/enabled boolean true" | debconf-set-selections
echo "samba-common samba-common/dhcp boolean false" | debconf-set-selections

if [ -f /usr/share/applications/yad-icon-browser.desktop ]; then
    rm -f /usr/share/applications/yad-icon-browser.desktop
fi

if [ "$DISTRIBUTION" = "bionic" ]; then
    rm /etc/skel/.config/xfce4/xfconf/xfce-perchannel-xml/thunar.xml
fi

if [ -f "/usr/share/applications/xfce4-clipman-settings.desktop" ]; then
    sed -i "15i Name[ru]=Настройки менеджера буфера обмена" /usr/share/applications/xfce4-clipman-settings.desktop
    sed -i "29i Comment[ru]=Настройте свой буфер обмена" /usr/share/applications/xfce4-clipman-settings.desktop
fi

mv /usr/bin/thunar /usr/bin/thunar-bin
mv /usr/bin/thunar.sh /usr/bin/thunar
chmod +x /usr/bin/thunar

if [ -f /usr/bin/connman-gtk ]; then
    if [ ! -d /etc/skel/.config/autostart ]; then
        mkdir -p /etc/skel/.config/autostart
    fi
    cat <<EOF >/etc/skel/.config/autostart/connman-tray.desktop
[Desktop Entry]
Encoding=UTF-8
Type=Application
Name=connman-tray
Comment=
Exec=connman-tray
OnlyShowIn=XFCE;
StartupNotify=false
Terminal=false
Hidden=false

EOF
    apt-get install dconf-cli
    cat <<EOF >/usr/bin/connman-tray
#!/bin/bash

if [ "$(dconf read /net/connman/gtk/launch-to-tray)" != "true" ]; then
    dconf write /net/connman/gtk/launch-to-tray true
fi
if [ "$(dconf read /net/connman/gtk/status-icon-enabled)" != "true" ]; then
    dconf write /net/connman/gtk/status-icon-enabled true
fi
connman-gtk

EOF
    chmod +x /usr/bin/connman-tray
    rm /usr/share/applications/connman-gtk.desktop
fi

# Apply XML changes
XML_XSETTINGS=/etc/skel/.config/xfce4/xfconf/xfce-perchannel-xml/xsettings.xml
XML_XFWM4=/etc/skel/.config/xfce4/xfconf/xfce-perchannel-xml/xfwm4.xml

# Set FontName to "Inter 10"
xmlstarlet ed -L \
    -u "//property[@name='FontName']/@type" -v "string" \
    -i "//property[@name='FontName']" -t attr -n "value" -v "Inter 10" \
    "$XML_XSETTINGS"

# Set MonospaceFontName to "JetBrains Mono 10"
xmlstarlet ed -L \
    -u "//property[@name='MonospaceFontName']/@type" -v "string" \
    -i "//property[@name='MonospaceFontName']" -t attr -n "value" -v "JetBrains Mono 10" \
    "$XML_XSETTINGS"

# Enable antialiasing
xmlstarlet ed -L \
    -u "//property[@name='Antialias']/@type" -v "int" \
    -i "//property[@name='Antialias']" -t attr -n "value" -v "1" \
    "$XML_XSETTINGS"

# Change hint style to "hintfull"
xmlstarlet ed -L \
    -u "//property[@name='HintStyle']/@value" -v "hintfull" \
    "$XML_XSETTINGS"

# Replace "Sans Bold 10" with "Inter Bold 10" in xfwm4.xml
xmlstarlet ed -L \
    -u "//property[contains(@value,'Sans Bold 10')]/@value" -v "Inter Bold 10" \
    "$XML_XFWM4"

if pkg_is_installed "xfce4-terminal"; then
    update-alternatives --set x-terminal-emulator /usr/bin/xfce4-terminal.wrapper >/dev/null 2>&1
fi
if [ -f /usr/share/applications/pavucontrol.desktop ]; then
    rm -f /usr/share/applications/pavucontrol.desktop
fi

if [ -f /usr/share/applications/nm-connection-editor.desktop ]; then
    if ! grep -q "Name\[ru\]" /usr/share/applications/nm-connection-editor.desktop; then
        sed -i "7i Name\[ru\]=Расширенная конфигурация сети" /usr/share/applications/nm-connection-editor.desktop
    fi
fi

if pkg_is_installed "xfce4-whiskermenu-plugin"; then
    if [ $DISTRIBUTION_PHASE = "legacy" ]; then
        echo "favorites=exo-terminal-emulator.desktop,exo-file-manager.desktop,exo-mail-reader.desktop,exo-web-browser.desktop" >/etc/skel/.config/xfce4/panel/whiskermenu-1.rc
    else
        echo "favorites=xfce4-terminal-emulator.desktop,xfce4-file-manager.desktop,xfce4-web-browser.desktop,xfce4-mail-reader.desktop" >/etc/skel/.config/xfce4/panel/whiskermenu-1.rc
    fi
    cat <<EOF >>/etc/skel/.config/xfce4/panel/whiskermenu-1.rc
recent=
button-icon=/usr/share/pixmaps/MiniOS-white.svg
button-single-row=false
show-button-title=false
show-button-icon=true
launcher-show-name=true
launcher-show-description=false
launcher-show-tooltip=true
item-icon-size=2
hover-switch-category=false
category-show-name=true
category-icon-size=1
load-hierarchy=false
view-as-icons=false
default-category=0
recent-items-max=10
favorites-in-recent=true
position-search-alternate=true
position-commands-alternate=false
position-categories-alternate=true
stay-on-focus-out=false
confirm-session-command=true
menu-width=450
menu-height=500
menu-opacity=100
command-settings=xfce4-settings-manager
show-command-settings=true
command-lockscreen=xflock4
show-command-lockscreen=false
command-switchuser=dm-tool switch-to-greeter
show-command-switchuser=false
command-logoutuser=xfce4-session-logout --logout --fast
show-command-logoutuser=false
command-restart=xfce4-session-logout --reboot --fast
show-command-restart=false
command-shutdown=xfce4-session-logout --halt --fast
show-command-shutdown=false
command-suspend=xfce4-session-logout --suspend
show-command-suspend=false
command-hibernate=xfce4-session-logout --hibernate
show-command-hibernate=false
command-logout=xfce4-session-logout
show-command-logout=true
command-menueditor=menulibre
show-command-menueditor=true
command-profile=mugshot
show-command-profile=false
search-actions=5

[action0]
name=Man Pages
pattern=#
command=exo-open --launch TerminalEmulator man %s
regex=false

[action1]
name=Web Search
pattern=?
command=exo-open --launch WebBrowser https://duckduckgo.com/?q=%u
regex=false

[action2]
name=Wikipedia
pattern=!w
command=exo-open --launch WebBrowser https://en.wikipedia.org/wiki/%u
regex=false

[action3]
name=Run in Terminal
pattern=!
command=exo-open --launch TerminalEmulator %s
regex=false

[action4]
name=Open URI
pattern=^(file|http|https):\\/\\/(.*)$
command=exo-open \\0
regex=true


EOF
else
    cat <<EOF >/etc/skel/.config/xfce4/xfconf/xfce-perchannel-xml/xfce4-panel.xml
<?xml version="1.0" encoding="UTF-8"?>

<channel name="xfce4-panel" version="1.0">
  <property name="configver" type="int" value="2"/>
  <property name="panels" type="array">
    <value type="int" value="1"/>
    <property name="panel-1" type="empty">
      <property name="position" type="string" value="p=8;x=512;y=752"/>
      <property name="length" type="uint" value="100"/>
      <property name="position-locked" type="bool" value="true"/>
      <property name="size" type="uint" value="34"/>
      <property name="background-alpha" type="uint" value="90"/>
      <property name="mode" type="uint" value="0"/>
      <property name="enter-opacity" type="uint" value="100"/>
      <property name="leave-opacity" type="uint" value="100"/>
      <property name="plugin-ids" type="array">
        <value type="int" value="1"/>
        <value type="int" value="2"/>
        <value type="int" value="3"/>
        <value type="int" value="4"/>
        <value type="int" value="5"/>
        <value type="int" value="6"/>
        <value type="int" value="7"/>
        <value type="int" value="8"/>
        <value type="int" value="9"/>
        <value type="int" value="10"/>
        <value type="int" value="12"/>
        <value type="int" value="13"/>
        <value type="int" value="14"/>
      </property>
    </property>
    <property name="dark-mode" type="bool" value="true"/>
  </property>
  <property name="plugins" type="empty">
    <property name="plugin-1" type="string" value="applicationsmenu">
      <property name="button-icon" type="string" value="/usr/share/pixmaps/MiniOS-white.svg"/>
      <property name="show-button-title" type="bool" value="false"/>
    </property>
    <property name="plugin-2" type="string" value="separator">
      <property name="style" type="uint" value="0"/>
    </property>
    <property name="plugin-3" type="string" value="launcher">
      <property name="items" type="array">
        <value type="string" value="TerminalEmulator.desktop"/>
      </property>
    </property>
    <property name="plugin-4" type="string" value="launcher">
      <property name="items" type="array">
        <value type="string" value="FileManager.desktop"/>
      </property>
    </property>
    <property name="plugin-5" type="string" value="launcher">
      <property name="items" type="array">
        <value type="string" value="WebBrowser.desktop"/>
      </property>
    </property>
    <property name="plugin-6" type="string" value="separator">
      <property name="style" type="uint" value="0"/>
    </property>
    <property name="plugin-7" type="string" value="tasklist">
      <property name="show-handle" type="bool" value="false"/>
      <property name="flat-buttons" type="bool" value="true"/>
      <property name="show-labels" type="bool" value="true"/>
      <property name="grouping" type="uint" value="1"/>
    </property>
    <property name="plugin-8" type="string" value="separator">
      <property name="style" type="uint" value="0"/>
      <property name="expand" type="bool" value="true"/>
    </property>
    <property name="plugin-9" type="string" value="xkb">
      <property name="display-type" type="uint" value="2"/>
      <property name="display-name" type="uint" value="0"/>
      <property name="group-policy" type="uint" value="0"/>
    </property>
    <property name="plugin-10" type="string" value="battery"/>
    <property name="plugin-12" type="string" value="systray">
      <property name="known-legacy-items" type="array">
        <value type="string" value="task manager"/>
        <value type="string" value="volumeicon"/>
        <value type="string" value="networkmanager applet"/>
      </property>
    </property>
    <property name="plugin-13" type="string" value="clock">
      <property name="digital-format" type="string" value="%_H:%M"/>
    </property>
    <property name="plugin-14" type="string" value="showdesktop"/>
  </property>
</channel>

EOF
fi

if ! pkg_is_installed "xfce4-terminal" >/dev/null 2>&1 && pkg_is_installed "xfce4-screenshooter" >/dev/null 2>&1; then
    cat <<EOF >/etc/skel/.config/xfconf/xfce-perchannel-xml/xfce4-keyboard-shortcuts.xml
<?xml version="1.0" encoding="UTF-8"?>

<channel name="xfce4-keyboard-shortcuts" version="1.0">
  <property name="commands" type="empty">
    <property name="default" type="empty">
      <property name="&lt;Alt&gt;F1" type="empty"/>
      <property name="&lt;Alt&gt;F2" type="empty">
        <property name="startup-notify" type="empty"/>
      </property>
      <property name="&lt;Alt&gt;F3" type="empty">
        <property name="startup-notify" type="empty"/>
      </property>
      <property name="&lt;Primary&gt;&lt;Alt&gt;Delete" type="empty"/>
      <property name="&lt;Primary&gt;&lt;Alt&gt;l" type="empty"/>
      <property name="&lt;Primary&gt;&lt;Alt&gt;t" type="empty"/>
      <property name="XF86Display" type="empty"/>
      <property name="&lt;Super&gt;p" type="empty"/>
      <property name="&lt;Primary&gt;Escape" type="empty"/>
      <property name="XF86WWW" type="empty"/>
      <property name="HomePage" type="empty"/>
      <property name="XF86Mail" type="empty"/>
      <property name="Print" type="empty"/>
      <property name="&lt;Alt&gt;Print" type="empty"/>
      <property name="&lt;Shift&gt;Print" type="empty"/>
      <property name="&lt;Super&gt;e" type="empty"/>
      <property name="&lt;Primary&gt;&lt;Alt&gt;f" type="empty"/>
      <property name="&lt;Primary&gt;&lt;Alt&gt;Escape" type="empty"/>
      <property name="&lt;Primary&gt;&lt;Shift&gt;Escape" type="empty"/>
      <property name="&lt;Super&gt;r" type="empty">
        <property name="startup-notify" type="empty"/>
      </property>
      <property name="&lt;Alt&gt;&lt;Super&gt;s" type="empty"/>
    </property>
    <property name="custom" type="empty">
      <property name="&lt;Alt&gt;F2" type="string" value="xfce4-appfinder --collapsed">
        <property name="startup-notify" type="bool" value="true"/>
      </property>
      <property name="&lt;Super&gt;r" type="string" value="xfce4-appfinder -c">
        <property name="startup-notify" type="bool" value="true"/>
      </property>
      <property name="XF86WWW" type="string" value="exo-open --launch WebBrowser"/>
      <property name="XF86Mail" type="string" value="exo-open --launch MailReader"/>
      <property name="&lt;Alt&gt;F3" type="string" value="xfce4-appfinder">
        <property name="startup-notify" type="bool" value="true"/>
      </property>
      <property name="&lt;Primary&gt;Escape" type="string" value="xfdesktop --menu"/>
      <property name="&lt;Primary&gt;&lt;Alt&gt;Delete" type="string" value="xfce4-session-logout"/>
      <property name="&lt;Primary&gt;&lt;Alt&gt;t" type="string" value="exo-open --launch TerminalEmulator"/>
      <property name="&lt;Primary&gt;&lt;Alt&gt;f" type="string" value="thunar"/>
      <property name="&lt;Primary&gt;&lt;Alt&gt;l" type="string" value="xflock4"/>
      <property name="&lt;Alt&gt;F1" type="string" value="xfce4-popup-applicationsmenu"/>
      <property name="&lt;Super&gt;p" type="string" value="xfce4-display-settings --minimal"/>
      <property name="&lt;Super&gt;e" type="string" value="thunar"/>
      <property name="&lt;Primary&gt;&lt;Alt&gt;Escape" type="string" value="xkill"/>
      <property name="HomePage" type="string" value="exo-open --launch WebBrowser"/>
      <property name="XF86Display" type="string" value="xfce4-display-settings --minimal"/>
      <property name="override" type="bool" value="true"/>
      <property name="&lt;Primary&gt;&lt;Shift&gt;Escape" type="string" value="xterm -T &quot;Task Manager&quot; -e &quot;htop&quot;"/>
    </property>
  </property>
  <property name="xfwm4" type="empty">
    <property name="default" type="empty">
      <property name="&lt;Alt&gt;Insert" type="empty"/>
      <property name="Escape" type="empty"/>
      <property name="Left" type="empty"/>
      <property name="Right" type="empty"/>
      <property name="Up" type="empty"/>
      <property name="Down" type="empty"/>
      <property name="&lt;Alt&gt;Tab" type="empty"/>
      <property name="&lt;Alt&gt;&lt;Shift&gt;Tab" type="empty"/>
      <property name="&lt;Alt&gt;Delete" type="empty"/>
      <property name="&lt;Primary&gt;&lt;Alt&gt;Down" type="empty"/>
      <property name="&lt;Primary&gt;&lt;Alt&gt;Left" type="empty"/>
      <property name="&lt;Shift&gt;&lt;Alt&gt;Page_Down" type="empty"/>
      <property name="&lt;Alt&gt;F4" type="empty"/>
      <property name="&lt;Alt&gt;F6" type="empty"/>
      <property name="&lt;Alt&gt;F7" type="empty"/>
      <property name="&lt;Alt&gt;F8" type="empty"/>
      <property name="&lt;Alt&gt;F9" type="empty"/>
      <property name="&lt;Alt&gt;F10" type="empty"/>
      <property name="&lt;Alt&gt;F11" type="empty"/>
      <property name="&lt;Alt&gt;F12" type="empty"/>
      <property name="&lt;Primary&gt;&lt;Shift&gt;&lt;Alt&gt;Left" type="empty"/>
      <property name="&lt;Primary&gt;&lt;Alt&gt;End" type="empty"/>
      <property name="&lt;Primary&gt;&lt;Alt&gt;Home" type="empty"/>
      <property name="&lt;Primary&gt;&lt;Shift&gt;&lt;Alt&gt;Right" type="empty"/>
      <property name="&lt;Primary&gt;&lt;Shift&gt;&lt;Alt&gt;Up" type="empty"/>
      <property name="&lt;Primary&gt;&lt;Alt&gt;KP_1" type="empty"/>
      <property name="&lt;Primary&gt;&lt;Alt&gt;KP_2" type="empty"/>
      <property name="&lt;Primary&gt;&lt;Alt&gt;KP_3" type="empty"/>
      <property name="&lt;Primary&gt;&lt;Alt&gt;KP_4" type="empty"/>
      <property name="&lt;Primary&gt;&lt;Alt&gt;KP_5" type="empty"/>
      <property name="&lt;Primary&gt;&lt;Alt&gt;KP_6" type="empty"/>
      <property name="&lt;Primary&gt;&lt;Alt&gt;KP_7" type="empty"/>
      <property name="&lt;Primary&gt;&lt;Alt&gt;KP_8" type="empty"/>
      <property name="&lt;Primary&gt;&lt;Alt&gt;KP_9" type="empty"/>
      <property name="&lt;Alt&gt;space" type="empty"/>
      <property name="&lt;Shift&gt;&lt;Alt&gt;Page_Up" type="empty"/>
      <property name="&lt;Primary&gt;&lt;Alt&gt;Right" type="empty"/>
      <property name="&lt;Primary&gt;&lt;Alt&gt;d" type="empty"/>
      <property name="&lt;Primary&gt;&lt;Alt&gt;Up" type="empty"/>
      <property name="&lt;Super&gt;Tab" type="empty"/>
      <property name="&lt;Primary&gt;F1" type="empty"/>
      <property name="&lt;Primary&gt;F2" type="empty"/>
      <property name="&lt;Primary&gt;F3" type="empty"/>
      <property name="&lt;Primary&gt;F4" type="empty"/>
      <property name="&lt;Primary&gt;F5" type="empty"/>
      <property name="&lt;Primary&gt;F6" type="empty"/>
      <property name="&lt;Primary&gt;F7" type="empty"/>
      <property name="&lt;Primary&gt;F8" type="empty"/>
      <property name="&lt;Primary&gt;F9" type="empty"/>
      <property name="&lt;Primary&gt;F10" type="empty"/>
      <property name="&lt;Primary&gt;F11" type="empty"/>
      <property name="&lt;Primary&gt;F12" type="empty"/>
      <property name="&lt;Super&gt;KP_Left" type="empty"/>
      <property name="&lt;Super&gt;KP_Right" type="empty"/>
      <property name="&lt;Super&gt;KP_Down" type="empty"/>
      <property name="&lt;Super&gt;KP_Up" type="empty"/>
      <property name="&lt;Super&gt;KP_Page_Up" type="empty"/>
      <property name="&lt;Super&gt;KP_Home" type="empty"/>
      <property name="&lt;Super&gt;KP_End" type="empty"/>
      <property name="&lt;Super&gt;KP_Next" type="empty"/>
    </property>
    <property name="custom" type="empty">
      <property name="Up" type="string" value="up_key"/>
      <property name="&lt;Primary&gt;&lt;Alt&gt;KP_9" type="string" value="move_window_workspace_9_key"/>
      <property name="&lt;Primary&gt;&lt;Alt&gt;KP_8" type="string" value="move_window_workspace_8_key"/>
      <property name="Left" type="string" value="left_key"/>
      <property name="&lt;Primary&gt;&lt;Alt&gt;KP_6" type="string" value="move_window_workspace_6_key"/>
      <property name="&lt;Alt&gt;Insert" type="string" value="add_workspace_key"/>
      <property name="&lt;Alt&gt;Tab" type="string" value="cycle_windows_key"/>
      <property name="&lt;Alt&gt;&lt;Shift&gt;Tab" type="string" value="cycle_reverse_windows_key"/>
      <property name="&lt;Primary&gt;&lt;Alt&gt;KP_7" type="string" value="move_window_workspace_7_key"/>
      <property name="&lt;Primary&gt;&lt;Alt&gt;Right" type="string" value="right_workspace_key"/>
      <property name="&lt;Primary&gt;&lt;Shift&gt;&lt;Alt&gt;Right" type="string" value="move_window_right_key"/>
      <property name="&lt;Primary&gt;&lt;Alt&gt;d" type="string" value="show_desktop_key"/>
      <property name="&lt;Primary&gt;&lt;Alt&gt;Up" type="string" value="up_workspace_key"/>
      <property name="&lt;Primary&gt;F7" type="string" value="workspace_7_key"/>
      <property name="&lt;Primary&gt;&lt;Alt&gt;Home" type="string" value="move_window_prev_workspace_key"/>
      <property name="&lt;Alt&gt;F4" type="string" value="close_window_key"/>
      <property name="&lt;Primary&gt;&lt;Shift&gt;&lt;Alt&gt;Left" type="string" value="move_window_left_key"/>
      <property name="&lt;Alt&gt;F6" type="string" value="stick_window_key"/>
      <property name="&lt;Alt&gt;F10" type="string" value="maximize_window_key"/>
      <property name="&lt;Alt&gt;F12" type="string" value="above_key"/>
      <property name="&lt;Alt&gt;F9" type="string" value="hide_window_key"/>
      <property name="&lt;Primary&gt;&lt;Alt&gt;Down" type="string" value="down_workspace_key"/>
      <property name="&lt;Alt&gt;F8" type="string" value="resize_window_key"/>
      <property name="&lt;Super&gt;Tab" type="string" value="switch_window_key"/>
      <property name="Escape" type="string" value="cancel_key"/>
      <property name="&lt;Primary&gt;&lt;Alt&gt;End" type="string" value="move_window_next_workspace_key"/>
      <property name="&lt;Primary&gt;F10" type="string" value="workspace_10_key"/>
      <property name="&lt;Primary&gt;F11" type="string" value="workspace_11_key"/>
      <property name="&lt;Alt&gt;F11" type="string" value="fullscreen_key"/>
      <property name="&lt;Primary&gt;&lt;Shift&gt;&lt;Alt&gt;Up" type="string" value="move_window_up_key"/>
      <property name="Right" type="string" value="right_key"/>
      <property name="Down" type="string" value="down_key"/>
      <property name="&lt;Alt&gt;F7" type="string" value="move_window_key"/>
      <property name="&lt;Shift&gt;&lt;Alt&gt;Page_Down" type="string" value="lower_window_key"/>
      <property name="&lt;Primary&gt;F12" type="string" value="workspace_12_key"/>
      <property name="&lt;Primary&gt;F1" type="string" value="workspace_1_key"/>
      <property name="&lt;Primary&gt;&lt;Alt&gt;Left" type="string" value="left_workspace_key"/>
      <property name="&lt;Primary&gt;F2" type="string" value="workspace_2_key"/>
      <property name="&lt;Primary&gt;F4" type="string" value="workspace_4_key"/>
      <property name="&lt;Primary&gt;F5" type="string" value="workspace_5_key"/>
      <property name="&lt;Primary&gt;F6" type="string" value="workspace_6_key"/>
      <property name="&lt;Alt&gt;space" type="string" value="popup_menu_key"/>
      <property name="&lt;Primary&gt;F8" type="string" value="workspace_8_key"/>
      <property name="&lt;Primary&gt;F9" type="string" value="workspace_9_key"/>
      <property name="&lt;Primary&gt;&lt;Alt&gt;KP_1" type="string" value="move_window_workspace_1_key"/>
      <property name="&lt;Alt&gt;Delete" type="string" value="del_workspace_key"/>
      <property name="&lt;Shift&gt;&lt;Alt&gt;Page_Up" type="string" value="raise_window_key"/>
      <property name="&lt;Primary&gt;F3" type="string" value="workspace_3_key"/>
      <property name="&lt;Primary&gt;&lt;Alt&gt;KP_2" type="string" value="move_window_workspace_2_key"/>
      <property name="&lt;Primary&gt;&lt;Alt&gt;KP_3" type="string" value="move_window_workspace_3_key"/>
      <property name="&lt;Primary&gt;&lt;Alt&gt;KP_4" type="string" value="move_window_workspace_4_key"/>
      <property name="&lt;Primary&gt;&lt;Alt&gt;KP_5" type="string" value="move_window_workspace_5_key"/>
      <property name="override" type="bool" value="true"/>
    </property>
  </property>
  <property name="providers" type="array">
    <value type="string" value="xfwm4"/>
    <value type="string" value="commands"/>
  </property>
</channel>
EOF
fi

sed -i -e "/FontName=/s/=.*/=JetBrains Mono Light 10/" /etc/skel/.config/xfce4/terminal/terminalrc

if pkg_is_installed "xfce4-power-manager-plugins"; then
    xmlstarlet ed -L \
        -u '//property[@type="string"][@value="battery"]/@value' \
        -v 'power-manager-plugin' \
        /etc/skel/.config/xfce4/xfconf/xfce-perchannel-xml/xfce4-panel.xml
fi

if pkg_is_installed "xfce4-terminal"; then
    cat <<EOF >/etc/skel/.config/xfce4/helpers.rc
TerminalEmulator=xfce4-terminal
FileManager=Thunar
WebBrowser=

EOF
else
    cat <<EOF >/etc/skel/.config/xfce4/helpers.rc
TerminalEmulator=xterm
FileManager=Thunar
WebBrowser=

EOF
fi

if [ $DISTRIBUTION_TYPE = "debian" ] && [[ $DESKTOP_ENVIRONMENT == *"xfce"* ]]; then
    if [ $DISTRIBUTION_PHASE = "legacy" ]; then
        sed -i "s/exo-open/env XDG_CURRENT_DESKTOP=XFCE exo-open/g" /usr/share/applications/exo-web-browser.desktop
    else
        sed -i "s/exo-open/env XDG_CURRENT_DESKTOP=XFCE exo-open/g" /usr/share/applications/xfce4-web-browser.desktop
    fi
    sed -i "s/exo-open/env XDG_CURRENT_DESKTOP=XFCE exo-open/g" /etc/skel/.config/xfce4/panel/launcher-5/WebBrowser.desktop
fi

if ! pkg_is_installed "xfce4-terminal"; then
    update-alternatives --set x-terminal-emulator /usr/bin/xterm >/dev/null 2>&1
fi

rm -f /usr/share/backgrounds/xfce/*
rm -f /usr/share/backgrounds/greybird.svg
rm -f /usr/share/wallpapers/*
if [ ! -d /usr/share/backgrounds/xfce ]; then
    mkdir -p /usr/share/backgrounds/xfce
fi

find /usr/share/backgrounds/ -maxdepth 1 -type f ! -name "MiniOS5*" -delete

set_wallpaper() {
    local WALLPAPER=$1

    case "$DISTRIBUTION" in
    #bookworm | trixie | sid)
    *)
        # Update link to reference selected wallpaper
        rm -f /usr/share/backgrounds/minios-wallpaper.jpg
        ln -s "/usr/share/backgrounds/MiniOS5-wallpaper-${WALLPAPER}.jpg" /usr/share/backgrounds/minios-wallpaper.jpg
        ;;
        #*)
        # Update desktop XML to reference selected wallpaper directly
        #    local XML_FILE="/etc/skel/.config/xfce4/xfconf/xfce-perchannel-xml/xfce4-desktop.xml"
        #    xmlstarlet ed -L \
        #        -u "//property[@name='image-path']/@value" \
        #        -v "/usr/share/backgrounds/MiniOS5-wallpaper-${WALLPAPER}.jpg" \
        #        "$XML_FILE"
        #    ;;
    esac

    # Remove other wallpaper files
    for OTHER_WALLPAPER in Minimum Flux Standard Toolbox Ultra; do
        if [[ "$OTHER_WALLPAPER" != "$WALLPAPER" ]]; then
            rm -f "/usr/share/backgrounds/MiniOS5-wallpaper-$OTHER_WALLPAPER.jpg"
        fi
    done
}

if [ "$PACKAGE_VARIANT" = "minimum" ] && [ "$DESKTOP_ENVIRONMENT" = "flux" ]; then
    set_wallpaper "Flux"
elif [ "$PACKAGE_VARIANT" = "minimum" ]; then
    set_wallpaper "Minimum"
elif [ "$PACKAGE_VARIANT" = "standard" ]; then
    set_wallpaper "Standard"
elif [ "$PACKAGE_VARIANT" = "toolbox" ]; then
    set_wallpaper "Toolbox"
elif [ "$PACKAGE_VARIANT" = "ultra" ]; then
    set_wallpaper "Ultra"
    xmlstarlet ed -L \
        -u "//property[@name='ThemeName']/@value" \
        -v "Greybird-dark" \
        /etc/skel/.config/xfce4/xfconf/xfce-perchannel-xml/xsettings.xml
    xmlstarlet ed -L \
        -u "//property[@name='theme']/@value" \
        -v "Greybird-dark" \
        /etc/skel/.config/xfce4/xfconf/xfce-perchannel-xml/xfwm4.xml
else
    set_wallpaper "sea"
fi

find /usr/share/backgrounds/ -maxdepth 1 -type f ! -name "MiniOS5*" ! -delete

# if we're NOT running flux, and we're on the minimum variant,
# delete everything except the two images we want to keep
if [ "$PACKAGE_VARIANT" = "minimum" ]; then
    case "$DESKTOP_ENVIRONMENT" in
    "flux")
        KEEP_FILES=("MiniOS5-wallpaper-Flux.jpg" "MiniOS5-background-jungle.jpg")
        ;;
    *)
        KEEP_FILES=("MiniOS5-wallpaper-Minimum.jpg" "MiniOS5-background-jungle.jpg")
        ;;
    esac

    find /usr/share/backgrounds -type f ! -name "${KEEP_FILES[0]}" ! -name "${KEEP_FILES[1]}" -delete
fi

if [ $PACKAGE_VARIANT != "minimum" ]; then
    rm -f /usr/share/applications/taskmanager.desktop
fi
