# encoding: ascii
# api: powershell
# title: Google Chromium Download
# description: Download and install Google Chromium if there is a newer version available.
# version: 0.1
# type: script
# author: MJWJ1
# license: CC0
# x-poshcode-id: 2747
# x-archived: 2013-07-18T22:27:29
# x-published: 2013-06-19T18:05:00
#
#
<#
    .Synopsis
        Download Google Chromium if there is a later build.
    .Description
        Download Google Chromium if there is a later build.        
        http://poshcode.org/2422
#>

Set-StrictMode -Version Latest
Import-Module bitstransfer

# comment out when not debugging
$VerbosePreference = "Continue"
#$VerbosePreference = "SilentlyContinue"

$versionFile = "$Env:temp\latestChromiumVersion.txt"
$installedChromiumVersion = 0

trap [Exception] { 
      write-host
      write-error $("TRAPPED: " + $_.Exception.GetType().FullName); 
      write-error $("TRAPPED: " + $_.Exception.Message); 
      [string]($installedChromiumVersion) > $versionFile
      exit; 
}


if (Test-Path $versionFile)
{ $installedChromiumVersion = [int32] (cat $versionFile) }

#$latestChromiumBuildURL ="http://build.chromium.org/f/chromium/snapshots/chromium-rel-xp"
$latestChromiumBuildURL ="http://build.chromium.org/f/chromium/snapshots/Win"
Start-BitsTransfer "$latestChromiumBuildURL/LATEST" $versionFile
$latestChromiumVersion = [int32] (cat $versionFile)

if ($installedChromiumVersion -eq $latestChromiumVersion)
{ 
    Write-Verbose "Exiting... Version $installedChromiumVersion is the latest."
    return
}

$installerAppName = "mini_installer"
$installer = "$Env:temp\$installerAppName.exe"
Write-Verbose "Initiating download of new version $latestChromiumVersion"
Start-BitsTransfer "$latestChromiumBuildURL/$latestChromiumVersion/mini_installer.exe" $installer
Write-Verbose "Installing new version of Chromium"
Invoke-Item $installer
$installerRunning = 1
while (!($installerRunning -eq $null))
{ 
    sleep 5
    $installerRunning = ( Get-Process | ? {$_.ProcessName -match "$installerAppName"} )
}

Write-Verbose "New Chromium Installed! Please restart the Chromium browser"
