framework/Resources/Scripts/NCache/installation.ps1

$Global:WindowsNCachePath = "C:\Program Files\NCache"
$Global:LinuxNCachePath   = "/opt/ncache"

function Get-NCacheInstallDir{
    param(
        [string]$InstallNCacheDir,
        [string]$OS,
        [string]$ResourceGroupName
    )

    $Path = $null

    #1 Use provided install path
    if ($InstallNCacheDir) {
        $Path = $InstallNCacheDir
    }

    #2 If Resource Group is provided, check Azure tags
    elseif ($ResourceGroupName) {
        try {
            $rg = Get-AzResourceGroup -Name $ResourceGroupName -ErrorAction Stop
            if ($rg.Tags.ContainsKey("InstallNCacheDir")) {
                $Path = $rg.Tags["InstallNCacheDir"]
                Write-Host "Path fetched from Resource Group tag: $Path"
            }
            else {
                Write-Host "No 'InstallNCacheDir' tag found on Resource Group '$ResourceGroupName'." -ForegroundColor Yellow
            }
        }
        catch {
            Write-Warning "Failed to fetch Resource Group info for '$ResourceGroupName'. $_"
        }
    }

    #3 If OS provided, use corresponding global path
    elseif ($OS) {
        switch ($OS.ToLower()) {
            "windows" { $Path = $Global:WindowsNCachePath }
            "linux"   { $Path = $Global:LinuxNCachePath }
            default   { Write-Warning "Unsupported OS type: $OS" }
        }
    }

    #4 Throw if still undefined
    if (-not $Path) {
        throw "Unable to determine NCache installation path. Please specify -InstallNCacheDir or ensure Resource Group has 'InstallNCacheDir' tag."
    }

    #5 Path should contain NCache in end
    if ($OS -and $OS.ToLower() -eq "windows") {
        if ($Path -notmatch "(?i)NCache$") {
            $Path = (Join-Path $Path "NCache")
        }
    }
    elseif ($OS -and $OS.ToLower() -eq "linux") {
        if ($Path -notmatch "(?i)ncache$") {
            $Path = (Join-Path $Path "ncache")
        }
    }

    # 6. Normalize path slashes
    if ($OS -and $OS.ToLower() -eq "windows") {
        # Convert forward slashes to backslashes
        $Path = $Path -replace '/', '\\'
    }
    elseif ($OS -and $OS.ToLower() -eq "linux") {
        # Convert backslashes to forward slashes
        $Path = $Path -replace '\\', '/'
    }

    return $Path
}