#!/usr/bin/env bash

function autostart_clean() {
    FILENAME="${1}"
    if [ -e "/usr/share/mate/autostart/${FILENAME}" ] && [ -e "${HOME}/.config/autostart/${FILENAME}" ]; then
        rm -f "${HOME}/.config/autostart/${FILENAME}"
    fi
}

function overlay_file() {
    local SYSTEM_FILE="/usr/share/ubuntu-mate/settings-overlay/${1}"
    local USER_FILE="${HOME}/.${1}"
    local USER_DIR=$(dirname "${USER_FILE}")

    # If the file doesn't exist or is zero-bytes then overlay the
    # configuration.
    if [ ! -f "${USER_FILE}" ] || [ ! -s "${USER_FILE}" ]; then
        mkdir -p "${USER_DIR}"
        cp "${SYSTEM_FILE}" "${USER_FILE}"
    fi
}

if [ ! -d "${HOME}/.config/plank/dock1" ]; then
    overlay_file config/plank/dock1/launchers/desktop.dockitem
    overlay_file config/plank/dock1/launchers/firefox_firefox.dockitem
    overlay_file config/plank/dock1/launchers/caja-browser.dockitem
    overlay_file config/plank/dock1/launchers/matecc.dockitem
    overlay_file config/plank/dock1/launchers/mate-system-monitor.dockitem
    overlay_file config/plank/dock1/launchers/mate-terminal.dockitem
    overlay_file config/plank/dock1/launchers/mate-calc.dockitem
    overlay_file config/plank/dock1/launchers/clock.dockitem
    overlay_file config/plank/dock1/launchers/trash.dockitem
fi
overlay_file config/folder-color/hide_donation
overlay_file config/libreoffice/4/user/registrymodifications.xcu
overlay_file config/smplayer/smplayer.ini
overlay_file config/tilda/config_0
overlay_file config/Webcamoid/Webcamoid.conf
overlay_file config/Trolltech.conf

# Now that the HUD is enabled by default at the system
# level, any duplicate local autostart files can be removed.
autostart_clean mate-hud.desktop
autostart_clean ubuntu-mate-welcome.desktop

# Migrate from Ambiant/Radiant themes for Yaru/Yaru-MATE
ambiant="Ambiant-MATE"
ambiant_dark="Ambiant-MATE-Dark"
radiant="Radiant-MATE"

yaru_mate_light="'Yaru-MATE-light'"
yaru_mate_dark="'Yaru-MATE-dark'"
yaru_light="'Yaru'"
yaru_dark="'Yaru-dark'"

color_scheme_light="'default'"
color_scheme_dark="'prefer-dark'"

plank_light="'Yaru-light'"
plank_dark="'Yaru-dark'"
cursor_theme="'Yaru'"

gtk_theme=$(gsettings get org.mate.interface gtk-theme | cut -d"'" -f2)
if [ ! -e "${HOME}/.themes/${gtk_theme}/index.theme" ] && \
   [ ! -e "${HOME}/.local/share/${gtk_theme}/index.theme" ] && \
   [ ! -e "/usr/share/themes/${gtk_theme}/index.theme" ]; then
    # Migrate from missing gtk theme
    case ${gtk_theme} in
        "$ambiant"|"$ambiant_dark")
            gsettings set org.mate.interface gtk-theme "$yaru_mate_dark"
            gsettings set org.gnome.desktop.interface gtk-theme "$yaru_mate_dark"
            gsettings set org.gnome.desktop.interface color-scheme "$color_scheme_dark"
            gsettings set net.launchpad.plank.dock.settings theme "$plank_dark"
            dconf write /net/launchpad/plank/docks/dock1/theme "$plank_dark"
            ;;
        *)
            gsettings set org.mate.interface gtk-theme "$yaru_mate_light"
            gsettings set org.gnome.desktop.interface gtk-theme "$yaru_mate_light"
            gsettings set org.gnome.desktop.interface color-scheme "$color_scheme_light"
            gsettings set net.launchpad.plank.dock.settings theme "$plank_light"
            dconf write /net/launchpad/plank/docks/dock1/theme "$plank_dark"
            ;;
    esac
fi

icon_theme=$(gsettings get org.mate.interface icon-theme | cut -d"'" -f2)
if [ ! -e "${HOME}/.icons/${icon_theme}/index.theme" ] && \
   [ ! -e "${HOME}/.local/share/icons/${icon_theme}/index.theme" ] && \
   [ ! -e "/usr/share/icons/${icon_theme}/index.theme" ]; then
    # Migrate from missing icon theme
    case ${icon_theme} in
        "$ambiant"|"$ambiant_dark")
            gsettings set org.mate.interface icon-theme "$yaru_mate_dark"
            gsettings set org.gnome.desktop.interface icon-theme "$yaru_mate_dark"
            ;;
        *)
            gsettings set org.mate.interface icon-theme "$yaru_mate_light"
            gsettings set org.gnome.desktop.interface icon-theme "$yaru_mate_light"
            ;;
    esac
fi

# Migrate from Ambiant/Radiant/Yaru-MATE xcursor theme
case $(gsettings get org.mate.peripherals-mouse cursor-theme) in
    "$ambiant"|"$ambiant_dark"|"$radiant"|"$yaru_mate_light"|"$yaru_mate_dark")
        gsettings set org.mate.peripherals-mouse cursor-theme "$cursor_theme"
        gsettings set org.gnome.desktop.interface cursor-theme "$cursor_theme"
        ;;
esac

marco_theme=$(gsettings get org.mate.Marco.general theme | cut -d"'" -f2)
if [ ! -e "${HOME}/.themes/${marco_theme}/metacity-1/metacity-theme-1.xml" ] && \
   [ ! -e "${HOME}/.local/share/${marco_theme}/metacity-1/metacity-theme-1.xml" ] && \
   [ ! -e "/usr/share/themes/${marco_theme}/metacity-1/metacity-theme-1.xml" ]; then
    # Migrate from missing Metacity theme
    case ${marco_theme} in
        "$ambiant"|"$ambiant_dark") gsettings set org.mate.Marco.general theme "$yaru_dark";;
        *) gsettings set org.mate.Marco.general theme "$yaru_light";;
    esac
fi

# Migrate from Ambiant/Radiant/Yaru-MATE GtkSourceView styles
case $(gsettings get org.mate.pluma color-scheme) in
  "$ambiant"|"$ambiant_dark"|"$yaru_mate_dark") gsettings set org.mate.pluma color-scheme "$yaru_dark";;
  "$radiant"|"$yaru_mate_light") gsettings set org.mate.pluma color-scheme "$yaru_light";;
esac

# If the theme is "-dark", set the GNOME 42 dark preference
case $(gsettings get org.mate.interface gtk-theme) in
  *-dark\') gsettings set org.gnome.desktop.interface color-scheme "$color_scheme_dark";;
esac

# Migrate Firefox from deb to snap
# https://bazaar.launchpad.net/~mozillateam/firefox/firefox.jammy/view/head:/firefox
if [ -x /snap/bin/firefox ]; then
    # Update default browser
    if [ "$(xdg-settings get default-web-browser)" = "firefox.desktop" ]; then
        xdg-settings set default-web-browser firefox_firefox.desktop
    fi

    # Migrate mate-panel launchers
    OLD="/usr/share/applications/firefox.desktop"
    NEW="/var/lib/snapd/desktop/applications/firefox_firefox.desktop"
    OBJECTS=$(gsettings get org.mate.panel object-id-list 2> /dev/null)
    for object in $OBJECTS; do
        object=$(echo $object | cut -d\' -f2)
        launcher=$(gsettings get org.mate.panel.object:/org/mate/panel/objects/$object/ launcher-location)
        if [ "$launcher" = "'$OLD'" ]; then
            gsettings set org.mate.panel.object:/org/mate/panel/objects/$object/ launcher-location "'$NEW'"
        fi
    done

    # Migrate plank dockitems
    if [ -e ~/.config/plank/dock1/launchers/firefox.dockitem ]; then
        rm ~/.config/plank/dock1/launchers/firefox.dockitem
        overlay_file config/plank/dock1/launchers/firefox_firefox.dockitem
    fi
fi
