#!/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
}

#install box86
if ! command -v box86 || [ "$(cat "${DIRECTORY}/data/status/Box86" 2>/dev/null)" != 'installed' ];then
  echo 'Installing box86...'
  "${DIRECTORY}/manage" install Box86 || error "Box86 failed to install somehow!"
  if ! command -v box86 ;then
    error "Box86 executable does not exist!"
  else
    echo "installed" > "${DIRECTORY}/data/status/Box86"
  fi
fi

wget https://download.sublimetext.com/Sublime%20Text%202.0.2.tar.bz2 -O ~/sublime-text-2.tar.bz2 || error 'Failed to download!'
tar -xf ~/sublime-text-2.tar.bz2 || error 'Failed to extract!'
rm -f sublime-text-2.tar.bz2

echo "[Desktop Entry]
Name=Sublime Text
Comment=nice text editor that has native support for most programming languages.
Exec='$HOME/Sublime Text 2/sublime_text'
Icon=$(dirname "$0")/icon-64.png
Terminal=false
StartupNotify=true
Type=Application
Categories=Utility;" > ~/.local/share/applications/sublime-text.desktop || error "Failed to create menu button!"

