#!/bin/bash

function error {
  echo -e "\e[91m$1\e[39m"
  exit 1
}

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

#install yad automatically
if ! command -v yad >/dev/null;then
  if [ -f /usr/bin/apt ];then
    sudo apt update
    sudo apt install -y yad
    updated=1
  elif [ -f /usr/bin/pacman ];then
    sudo pacman -S yad
  else
    error "Failed to find any package manager to install yad."
  fi
fi
#remove annoying YAD icon browser launcher
sudo rm -f /usr/share/applications/yad-icon-browser.desktop

#install git
if ! command -v git >/dev/null;then
  if [ -f /usr/bin/apt ];then
    #only run sudo apt update if not already run
    if [ -z "$updated" ];then
      sudo apt update
    fi
    sudo apt install -y git
  else
    error "Failed to find any package manager to install git."
  fi
fi

#download pi-apps if folder missing
if [ -z "$DIRECTORY" ] || [ "$DIRECTORY" == "$HOME" ] || [ "$DIRECTORY" == bash ];then
  DIRECTORY="$HOME/pi-apps"
  
  #if directory does not exist, download it
  if [ ! -d "$DIRECTORY" ];then
    echo "Downloading Pi-Apps..."
    output="$(git clone --depth 1 https://github.com/Botspot/pi-apps "$DIRECTORY" 2>&1)"
    if [ ! -d "$DIRECTORY" ];then
      error "Pi-Apps download failed!\ngit clone output was: $output"
    fi
  fi
fi
#from this point in, DIRECTORY variable populated with valid pi-apps directory

#menu button
if [ ! -f ~/.local/share/applications/pi-apps.desktop ];then
  echo "Creating menu button..."
fi
mkdir -p ~/.local/share/applications
echo "[Desktop Entry]
Name=Pi Apps
Comment=Raspberry Pi App Store for open source projects
Exec=${DIRECTORY}/gui
Icon=${DIRECTORY}/icons/logo.png
Terminal=false
Type=Application
Categories=Utility;" > ~/.local/share/applications/pi-apps.desktop

if [ ! -f ~/Desktop/pi-apps.desktop ];then
  echo "Adding Desktop shortcut..."
fi
cp -f ~/.local/share/applications/pi-apps.desktop ~/Desktop/pi-apps.desktop
chmod +x ~/Desktop/pi-apps.desktop

if [ ! -f ~/.local/share/applications/pi-apps-settings.desktop ];then
  echo "Creating Settings menu button..."
fi
echo "[Desktop Entry]
Name=Pi Apps Settings
Comment=Configure Pi-Apps or create an App
Exec=${DIRECTORY}/settings
Icon=${DIRECTORY}/icons/logo.png
Terminal=false
Type=Application
Categories=Settings;" > ~/.local/share/applications/pi-apps-settings.desktop

if [ ! -f ~/.config/autostart/pi-apps-updater.desktop ];then
  echo "Creating autostarted updater..."
fi
mkdir -p ~/.config/autostart
echo "[Desktop Entry]
Name=Pi Apps Updater
Exec=${DIRECTORY}/updater onboot
Icon=${DIRECTORY}/icons/logo.png
Terminal=false
Type=Application
X-GNOME-Autostart-enabled=true
Hidden=false
NoDisplay=false" > ~/.config/autostart/pi-apps-updater.desktop

mkdir -p "${DIRECTORY}/data" && cd "${DIRECTORY}/data" || error "Failed to make and enter ${DIRECTORY}/data directory!"
mkdir -p installed-packages preload settings status update-status categories
cd $HOME

#hide template file by default
"${DIRECTORY}/etc/categoryedit" "template" 'hidden' >/dev/null

#hide duplicates if running in twisteros
if [ -f /usr/local/bin/twistver ];then
  PREIFS="$IFS"
  IFS=$'\n'
  apps="BalenaEtcher
Chromium Media Edition
CommanderPi
Box86
Discord
piKiss
Retropie
Scrcpy
Steam
Windows 10 Theme
Chromium Widevine
Back to Chromium v78
Lightpad"
  for app in $apps ;do
    "${DIRECTORY}/etc/categoryedit" "$app" 'hidden' >/dev/null
  done
  echo "Finished hiding apps on TwisterOS."
fi

#pi-apps terminal command
if [ ! -f /usr/local/bin/pi-apps ] || ! grep -q /usr/local/bin/pi-apps "${DIRECTORY}/gui";then
  rm /usr/local/bin/pi-apps
  echo "#!/bin/bash
${DIRECTORY}/gui" | sudo tee /usr/local/bin/pi-apps >/dev/null
  sudo chmod +x /usr/local/bin/pi-apps
  echo "You can now run pi-apps in a terminal, just by running 'pi-apps'"
fi

if [ "$(ls "$DIRECTORY/data/settings" 2>/dev/null | wc -l)" -le 2 ];then
  echo "Generating default settings..."
fi
"${DIRECTORY}/settings" refresh

if [ ! -f "$DIRECTORY/data/preload/LIST-" ];then
  echo "Preloading app list..."
fi
"${DIRECTORY}/preload" &>/dev/null

echo "Installation complete."

