#!/usr/bin/env bash

type="$1"
account="$2"
jq_chain="."
vault_type="text"

case "$type" in
  pass)
    jq_chain='select(.label == "gmail app-password") | .password'
    ;;
  user)
    jq_chain='select(.label == "gmail app-password") | .login'
    ;;
  realname)
    jq_chain='select(.label == "Real Name") | .password'
    ;;
  email)
    vault_type="email"
    jq_chain='select(.password != "") | .password'
    ;;
  smtp)
    vault_type="email"
    jq_chain='select(.password != "") | "smtp://" + .password + "@smtp.gmail.com:587/"'
    ;;
  *)
    exit 1
    ;;
esac

enpasscli \
  -vault ~/Documents/Enpass/Vaults/primary \
  -json \
  --type "$vault_type" \
  show "Google ($account)" \
  | jq -r '.[] | '"$jq_chain"
