#    Script for installing the required dependencied for 
#    the ISMRM Raw Data Format on Windows.
#    
#    Prerequisites:
#        - Windows 7 (64-bit)
#        - Visual Studio (C/C++) installed
#
function download_file($url,$destination) {
    #Let's set up a webclient for all the files we have to download
    $client = New-Object System.Net.WebClient
    $client.DownloadFile($url,$destination)
}
function unzip($zipPath, $destination){
    $shell = new-object -com shell.application;
    $zip = $shell.NameSpace($zipPath);
    if ((Test-Path -path $destination) -ne $True)
    {
        New-Item $destination -type directory
    }
    foreach($item in $zip.items()){
        $shell.Namespace($destination).copyhere($item)
    }
}
function Set-VS-Environment () {
    $file = (Get-ChildItem Env:VS100COMNTOOLS).Value + "vsvars32.bat"
    $cmd = "`"$file`" & set"
    cmd /c $cmd | Foreach-Object {
       $p, $v = $_.split('=')
       Set-Item -path env:$p -value $v
   }
}
function add_path($pathname) {
    if ($env:path  -match [regex]::escape($pathname)) {
        Write-Host "Path $path already exists"
    } else {
        setx PATH "$env:path;$pathname" -m
    }
}
Write-Host "ISMRMRD Raw Data Format Dependencies Installation"
$library_location = "C:\MRILibraries"
$download_location = "C:\MRILibraries\downloads"

#Let's first check if we have the library folder and if not create it
if ((Test-Path -path $library_location) -ne $True)
{
    Write-Host "Library location: " $library_location " not found, creating"
    New-Item $library_location -type directory
}
else
{
    Write-Host "Library location: " $library_location " found."
}
#Now check if we have the library folder and if not create it
if ((Test-Path -path $download_location) -ne $True)
{
    Write-Host "Download location: " $download_location " not found, creating"
    New-Item $download_location -type directory
}
else
{
    Write-Host "Download location: " $download_location " found."
}
#Download and install CMAKE
download_file "http://www.cmake.org/files/v2.8/cmake-2.8.9-win32-x86.exe" ($download_location + "\cmake-2.8.9-win32-x86.exe")
& ($download_location + "\cmake-2.8.9-win32-x86.exe")
#Download and install Git
download_file "http://msysgit.googlecode.com/files/Git-1.7.11-preview20120710.exe" ($download_location + "\Git-1.7.11-preview20120710.exe")
& ($download_location + "\Git-1.7.11-preview20120710.exe")
#Download, unzip, and install HDF5
download_file "http://www.hdfgroup.org/ftp/HDF5/current/bin/windows/HDF5189-win64-vs10-shared.zip" ($download_location + "\HDF5189-win64-vs10-shared.zip")
unzip ($download_location + "\HDF5189-win64-vs10-shared.zip")  "$download_location\hdf5_binaries"
& "$download_location\hdf5_binaries\HDF5-1.8.9-win64.exe"
#Download, install HDFView
download_file "http://www.hdfgroup.org/ftp/HDF5/hdf-java/hdfview/hdfview_install_win64.exe" ($download_location + "\hdfview_install_win64.exe")
& ($download_location + "\hdfview_install_win64.exe")

⌀䐀漀眀渀氀漀愀搀 愀渀搀 椀渀猀琀愀氀氀 䌀漀搀攀匀礀渀琀栀攀猀椀猀 堀匀䐀ഀ搀漀眀渀氀漀愀搀开昀椀氀攀 ∀栀琀琀瀀㨀⼀⼀眀眀眀⸀挀漀搀攀猀礀渀琀栀攀猀椀猀⸀挀漀洀⼀搀漀眀渀氀漀愀搀⼀砀猀搀⼀㌀⸀㌀⼀眀椀渀搀漀眀猀⼀椀㘀㠀㘀⼀砀猀搀ⴀ㌀⸀㌀⸀洀猀椀∀ ⠀␀搀漀眀渀氀漀愀搀开氀漀挀愀琀椀漀渀 ⬀ ∀尀砀猀搀ⴀ㌀⸀㌀⸀洀猀椀∀⤀ഀ☀ ⠀␀搀漀眀渀氀漀愀搀开氀漀挀愀琀椀漀渀 ⬀ ∀尀砀猀搀ⴀ㌀⸀㌀⸀洀猀椀∀⤀ഀ਀⌀䐀漀眀渀氀漀愀搀 愀渀搀 椀渀猀琀愀氀氀 戀漀漀猀琀ഀ搀漀眀渀氀漀愀搀开昀椀氀攀 ∀栀琀琀瀀㨀⼀⼀戀漀漀猀琀瀀爀漀⸀挀漀洀⼀搀漀眀渀氀漀愀搀⼀砀㘀㐀⼀戀漀漀猀琀开㄀开㔀㄀开猀攀琀甀瀀⸀攀砀攀∀ ⠀␀搀漀眀渀氀漀愀搀开氀漀挀愀琀椀漀渀 ⬀ ∀尀戀漀漀猀琀开㄀开㔀㄀开猀攀琀甀瀀⸀攀砀攀∀⤀ഀ☀ ⠀␀搀漀眀渀氀漀愀搀开氀漀挀愀琀椀漀渀 ⬀ ∀尀戀漀漀猀琀开㄀开㔀㄀开猀攀琀甀瀀⸀攀砀攀∀⤀ഀ਀⌀䘀䘀吀圀ഀ搀漀眀渀氀漀愀搀开昀椀氀攀 ∀昀琀瀀㨀⼀⼀昀琀瀀⸀昀昀琀眀⸀漀爀最⼀瀀甀戀⼀昀昀琀眀⼀昀昀琀眀ⴀ㌀⸀㌀⸀㈀ⴀ搀氀氀㘀㐀⸀稀椀瀀∀ ⠀␀搀漀眀渀氀漀愀搀开氀漀挀愀琀椀漀渀 ⬀ ∀尀昀昀琀眀ⴀ㌀⸀㌀⸀㈀ⴀ搀氀氀㘀㐀⸀稀椀瀀∀⤀ഀ匀攀琀ⴀ嘀匀ⴀ䔀渀瘀椀爀漀渀洀攀渀琀ഀ甀渀稀椀瀀 ⠀␀搀漀眀渀氀漀愀搀开氀漀挀愀琀椀漀渀 ⬀ ∀尀昀昀琀眀ⴀ㌀⸀㌀⸀㈀ⴀ搀氀氀㘀㐀⸀稀椀瀀∀⤀  ∀␀氀椀戀爀愀爀礀开氀漀挀愀琀椀漀渀尀昀昀琀眀㌀∀ഀ挀搀 ∀␀氀椀戀爀愀爀礀开氀漀挀愀琀椀漀渀尀昀昀琀眀㌀∀ഀ☀ 氀椀戀 ∀⼀洀愀挀栀椀渀攀㨀堀㘀㐀∀ ∀⼀搀攀昀㨀氀椀戀昀昀琀眀㌀ⴀ㌀⸀搀攀昀∀ഀ☀ 氀椀戀 ∀⼀洀愀挀栀椀渀攀㨀堀㘀㐀∀ ∀⼀搀攀昀㨀氀椀戀昀昀琀眀㌀昀ⴀ㌀⸀搀攀昀∀ഀ☀ 氀椀戀 ∀⼀洀愀挀栀椀渀攀㨀堀㘀㐀∀ ∀⼀搀攀昀㨀氀椀戀昀昀琀眀㌀氀ⴀ㌀⸀搀攀昀∀ഀ਀⌀䴀攀猀猀愀最攀 爀攀洀椀渀搀椀渀最 琀漀 猀攀琀 瀀愀琀栀猀ഀ圀爀椀琀攀ⴀ䠀漀猀琀 ∀倀氀攀愀猀攀 攀渀猀甀爀攀 琀栀愀琀 瀀愀琀栀猀 琀漀 琀栀攀 昀漀氀氀漀眀椀渀最 氀漀挀愀琀椀漀渀猀 愀爀攀 椀渀 礀漀甀爀 倀䄀吀䠀 攀渀瘀椀爀漀渀洀攀渀琀 瘀愀爀椀愀戀氀攀㨀 ∀ഀ圀爀椀琀攀ⴀ䠀漀猀琀 ∀    ⴀ 䈀漀漀猀琀 氀椀戀爀愀爀椀攀猀    ⠀琀礀瀀椀挀愀氀氀礀 䌀㨀尀倀爀漀最爀愀洀 䘀椀氀攀猀尀戀漀漀猀琀尀戀漀漀猀琀开㄀开㔀㄀尀氀椀戀⤀∀ഀ圀爀椀琀攀ⴀ䠀漀猀琀 ∀    ⴀ 䌀漀搀攀 匀礀渀琀栀攀猀椀猀 堀匀䐀 ⠀琀礀瀀椀挀愀氀氀礀 䌀㨀尀倀爀漀最爀愀洀 䘀椀氀攀猀 ⠀砀㠀㘀⤀尀䌀漀搀攀匀礀渀琀栀攀猀椀猀 堀匀䐀 ㌀⸀㌀尀戀椀渀尀㬀䌀㨀尀倀爀漀最爀愀洀 䘀椀氀攀猀 ⠀砀㠀㘀⤀尀䌀漀搀攀匀礀渀琀栀攀猀椀猀 堀匀䐀 ㌀⸀㌀尀戀椀渀㘀㐀尀⤀∀ഀ圀爀椀琀攀ⴀ䠀漀猀琀 ∀    ⴀ 䘀䘀吀圀 氀椀戀爀愀爀椀攀猀     ⠀琀礀瀀椀挀愀氀氀礀 䌀㨀尀䴀刀䤀䰀椀戀爀愀爀椀攀猀尀昀昀琀眀㌀⤀∀ഀ圀爀椀琀攀ⴀ䠀漀猀琀 ∀    ⴀ 䠀䐀䘀㔀 氀椀戀爀愀爀椀攀猀     ⠀琀礀瀀椀挀愀氀氀礀 䌀㨀尀倀爀漀最爀愀洀 䘀椀氀攀猀尀䠀䐀䘀 䜀爀漀甀瀀尀䠀䐀䘀㔀尀㄀⸀㠀⸀㤀尀戀椀渀⤀∀ഀ圀爀椀琀攀ⴀ䠀漀猀琀 ∀    ⴀ 䤀匀䴀刀䴀刀䐀            ⠀琀礀瀀椀挀愀氀氀礀 䌀㨀尀倀爀漀最爀愀洀 䘀椀氀攀猀尀椀猀洀爀洀爀搀尀戀椀渀㬀䌀㨀尀倀爀漀最爀愀洀 䘀椀氀攀猀尀椀猀洀爀洀爀搀尀戀椀渀⤀∀ഀ਀ഀ⌀一漀眀 搀漀眀渀氀漀愀搀 愀渀搀 挀漀洀瀀椀氀攀 䤀匀䴀刀䴀刀䐀ഀ搀漀眀渀氀漀愀搀开昀椀氀攀 ∀栀琀琀瀀㨀⼀⼀猀漀甀爀挀攀昀漀爀最攀⸀渀攀琀⼀瀀爀漀樀攀挀琀猀⼀椀猀洀爀洀爀搀⼀昀椀氀攀猀⼀猀爀挀⼀椀猀洀爀洀爀搀开氀愀琀攀猀琀⸀稀椀瀀∀ ⠀␀搀漀眀渀氀漀愀搀开氀漀挀愀琀椀漀渀 ⬀ ∀尀椀猀洀爀洀爀搀开氀愀琀攀猀琀⸀稀椀瀀∀⤀ഀ甀渀稀椀瀀 ⠀␀搀漀眀渀氀漀愀搀开氀漀挀愀琀椀漀渀 ⬀ ∀尀椀猀洀爀洀爀搀开氀愀琀攀猀琀⸀稀椀瀀∀⤀  ∀␀氀椀戀爀愀爀礀开氀漀挀愀琀椀漀渀尀椀猀洀爀洀爀搀∀ഀ挀搀 ∀␀氀椀戀爀愀爀礀开氀漀挀愀琀椀漀渀尀椀猀洀爀洀爀搀∀ഀ一攀眀ⴀ䤀琀攀洀 ∀戀甀椀氀搀∀ ⴀ琀礀瀀攀 搀椀爀攀挀琀漀爀礀ഀ挀搀 戀甀椀氀搀ഀ☀ 挀洀愀欀攀 ∀ⴀ䜀∀ ∀嘀椀猀甀愀氀 匀琀甀搀椀漀 ㄀　 圀椀渀㘀㐀∀ ∀ⴀ䐀䈀伀伀匀吀开刀伀伀吀㴀䌀㨀⼀倀爀漀最爀愀洀 䘀椀氀攀猀⼀戀漀漀猀琀⼀戀漀漀猀琀开㄀开㔀㄀∀ ∀ⴀ䐀堀䔀刀䌀䔀匀䌀开䤀一䌀䰀唀䐀䔀开䐀䤀刀㴀䌀㨀⼀倀爀漀最爀愀洀 䘀椀氀攀猀 ⠀砀㠀㘀⤀⼀䌀漀搀攀匀礀渀琀栀攀猀椀猀 堀匀䐀 ㌀⸀㌀⼀椀渀挀氀甀搀攀⼀砀攀爀挀攀猀挀∀ ∀ⴀ䐀堀䔀刀䌀䔀匀䌀开䰀䤀䈀刀䄀刀䤀䔀匀㴀䌀㨀⼀倀爀漀最爀愀洀 䘀椀氀攀猀 ⠀砀㠀㘀⤀⼀䌀漀搀攀匀礀渀琀栀攀猀椀猀 堀匀䐀 ㌀⸀㌀⼀氀椀戀㘀㐀⼀瘀挀ⴀ㄀　⸀　⼀砀攀爀挀攀猀ⴀ挀开㌀⸀氀椀戀∀ ∀ⴀ䐀堀匀䐀开䐀䤀刀㴀䌀㨀⼀倀爀漀最爀愀洀 䘀椀氀攀猀 ⠀砀㠀㘀⤀⼀䌀漀搀攀匀礀渀琀栀攀猀椀猀 堀匀䐀 ㌀⸀㌀∀ ∀ⴀ䐀䘀䘀吀圀㌀开䤀一䌀䰀唀䐀䔀开䐀䤀刀㴀䌀㨀⼀䴀刀䤀䰀椀戀爀愀爀椀攀猀⼀昀昀琀眀㌀∀ ∀ⴀ䐀䘀䘀吀圀㌀䘀开䰀䤀䈀刀䄀刀夀㴀䌀㨀⼀䴀刀䤀䰀椀戀爀愀爀椀攀猀⼀昀昀琀眀㌀⼀氀椀戀昀昀琀眀㌀昀ⴀ㌀⸀氀椀戀∀ ∀⸀⸀⼀∀ ഀ洀猀戀甀椀氀搀 ⸀尀䤀匀䴀刀䴀刀䐀⸀猀氀渀 ⼀瀀㨀䌀漀渀昀椀最甀爀愀琀椀漀渀㴀刀攀氀攀愀猀