#!/bin/bash

# Print the logo
print_logo() {
    cat << "EOF"

                                                                                                     
    ██                                                                                               
  █   ██                                                                                             
    ██    ██████    █████         ██    █████     █████     ████    ██        █████         ██       
         ██    ██  ██   ███  ███████   ██   ██   ██   ██       ██  ████████  ██    ██  ███████       
         █████     ██████   ██    ██   ██████    ██      ████████   ██   █   ███████   ██   ██       
         ██        ██████    ███████   ██████    ██       ██████    ██████   ██████    ███████       
                                                                                                     

EOF
}

# Clear screen and show logo
clear
print_logo

COREDESKTOP="$HOME/.local/share/coredesktop"

echo "Updating CoreDesktop..."
git -C "$COREDESKTOP" pull
mkdir -p "$COREDESKTOP/updates.d"
sudo cp "$COREDESKTOP/coreupdate" ~/.local/bin/coreupdate
sudo cp "$COREDESKTOP/corerefresh" ~/.local/bin/corerefresh

if [[ -z "$COREUPDATE_REEXECED" ]]; then
    export COREUPDATE_REEXECED=1
    exec ~/.local/bin/coreupdate
fi

for plugin in "$COREDESKTOP/updates.d/"*.sh; do
    if [ -f "$plugin" ]; then
        unset -f is_installed run_update
        source "$plugin"
        if declare -f run_update > /dev/null; then
            if ! declare -f is_installed > /dev/null || is_installed; then
                run_update
            fi
        fi
    fi
done

echo "Done."
