#!/usr/bin/env fish
# SPDX-FileCopyrightText: 2024 AerynOS Developers
# SPDX-License-Identifier: MPL-2.0

function __aos_repo_dir
    realpath (dirname (readlink -m (status -f)))/../
end

function __aos_toplevel
    git rev-parse --show-toplevel
end

function cpesearch -d "Search for known CPE entries for a program or library"
    if test "$argv[1]" = --help; or test "$argv[1]" = -h; or test (count $argv) -ne 1
        echo "usage: cpesearch <package-name>"
    else
        curl -s -X POST https://cpe-guesser.cve-search.org/search -d "{\"query\": [\"$argv[1]\"]}" | jq .

        echo "Verify successful hits by visiting https://cve.circl.lu/search/\$VENDOR/\$PRODUCT"
        echo "- CPE entries for software applications have the form 'cpe:2.3:a:\$VENDOR:\$PRODUCT"
    end
end

function gotoaosrepo -d "Go to the root of the AerynOS recipes repository"
    cd (__aos_repo_dir)
end

function goroot -d "Go to the root of the current Git repository"
    cd (__aos_toplevel)
end

function chpkg -a package -d "Go to a package directory"
    cd (__aos_toplevel)/*/$package
end

function fix-version-strings -d "Quote unquoted version strings in recipes"
    find . -type f -name "stone.yaml" -exec sed -i -E \
        -e 's|^([[:space:]]*version[[:space:]]*:[[:space:]]*)'\''([^'\'']*)'\''|\1"\2"|' \
        -e 's|^([[:space:]]*version[[:space:]]*:[[:space:]]*)([^"'\''[:space:]][^[:space:]]*)|\1"\2"|' \
        {} +
end

complete -c gotoaosrepo -f
complete -c goroot -f
complete -c chpkg -f
complete -c chpkg -a "(path basename (__aos_toplevel)/*/*)"
