#!/bin/sh

echo "checking internet connection.."
if [ ! "$(ping -c 1 vectorlinux.osuosl.org)" ];then echo "site not found, are you connected to the Internet?";sleep 5;exit;fi

# find current base version
for i in alpha beta Alpha Beta ALPHA BETA rc RC "release candidate" "Release Candidate" "RELEASE CANDIDATE";do
if [ "$(grep "$i" /etc/vector-version)" ];then VER="$(grep "$i" /etc/vector-version | grep "$i [0-9]*\.*[0-9]*" -o)";fi
done

# define update file name
UPDFILE="vl7-$(echo $VER | sed 's/ //')-current.tar.gz"

# find current update version
UPD="$(cat /etc/vector-version | grep "Update [0-9]*" -o | cut -d " " -f2)"
if [ -z "$UPD" ];then
  UPD=0
  echo "Your system has not been updated previously"
  else
    echo Your existing version is $VER Update $UPD
fi

# download the changelog and look for update, if new is available, download and install it 
echo "looking for updates.."
NEWUPD="$(curl http://vectorlinux.osuosl.org/vec7/live-updates/changelog.txt 2>/dev/null | grep "$VER:" | grep "Update [0-9]*" -o | grep [0-9]* -o)"
if [ "$NEWUPD" -gt "$UPD" ];then
 echo "downloading update.."
 wget -P /var/log/setup/tmp/ http://vectorlinux.osuosl.org/vec7/live-updates/$UPDFILE
 if [ -e "/var/log/setup/tmp/$UPDFILE" ];then
  echo "installing update.."
  tar -zxvf /var/log/setup/tmp/$UPDFILE -C /
  sh /var/log/setup/tmp/do_upgrade.sh
  rm /var/log/setup/tmp/do_upgrade.sh
  rm /var/log/setup/tmp/$UPDFILE
  echo Done
  sleep 5
  else
   zenity --info --title=Update --text="Unable to download update!"
 fi
 else
  zenity --info --title=Update --text="No updates found."
fi
