#!/usr/bin/env bash

save-kubeconfig-to-keepass() {
    local keepass_kubeconfig_entry="dotenv/kubeconfig_$USER"
    local kubeconfig_file="$HOME/.kube/config"

    if [[ -n "$1" ]]; then
        keepass_kubeconfig_entry="dotenv/kubeconfig_$1"
        kubeconfig_file="$HOME/.kube/config.$1"
    fi

    keepass_kubeconfig_entry=$(echo "$keepass_kubeconfig_entry" | tr '[:upper:]' '[:lower:]')

    if [[ -f "$kubeconfig_file" ]]; then
        echo "Store $kubeconfig_file file in keepassxc: $keepass_kubeconfig_entry"
        keepassxc-cli add \
            --key-file "$KEEPASS_DATABASE_KEY_FILE" \
            "$KEEPASS_DATABASE_FILE" \
            "$keepass_kubeconfig_entry"
        keepassxc-cli attachment-import \
            --key-file "$KEEPASS_DATABASE_KEY_FILE" \
            "$KEEPASS_DATABASE_FILE" \
            "$keepass_kubeconfig_entry" \
            "$(basename $kubeconfig_file)" \
            "$kubeconfig_file"
    else
        echo "No $kubeconfig_file file found."
    fi
}
sktk() {
    save-kubeconfig-to-keepass "$@"
}
