PS.Git.psm1

function Initialize-NativeGitLibrary {
    # Detect OS and architecture
    if ($IsWindows) {
        $os = 'Windows'
    }
    elseif ($IsLinux) {
        $os = 'Linux'
        # Optional: Detect specific Linux distro (like Ubuntu)
        if (Test-Path /etc/os-release) {
            $osRelease = Get-Content /etc/os-release | ConvertFrom-StringData -Delimiter '='
            $distroName = $osRelease['NAME']
            Write-Verbose "Detected Linux distribution: $distroName"
            if ($distroName -notlike '*Ubuntu*') {
                throw "Unsupported Linux distribution: $distroName. Only Ubuntu x64 is officially supported."
            }
        }
    }
    else {
        throw "Unsupported operating system. Only Windows and Ubuntu x64 are supported."
    }

    $arch = [System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture
    if ($arch -ne 'X64') {
        throw "Unsupported architecture: $arch. Only x64 is supported."
    }

    # Build native library path
    $basePath = Join-Path $PSScriptRoot "runtimes"
    if ($IsWindows) {
        $dllPath = Join-Path $basePath "win-x64\native\git2-3f4182d.dll"
    }
    elseif ($IsLinux) {
        $dllPath = Join-Path $basePath "linux-x64/native/libgit2-3f4182d.so"
    }

    if (-not (Test-Path $dllPath)) {
        throw "Native library not found: $dllPath"
    }

    # Load native library
    try {
        [System.Runtime.InteropServices.NativeLibrary]::Load($dllPath) | Out-Null
        Write-Host "Loaded native library: $dllPath"
    }
    catch {
        throw "Failed to load native library: $dllPath. Error: $_"
    }
}



# Call it immediately when the module is imported
Initialize-NativeGitLibrary

# Load the main DLL
$mainDllPath = Join-Path $PSScriptRoot "PS.Git.dll"
if (-not (Test-Path $mainDllPath)) {
    throw "Main DLL not found: $mainDllPath"
}

try {
    Import-Module -Name $mainDllPath -Verbose
    Write-Verbose "Loaded main DLL: $mainDllPath"
}
catch {
    throw "Failed to load main DLL: $mainDllPath. Error: $_"
}


# Export your functions here
Export-ModuleMember -Cmdlet * -Alias *