install-labbuildr.ps1

<#PSScriptInfo
 
.VERSION 4.0
 
.GUID fe3fb4e9-9977-4f99-8a71-b717acac7e30
 
.AUTHOR "Karsten Bott"
 
.COMPANYNAME
 
.COPYRIGHT Karsten Bott
 
.TAGS
 
.LICENSEURI
 
.PROJECTURI http://github.com/bottkars/labbuildr
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
.DESCRIPTION "This is the installer for labbuildr"
#>

[CmdletBinding(DefaultParametersetName = "install",
    SupportsShouldProcess=$true,
    ConfirmImpact="Medium")]
    [OutputType([psobject])]
param (
[Parameter(ParameterSetName = "install",Mandatory = $false, HelpMessage = "select a default path to install")][string]$Installpath = "$($HOME)/labbuildr2017",
[Parameter(ParameterSetName = "install",Mandatory = $false, HelpMessage = "select a branch to install from")][ValidateSet('master','testing','develop')]$branch  = "master",
[Parameter(ParameterSetName = "install",Mandatory = $false, HelpMessage = "start without openwrt autoload")][switch]$noopenwrt
)

function Install-FromGit
{


param (
    [string]$Repo,
    [string]$RepoLocation,
    [string]$branch,
    [string]$Destination
    )
        Write-Verbose "Using Install-FromGit function for $repo"
        [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::TLS12 
        $Uri = "https://api.github.com/repos/$RepoLocation/$repo/commits/$branch"
        $Zip = ("https://github.com/$RepoLocation/$repo/archive/$branch.zip").ToLower()
        Remove-Item -Path $Destination -Force -Confirm:$false -ErrorAction SilentlyContinue -Recurse | Out-Null
        Write-Host -ForegroundColor Magenta "Now Downloading $Repo"
        $DownloadPath = Join-path $Installpath "Install"
        if (!(Get-Item -Path $DownloadPath -ErrorAction SilentlyContinue))
                {
                $newDir = New-Item -ItemType Directory -Path "$DownloadPath"
                }
        Write-Host "We are now downloading $Zip"
        Get-LABHttpFile -SourceURL $Zip -TarGetFile "$DownloadPath/$repo-$branch.zip" -ignoresize
        if ($PSVersionTable.PSVersion.Major -ge 5) #means we can use native expand method tha sucks
            {
            Expand-Archive -Path "$DownloadPath/$repo-$branch.zip"  -DestinationPath (join-path $Destination expand) -Force
            Move-Item -Path ("$Destination/expand/$repo-$branch/$repo/*") "$Destination" -Force -Confirm:$false
            }
        else #oh jeay, we can use .net method that allows subdirs extraction !Yeahah, but not on lin/osx
            {    
        Expand-LABZip -zipfilename "$DownloadPath/$repo-$branch.zip" -destination $Destination -Folder $repo-$branch/$repo
            }
}
#####
function Expand-LABZip
{
 [CmdletBinding(DefaultParameterSetName='Parameter Set 1',
    HelpUri = "https://github.com/bottkars/LABbuildr/wiki/LABtools#Expand-LABZip")]
    param (
        [string]$zipfilename,
        [string]$destination,
        [String]$Folder)
    $copyFlag = 16 # overwrite = yes
    $Origin = $MyInvocation.MyCommand
    if (test-path($zipfilename))
    {
    If ($Folder)
        {
        $zipfilename = Join-Path $zipfilename $Folder
        }
            
        Write-Verbose "extracting $zipfilename to $destination"
        if (!(test-path  $destination))
            {
            New-Item -ItemType Directory -Force -Path $destination | Out-Null
            }

        $shellApplication = New-object -com shell.application
        $zipPackage = $shellApplication.NameSpace($zipfilename)
        $destinationFolder = $shellApplication.NameSpace("$destination")
        $destinationFolder.CopyHere($zipPackage.Items(), $copyFlag)
    }
}

function Get-LABHttpFile
 {
    [CmdletBinding(DefaultParametersetName = "1",
    HelpUri = "https://github.com/bottkars/LABbuildr/wiki/LABtools#GET-LABHttpFile")]
    param (
    [Parameter(ParameterSetName = "1", Mandatory = $true,Position = 0)]$SourceURL,
    [Parameter(ParameterSetName = "1", Mandatory = $false)]$TarGetFile,
    [Parameter(ParameterSetName = "1", Mandatory = $false)][switch]$ignoresize
    )

if (!$TarGetFile)
    {
    $TarGetFile = Split-Path -Leaf $SourceURL
    }
try
                    {
                    $Request = Invoke-WebRequest $SourceURL -UseBasicParsing -Method Head
                    }
                catch [Exception] 
                    {
                    Write-Warning "Could not downlod $SourceURL"
                    Write-Warning $_.Exception
                    break
                    }
                
                $Length = $request.Headers.'content-length'
                try
                    {
                    Invoke-WebRequest $SourceURL -OutFile $TarGetFile
                    }
                catch [Exception] 
                    {
                    Write-Warning "Could not downlod $SourceURL. please download manually"
                    Write-Warning $_.Exception
                    break
                    }
                if ( (Get-ChildItem  $TarGetFile).length -ne $Length -and !$ignoresize)
                    {
                    Write-Warning "File size does not match"
                    Remove-Item $TarGetFile -Force
                    break
                    }                       

}   
############################
Push-Location
$Icon = Split-Path -Leaf $Installpath

try
    {
    New-Item -ItemType Directory -Path $Installpath -force -ErrorAction Stop | Out-Null
    }
catch [System.IO.DriveNotFoundException]
    {
    Write-Warning "could not create directory $Installpath, error was $_"
    break
    }
if ($Installpath[-1] -eq "\")
    {
    $Installpath = $Installpath -replace ".$"
    }
Set-Location $Installpath
# downloading the repos. not using
$Repo = "labbuildr"
$RepoLocation = "bottkars"
$Destination = "$Installpath"
Install-FromGit -Repo $Repo -RepoLocation $RepoLocation -branch $branch -Destination $Destination
####
$Repo = "labbuildr-scripts"
$RepoLocation = "bottkars"
$Destination = Join-Path $Installpath $Repo
Install-FromGit -Repo $Repo -RepoLocation $RepoLocation -branch $branch -Destination $Destination
####
$Repo = "labtools"
$RepoLocation = "bottkars"
$Destination = Join-Path $Installpath $Repo
Install-FromGit -Repo $Repo -RepoLocation $RepoLocation -branch $branch -Destination $Destination
####
$Repo = "vmxtoolkit"
$RepoLocation = "bottkars"
$Destination = Join-Path $Installpath $Repo
Install-FromGit -Repo $Repo -RepoLocation $RepoLocation -branch $branch -Destination $Destination

#####
$Branch | Set-Content -Path "$Installpath/labbuildr.branch" -Force # -Verbose
ipmo ./vmxtoolkit
ipmo ./labtools
./build-lab.ps1 -createshortcut
Write-Host -ForegroundColor Cyan "Installed labbuildr in $Installpath,
return will start the labbuildr shell"

Pause
./profile.ps1 -noopenwrt:([System.Convert]::ToBoolean($noopenwrt.IsPresent))