#!/usr/bin/env bash

# kbs2-yad-login: Add a login record to kbs2 via a YAD form.

set -eo pipefail

[[ -n "${KBS2_SUBCOMMAND}" ]] \
  || { >&2 echo "Fatal: Not being run as a subcommand?"; exit 1; }

record=$(
  yad \
    --form \
    --separator=$'\x01' \
    --item-separator=$'\x01' \
    --text-align=left \
    --text="New Login" \
    --title="kbs2" \
    --field="Label" \
    --field="Username" \
    --field="Password:H" \
    "" \
    "" \
    "$(kbs2 generate)"
)

label=$(awk -F $'\x01' '{ print $1 }' <<< "${record}")
username=$(awk -F $'\x01' '{ print $2 }' <<< "${record}")
password=$(awk -F $'\x01' '{ print $3 }' <<< "${record}")

kbs2 new "${label}" <<< "${username}"$'\x01'"${password}"
