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

if [ ! -f /usr/local/bin/box86 ];then
  echo 'Installing box86 first...'
  wget -qO- https://raw.githubusercontent.com/Botspot/box86-updater/main/update-box86 | bash
  if [ ! -f /usr/local/bin/box86 ];then
    error "Box86 failed to install somehow!"
  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!"

