src/Install-Jdk.psm1

using namespace Belin.Cli.Compression
using namespace Belin.Cli.Validation

<#
.SYNOPSIS
    Downloads and installs the latest OpenJDK release.
.OUTPUTS
    The output from the `java --version` command.
#>

function Install-Jdk {
    [CmdletBinding()]
    [OutputType([string])]
    param (
        # The path to the output directory.
        [Parameter(Position = 0)]
        [ValidateScript({ Test-Path $_ -IsValid }, ErrorMessage = "The specified output path is invalid.")]
        [string] $Path = $IsWindows ? "C:\Program Files\OpenJDK" : "/opt/openjdk",

        # The major version of the Java development kit.
        [ValidateSet(11, 17, 21, 25)]
        [int] $Version = 25
    )

    if (-not (Test-Privilege $Path)) {
        throw [UnauthorizedAccessException] "You must run this command in an elevated prompt."
    }

    $platform, $extension = switch ($true) {
        ($IsMacOS) { "macOS", "tar.gz"; break }
        ($IsWindows) { "windows", "zip"; break }
        default { "linux", "tar.gz" }
    }

    $file = "microsoft-jdk-$Version-$platform-x64.$extension"
    "Downloading file ""$file""..."
    $outputFile = New-TemporaryFile
    Invoke-WebRequest "https://aka.ms/download-jdk/$file" -OutFile $outputFile

    "Extracting file ""$file"" into directory ""$Path""..."
    if ($extension -eq "zip") { Expand-ZipArchive $outputFile -DestinationPath $Path -Skip 1 }
    else { Expand-TarArchive $outputFile -DestinationPath $Path -Skip 1 }

    $executable = $IsWindows ? "java.exe" : "java"
    & "$Path/bin/$executable" --version
}