############################################################################
#
# Copyright (c) 2012 - dsixda (dislam@rocketmail.com)
#
# Android Kitchen is 100% free.  This script file is intended for personal
# and/or educational use only.  It may not be duplicated for monetary
# benefit or any other purpose without the permission of the developer.
#
############################################################################

clear

current_ver=$1

echo 
echo "============================================================"
echo " Android Kitchen $current_ver - by dsixda (xda-developers.com)"
echo "============================================================"
echo
echo "About the Kitchen"
echo
echo "------------------------------------------------------------"
echo
echo "  For help and discussion : http://bit.ly/aLKQaR"
echo "  Follow dsixda on Twitter: http://www.twitter.com/dsixda"
echo "  For Paypal donations    : http://bit.ly/blHTKy"
echo
echo "------------------------------------------------------------"
echo
echo -n "Check online for updates (y/n)? (default: y): "
read do_update

if [ "$do_update" == "n" ]
then
  exit 0
fi

echo 
echo "Searching for updates ..."

notes=`scripts/get_latest_notes`
latest_ver=`echo $notes | sed -e 's/.*Version[ ]*\([0-9]*.[0-9]*\) (.*/\1/g' -e 's/[ ]*//g'`

if [ "$latest_ver" == "" ]
then
  echo "Error: Unable to find update"

else

  curr_major=`echo $current_ver | sed 's/\([0-9]*\).[0-9]*/\1/'`
  curr_minor=`echo $current_ver | sed 's/[0-9]*.//'`

  latest_major=`echo $latest_ver | sed 's/\([0-9]*\).[0-9]*/\1/'`
  latest_minor=`echo $latest_ver | sed 's/[0-9]*.//'`

  update=1

  #
  # Compare our version with version online
  #
  if [ $curr_major -gt $latest_major ]
  then
    update=0
  else

    if [ $curr_major -eq $latest_major ]
    then
      if [ $curr_minor -ge $latest_minor ]
      then
        update=0
      fi
    fi
  fi

  
  if [ $update == 0 ] 
  then
    echo "Latest version online is $latest_ver.  No update required."
  else

    echo "An update was found ($latest_ver)."
    echo
    echo "Release notes are found online."

    # xda-developers updates the formatting of the page, so this routine would need to
    # updated every time.  Comment out for now.
    #echo "$notes"

    echo 
    echo -n "Download (y/n)? (default: y): "
    read proceed_download
    if [ "$proceed_download" == "n" ]
    then
      exit 0
    fi

    new_file=dsixda_Android_Kitchen_$latest_ver.zip

    echo
    echo "Downloading $new_file ..."
    echo

    url=https://github.com/dsixda/Android-Kitchen/archive/$latest_ver.zip

    wget --no-check-certificate $url -O $new_file
    echo

    #Not supported on Mac?
    #zip_size=`stat -c %s $new_file`

    ls_size=`ls -l $new_file`
    grep_empty=`echo $ls_size | grep -c " 0 "`
    
    if [ -e $new_file ] && [ $grep_empty == 0 ]
    then
      echo "$new_file successfully downloaded"
      echo "You must unzip this file to use the new kitchen"
    else
      echo "Error: $new_file was not downloaded"
      rm -f $new_file
    fi
  fi
fi

if [ -e $check_filename ]
then
  rm -f $check_filename
fi

echo
echo "Press Enter to continue"
read enterKey

