#!/bin/bash
# Get dependencies
install_packages libsquashfuse0 squashfuse fuse || exit 1
sudo apt install -y snapd || error "APT failed to install snapd"

sudo systemctl enable --now snapd.socket || error "Unable to enable snapd.socket"
sudo systemctl start snapd.service
if [ $? != 0 ];then
  echo "Unable to start snapd.service. Running debug..."
  echo "Running 'systemctl status snapd.service'"
  systemctl status snapd.service
  sudo /usr/lib/snapd/snapd
  exit 1
fi

sudo snap refresh
sudo systemctl restart snapd.service || error "Unable to restart snapd.service"
enable_module squashfs || exit 1

# install the core snap which obtains the latest snapd
sudo snap install core || error "Snapd was unable to install core"

echo "Output of 'snap version':"
snap version
sudo snap install snap-store || error "Unable to install snap store"

# correct orphaned directory from older script 
sudo rm -rf /usr/share/applications/snap

#Pi-Apps tries to avoid unnecessary reboots at all cost. Snap places desktop launchers in /var/lib/snapd/desktop/applications, which is not searched by default.
#This path is added to $XDG_DATA_DIRS on the next reboot, but we don't want to wait for that!
#If there are files in /var/lib/snapd/desktop/applications, and XDG_DATA_DIRS is missing snap paths, then bind-mount to /usr/share/applications
if [[ "$XDG_DATA_DIRS" != */var/lib/snapd/desktop* ]] && [ ! -z "$(ls /var/lib/snapd/desktop/applications)" ] && [ -z "$(ls /usr/share/applications/snapd-temporary)" ];then
  sudo mkdir -p /usr/share/applications/snapd-temporary
  sudo mount --bind /var/lib/snapd/desktop/applications /usr/share/applications/snapd-temporary
elif [[ "$XDG_DATA_DIRS" == */var/lib/snapd/desktop* ]] ;then
  sudo rm -rf /usr/share/applications/snapd-temporary
fi
#Additionally, PiOS Buster had a bug where XDG_DATA_DIRS was missing snap's entries due to PiOS mods. Pi-Apps fixes this with a runonce.
