#!/bin/bash

mkdir -p /etc/root 2> /dev/null
rootperm=$?
if [ $rootperm -eq 0 ] ; then
  rm -rf /etc/root
else
  echo "Root permission is required to run this script"
  exit
fi

if [ ! -f /usr/bin/apt ] ; then
  echo "This script is only for Debian-based systems"
  exit
fi

echo "Checking tabby version"
touch /etc/tabby_version.conf
version_tabby=$(curl "https://github.com/Eugeny/tabby/releases" 2> /dev/null | grep "releases/tag" | head -1 | cut -d "=" -f 4 | cut -d "/" -f 6 | cut -d '"' -f 1 | cut -d "v" -f 2)
version_tabby_current=$(cat /etc/tabby_version.conf)
if [ "${version_tabby}" != "${version_tabby_current}" ] ; then
  echo "New tabby version detected"
  echo "Downloading tabby"
  rm -rf /tmp/tabby.deb
  wget -q --show-progress -c "https://github.com/Eugeny/tabby/releases/download/v${version_tabby}/tabby-${version_tabby}-linux-x64.deb" -O /tmp/tabby.deb
  echo "Installing tabby"
  apt install /tmp/tabby.deb
  error_install=$?
  if [ ${error_install} -eq 0 ] ; then
    echo "${version_tabby}" > /etc/tabby_version.conf
    sed -i 's/Icon\=tabby/Icon\=terminal/g' "/usr/share/applications/tabby.desktop"
  fi
else
  echo "No updates for tabby"
fi
