functions/_Get-VendirPath.ps1

# <copyright file="_Get-VendirPath.ps1" company="Endjin Limited">
# Copyright (c) Endjin Limited. All rights reserved.
# </copyright>

<#
.SYNOPSIS
    Resolves the path to the vendir binary.

.DESCRIPTION
    Resolves the vendir binary using the following priority order:
    1. System PATH - uses Get-Command to find vendir already installed on the system
    2. Module-local bin/ directory - checks for a previously downloaded copy
    3. Auto-download - downloads the binary via _Install-Vendir as a fallback

    This ensures users who manage vendir themselves are respected, while
    providing a seamless first-run experience for new users.

.EXAMPLE
    $vendirPath = _Get-VendirPath

    Returns the absolute path to the vendir binary, downloading it if necessary.
#>

function _Get-VendirPath {
    [CmdletBinding()]
    param ()

    # 1. Check system PATH
    $systemVendir = Get-Command 'vendir' -ErrorAction SilentlyContinue
    if ($systemVendir) {
        Write-Verbose "Using vendir from system PATH: $($systemVendir.Source)"
        return $systemVendir.Source
    }

    # 2. Check module-local bin/ directory
    $ext = if ($IsWindows) { '.exe' } else { '' }
    $localPath = Join-Path $PSScriptRoot '..' 'bin' "vendir$ext"
    $localPath = [System.IO.Path]::GetFullPath($localPath)

    if (Test-Path $localPath) {
        Write-Verbose "Using vendir from module bin/: $localPath"
        return $localPath
    }

    # 3. Auto-download
    Write-Verbose "vendir not found in PATH or module bin/. Downloading..."
    $downloadedPath = _Install-Vendir
    return $downloadedPath
}