#!/bin/bash

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

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

if [ -z "$(dpkg-query -W --showformat='${Status}\n' yad 2>/dev/null | grep "install ok installed")" ];then
  zenity --question --title='Pi-Apps' --window-icon="${DIRECTORY}/icons/logo.png" \
	  --text="YAD is required but not installed. Install now?" \
	  --ok-label=Yes --cancel-label=No --ellipsize --icon-name='' \
	  2>/dev/null || error "User declined."


  lxterminal --title="Installing YAD" -e "sudo apt install -y yad;echo -e '\nClosing in 5 seconds.';sleep 5"
fi

#remove auto-created yad icon browser launcher
sudo rm /usr/share/applications/yad-icon-browser.desktop

echo "Creating menu button..."
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

echo "Adding Desktop shortcut..."
cp -f ${HOME}/.local/share/applications/pi-apps.desktop ${HOME}/Desktop/pi-apps.desktop

echo "Creating Settings menu button..."
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

echo "Installation complete."