#!/usr/bin/env bash

ARCH=$(uname -m)
if [ "${ARCH}" != "aarch64" ]; then
  echo "This script may only be run on a Ubuntu Pi image"
  exit
fi

snap download core20 --channel=latest/stable
snap download ubuntu-budgie-welcome --channel=latest/stable/ubuntu-23.04
mv core20*.assert /var/lib/snapd/seed/assertions/
mv core20*.snap /var/lib/snapd/seed/snaps/
mv ubuntu-budgie-welcome*.assert /var/lib/snapd/seed/assertions/
mv ubuntu-budgie-welcome*.snap /var/lib/snapd/seed/snaps/
rm /var/lib/snapd/seed/snaps/snap-store*.snap

rm /var/lib/snapd/seed/assertions/snap-store*

rm /snap/bin/*
rm -rf /snap/snap-store

rm /var/lib/snapd/apparmor/profiles/*
rm /var/lib/snapd/desktop/applications/*
rm /var/lib/snapd/inhibit/snap-store.lock

rm /var/lib/snapd/mount/*
rm -rf /var/lib/snapd/snaps/*
rm -rf /var/snap/snap-store

rm /etc/systemd/system/*.mount

WELCOME=$(ls /var/lib/snapd/seed/snaps/ubuntu-budgie-welcome* | sed 's#.*/##')
CORE22=$(ls /var/lib/snapd/seed/snaps/core22* | sed 's#.*/##')
CORE20=$(ls /var/lib/snapd/seed/snaps/core20* | sed 's#.*/##')
SNAPD=$(ls /var/lib/snapd/seed/snaps/snapd_* | sed 's#.*/##')
FIREFOX=$(ls /var/lib/snapd/seed/snaps/firefox* | sed 's#.*/##')
GTKCOMMON=$(ls /var/lib/snapd/seed/snaps/gtk-common-themes* | sed 's#.*/##')
GNOME=$(ls /var/lib/snapd/seed/snaps/gnome-* | sed 's#.*/##')
BARE=$(ls /var/lib/snapd/seed/snaps/bare* | sed 's#.*/##')
DESKTOPINT=$(ls /var/lib/snapd/seed/snaps/snapd-desktop-integration_* | sed 's#.*/##')

sed -i "/file: ubuntu-budgie-welcome_/c\    file: $WELCOME" /var/lib/snapd/seed/seed.yaml
sed -i "/file: core20_/c\    file: $CORE20" /var/lib/snapd/seed/seed.yaml
sed -i "/file: core22_/c\    file: $CORE22" /var/lib/snapd/seed/seed.yaml
sed -i "/file: snapd_/c\    file: $SNAPD" /var/lib/snapd/seed/seed.yaml
sed -i "/file: gnome-/c\    file: $GNOME" /var/lib/snapd/seed/seed.yaml
sed -i "/file: gtk-common-themes/c\    file: $GTKCOMMON" /var/lib/snapd/seed/seed.yaml
sed -i "/file: firefox_/c\    file: $FIREFOX" /var/lib/snapd/seed/seed.yaml
sed -i "/file: bare_/c\    file: $BARE" /var/lib/snapd/seed/seed.yaml
sed -i "/file: snapd-desktop-integration_/c\    file: $DESKTOPINT" /var/lib/snapd/seed/seed.yaml

rm /var/lib/snapd/state.json
sed -i 's/After=snapd.service snapd.socket/After=snapd.service/' /lib/systemd/system/snapd.seeded.service

apt update

# apt-add-repository -y -p proposed
# apt install -y linux-raspi
# apt-add-repository -y -r -p proposed

# apt purge -y linux-headers-5.13.0-1008-raspi linux-image-5.13.0-1008-raspi

apt remove -y gdm3 gnome-shell

apt remove -y gnome-bluetooth-sendto ibus ibus-gtk ibus-gtk3 ibus-table gnome-initial-setup ubuntu-report systemd-oomd eog \
gnome-control-center system-config-printer gnome-terminal gnome-text-editor gnome-font-viewer nautilus xdg-desktop-portal-gnome evince gnome-system-monitor \
gnome-calculator apt-config-icons-hidpi gamemode seahorse yaru-theme-gnome-shell yaru-theme-gtk yaru-theme-icon yaru-theme-sound \
ubuntu-wallpapers gnome-session-canberra plymouth-theme-spinner ubuntu-settings gsettings-ubuntu-schemas \
gstreamer1.0-plugins-base-apps xcursor-themes libpam-fprintd libpam-sss rhythmbox cheese totem remmina shotwell \
gnome-calendar branding-ubuntu libreoffice-style-breeze evince-common fprintd gamemode-daemon gir1.2-rb-3.0 \
gir1.2-totem-1.0 gnome-bluetooth-3-common gnome-control-center-data gnome-terminal-data gnome-video-effects \
grilo-plugins-0.3-base gstreamer1.0-tools ldap-utils libavahi-ui-gtk3-0 libbasicobjects0 libc-ares2 libcollection4 \
libcolord-gtk4-1 libcue2 libdhash1 libdmapsharing-3.0-2 libedataserverui4-1.0-0 libevdocument3-4 libevview3-3

apt remove -y libfprint-2-2 libfreerdp-client2-2 libgamemode0 libgamemodeauto0 libgdm1 libgexiv2-2 libgif7 libgnome-autoar-0-0 \
libgnome-bg-4-2 libgnome-bluetooth-3.0-13 libgnome-bluetooth-ui-3.0-13 libgnome-rr-4-2 libgom-1.0-0 libgrilo-0.3-0 \
libgtksourceview-5-0 libgtksourceview-5-common libini-config5 libipa-hbac0 liblirc-client0 \
libnss-sss libpam-pwquality libpath-utils1 libportal-gtk3-1 libportal-gtk4-1 libportal1 libref-array1 \
librhythmbox-core10 libsasl2-modules-gssapi-mit libsss-certmap0 libsss-idmap0 libsss-nss-idmap0 libsysmetrics1 libtotem0 \
libtracker-sparql-3.0-0 libvncclient1 libxcb-icccm4 libxcb-image0 libxcb-keysyms1 libxcb-render-util0 \
libxcb-xv0 media-player-info python3-mako python3-markupsafe python3-sss remmina-common rhythmbox-data shotwell-common \
totem-common tracker tracker-extract tracker-miner-fs ubiquity-slideshow-ubuntu ubuntu-wallpapers-lunar ubuntu-wallpapers \
xserver-xephyr xserver-xorg-input-all xserver-xorg-input-wacom

apt install -y ubuntu-budgie-desktop-raspi

sudo dpkg -i /tmp/*.deb

DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true apt upgrade -y

apt-get -y install --no-install-recommends oem-config-slideshow-ubuntu-budgie

sed -i 's/oem-config-slideshow-ubuntu/oem-config-slideshow-ubuntu-budgie/' /usr/lib/ubiquity/plugins/ubi-usersetup.py
sed -i 's/oem-config-slideshow-ubuntu/oem-config-slideshow-ubuntu-budgie/' /usr/sbin/oem-config-remove-gtk
sed -i 's/ubiquity-slideshow-ubuntu/ubiquity-slideshow-ubuntu-budgie/' /usr/sbin/oem-config-remove-gtk

echo "/usr/sbin/lightdm" > /etc/X11/default-display-manager
DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true dpkg-reconfigure lightdm
echo set shared/default-x-display-manager lightdm | debconf-communicate
update-alternatives --install /usr/share/plymouth/themes/default.plymouth default.plymouth /usr/share/plymouth/themes/ubuntu-budgie-logo/ubuntu-budgie-logo.plymouth 100
update-alternatives --set default.plymouth /usr/share/plymouth/themes/ubuntu-budgie-logo/ubuntu-budgie-logo.plymouth
update-initramfs -u

apt -y autoremove
apt -y autoclean
apt -y clean

rm /usr/bin/setup-budgie.sh
echo -n "" > /etc/resolv.conf

