#!/bin/sh
#
##################################################
# Chromeium for mac, daily build updater
##################################################
# Author: ajer001@twntwn.info
# Website: http://twntwn.info/blog/ajer001
# Version: 2011-12-07
##################################################

#
CurrentVersion=$(cat ~/.chromium_updater)

# check latest version
BaseUrl="http://commondatastorage.googleapis.com/chromium-browser-continuous/Mac"
LatestVersion=$(curl -s $BaseUrl/LAST_CHANGE)

# setting the download link
Link="$BaseUrl/$LatestVersion/chrome-mac.zip"

# Remove old temp file
if (test -f ~/chrome-mac.zip)||(test -f ~/chrome-mac.part1); then
    echo "Removing the old files..."
    rm ~/chrome-mac.*
fi

# Get file size
declare -i Length=`curl -s -L -I $Link|grep Content-Length|tail -n 1|sed s/[^0-9]//g`
echo "Upgrading $CurrentVersion to $LatestVersion, please wait..."

# 
echo "Downloading ($((Length/1024))kB), please wait..."

declare -i PartIdx=0
declare -i PartNum=5
declare -i PartSize=$((Length/PartNum))
for ((i=0; i < $PartNum-1; i++))
do
  echo "Now downloading $((i+1))/$PartNum"
  curl -s -L -r "$PartIdx-$(($PartIdx + $PartSize - 1))" -o "chrome-mac.part$((i+1))" "$Link"
  PartIdx=$(($PartIdx + $PartSize))
done

#echo "Now downloading $((i+1))/$PartNum"
curl -s -L -r "$PartIdx-$(($Length - 1))" -o "chrome-mac.part$((i+1))" "$Link"

cat chrome-mac.part* > chrome-mac.zip

# unzip and install the latest version
unzip -q chrome-mac.zip
cp -r chrome-mac/Chromium.app /Applications/

# remove the temporary files
rm -r chrome-mac chrome-mac.part* chrome-mac.zip

#
echo "$LatestVersion">~/.chromium_updater

echo "Update completed :) \n"

# done :)

