MSLab.psm1

function Get-MSLabReleases {
    $r = Invoke-RestMethod -UseBasicParsing -Uri "https://api.github.com/repos/microsoft/mslab/releases"
    $r | Sort-Object published_at | Select-Object @{ Name="version"; Expression={ $_.'tag_name' } }, created_at, prerelease 
}

function Read-Confirmation {
  Param(
    [Parameter(Mandatory=$false)]
    [string]$Prompt,
    [Parameter(Mandatory=$false)]
    [string]$Message
  )

  $choices = New-Object Collections.ObjectModel.Collection[Management.Automation.Host.ChoiceDescription]
  $choices.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList '&Yes'))
  $choices.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList '&No'))

  -not [bool]$Host.UI.PromptForChoice($Message, $Prompt, $choices, 1)
}

function Initialize-MSLabFolder {
    [cmdletbinding(SupportsShouldProcess)]
    param(
        $Version = "latest",
        $Destination = (Get-Location).Path
    )

    $doInstallation = if (-not $PSBoundParameters['Confirm'].IsPresent) {
      Read-Confirmation -Prompt "Initialize MSLab in $Destination directory?"
    } else {
      $true
    }

    if($doInstallation) {
    $r = Invoke-RestMethod -UseBasicParsing -Uri "https://api.github.com/repos/microsoft/mslab/releases/$($Version)"
    $link = $r.assets | Select-Object -First 1 -ExpandProperty browser_download_url

    $temporaryPath = New-TemporaryFile
    Invoke-WebRequest  -Uri $link -OutFile $temporaryPath.FullName

    Expand-Archive -Path $temporaryPath.FullName -DestinationPath $Destination

    Remove-Item -path $temporaryPath -force
    }
}

function Update-MSLabLinuxTemplates {
    $packerTemplatesDirectory = "$PSScriptRoot\ParentDisks\PackerTemplates\"
    if (-not (Test-Path $packerTemplatesDirectory)) {
        "Packer templates directory does not exist."
        return 
    }

    $templatesBase = "https://github.com/microsoft/mslab-templates/releases/latest/download/"
    $templatesFile = "$($packerTemplatesDirectory)\templates.json"

    #$current = Get-Content $templatesFile | ConvertFrom-Json
    #"Current version: $($current.version)"

    Invoke-WebRequest -Uri "$($templatesBase)/templates.json" -OutFile $templatesFile
    if(-not (Test-Path -Path $templatesFile)) {
        "Download of Packer templates failed"
        return
    }

    $templatesInfo = Get-Content -Path $templatesFile | ConvertFrom-Json
    foreach($template in $templatesInfo.templates) {
        " * $($template.package)"
        $templateDestinationPath = (Join-Path $packerTemplatesDirectory $template.directory)
        if(Test-Path -Path $templateDestinationPath) {
            Remove-Item -Force -Recurse -Path $templateDestinationPath
        }

        $templateZipFile = Join-Path $packerTemplatesDirectory $template.package
        Invoke-WebRequest -Uri "$($templatesBase)/$($template.package)" -OutFile $templateZipFile
        Expand-Archive -Path $templateZipFile -DestinationPath $templateDestinationPath
        Remove-Item -Path $templateZipFile
    }

    $current = Get-Content $templatesFile | ConvertFrom-Json
    "Downloaded version: $($current.version)"

}