#!/bin/bash

DIRECTORY="$(dirname "$(dirname "$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )")")"

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

#get java
if [ -f "${DIRECTORY}/pkg-install" ];then
  "${DIRECTORY}/pkg-install" "openjdk-11-jdk" "$(dirname "$0")" || exit 1
else
  apt install -y openjdk-11-jdk || sudo apt install -y openjdk-11-jdk || error "Failed to install openjdk-11-jdk."
fi

rm -rf ~/jgrasp.zip ~/jgrasp

wget 'https://www.jgrasp.org/dl4g/jgrasp/jgrasp206_07.zip' -O ~/jgrasp.zip || error "Failed to download jgrasp.zip!"
unzip ~/jgrasp.zip || error "Failed to extract jgrasp.zip!"

echo "Running jgrasp's configure script..."
~/jgrasp/src/configure || error "jgrasp's configure script failed!"

echo "[Desktop Entry]
Encoding=UTF-8
Version=1.0
Name=jGRASP
GenericName=IDE
Exec=$HOME/jgrasp/bin/jgrasp %F
Terminal=false
Icon=$([ -f "$(dirname "$0")/icon-64.png" ] && echo "$(dirname "$0")/icon-64.png" || echo "$HOME/jgrasp/data/gric48.png")
Type=Application
Categories=Application;Development;IDE;
Comment=jGRASP IDE
StartupNotify=true
Keywords=Text;Editor;" > "$([ -e "$HOME/.local/share/applications" ] && echo "$HOME/.local/share/applications || echo /usr/share/applications)/jgrasp.desktop"


