#!/usr/bin/env awk -f
#
# usage:
#   [credential "https://codeberg.org"]
#       helper = pass-store
# ---
# see: https://felixhanley.info/notes/awk-git-credentials.html

function ltrim(s) { sub(/^[ \t\r\n]+/, "", s); return s }
function rtrim(s) { sub(/[ \t\r\n]+$/, "", s); return s }
function trim(s) { return rtrim(ltrim(s)); }

BEGIN {
    if (ARGV[1] != "get") {
        exit 0
    }
    ARGC--
    FS="="
    getline
}
$1 == "host" {
    username = ENVIRON["USER"]
    FS=":"
    pass = "pass show "$2
    while ((pass | getline) > 0) {
        if ($1 ~ "login|user") {
            username = trim($2)
        }
        if ($1 ~ "token|pat") {
            printf "password=%s\nusername=%s\n", trim($2), username
            exit 0
        }
    }
    close(pass)
    exit 0
}
