#!/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 --title='Pi-Apps' --window-icon="${DIRECTORY}/icons/logo.png" \
	  --list --text="YAD is required but not installed.   \n Install now?" \
	  --ok-label=Yes --cancel-label=No \
	  --column=foo --hide-header 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

echo "Creating menu button..."
echo -n '' > ${HOME}/.local/share/applications/pi-apps.desktop
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;" >> ${HOME}/.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 "Installation complete."