Proxy/Dependencies.ps1

$BaseDir = Split-Path -Path $PSScriptRoot -Parent
. (Join-Path $baseDir 'Generic' 'Versioning.ps1')
. (Join-Path $PSScriptRoot 'OpenSSL.ps1')

<#
.SYNOPSIS
    Ensures a program is installed via winget if it's not already available.
#>

function Get-Win32Program {
    [CmdletBinding(DefaultParameterSetName = 'ByName', SupportsShouldProcess = $true)]
    param(
        [Parameter(Mandatory, Position = 0)] [string] $Name
    )
    
    if (-Not (Get-Command -Name $Name -ErrorAction SilentlyContinue)) {
        Write-Host 'Installing ' -NoNewline
        Write-Host $Name -ForegroundColor Cyan -NoNewline
        Write-Host ' via winget'
        if ($PSCmdlet.ShouldProcess(
            "winget",
            "Install $Name"
        )) {
            winget install $Name --silent --accept-package-agreements
        }
    }
}

function Install-Winget {
    [CmdletBinding(SupportsShouldProcess = $true)]
    param()

    if ($PSCmdlet.ShouldProcess("Installing winget from PSGallery", "Install-Winget")) {
        Set-PSRepository -Name PSGallery -InstallationPolicy Trusted -ErrorAction Stop
        Set-ExecutionPolicy -Scope Process -ExecutionPolicy Unrestricted -Force
        Install-Script -Name winget-install -Force -ErrorAction Stop
        Write-Verbose 'Winget installer script deployed.'
    }
}

<#
.SYNOPSIS
    Installs all required dependencies like OpenSSL, NSSM, sing-box, and frp-panel.
#>

function Install-Dependencies {
    [CmdletBinding(SupportsShouldProcess = $true)]
    param(
        [Parameter(Mandatory = $false, Position = 0)]
        [string] $InstallPath = (Join-Path $BaseDir 'bin')
    )

    if (-not (Get-WingetVersion)) {
        Write-Host 'Winget ' -ForegroundColor Cyan -NoNewline
        Write-Host 'not found. Attempting to install...'
        Install-Winget
    }
    Write-Host "Winget version $(Get-WingetVersion)"

    $opensslPaths = Find-OpenSSLStartScript
    if (-not $opensslPaths) {
        Get-Win32Program 'ShiningLight.OpenSSL.Light'
        $opensslPaths = Find-OpenSSLStartScript
    }
    Write-Host "OpenSSL found at: $($opensslPaths -join ', ')"

    foreach ($pkg in 'NSSM.NSSM','SagerNet.sing-box') {
        Get-Win32Program $pkg
    }
    #Write-Host "Sing-box version $($(Get-SingBoxVersionInfo)["version"])"

    if (-not (Test-Path $InstallPath)) {
        Install-FrpPanel -DestinationPath $InstallPath
    }

    if (-not (Get-Module -ListAvailable -Name powershell-yaml)) {
        if ($PSCmdlet.ShouldProcess(
            "powershell-yaml Module",
            "Install-Module"
        )) {
            Write-Host 'Installing module ' -NoNewline
            Write-Host 'powershell-yaml' -ForegroundColor Cyan
            Install-Module -Name powershell-yaml -Force -Scope CurrentUser
        }
    }
    return $opensslPaths
}