#!/usr/bin/env bash
#
# A utility script to get attributes for an entry. getAttr.sh shows a dialog with
# all of the entries in a DB; the user selectls one and is then shown all of the
# attributes for that entry. The user selects one of THOSE, and then the script
# outputs the value of that attribute dependiyng on the script arguments.
# getAtr.sh can put the value in the system clipboard; it can type it (using
# xdotool); or it can simply print it to STDOUTi.
#
# Depends on
# - rofi or fzf (one or both)
# - xsel
# - xdotool
# - colmun, from util-linux (on Arch)

# Number of milliseconds between keystrokes, used by xdotool. 150 is a decent WPM
STROKE=150

function _rook() {
  rook "$@"
}
if type keyctl>/dev/null; then
  rp=$(keyctl list @s | grep rookpin | cut -d: -f1)
  if [[ -n "$rp" ]]; then
    function _rook() {
      keyctl pipe "$rp" | rook --pin "$@"
    }
  fi
fi

function prompt() {
  rofi -dmenu -p $1 -i
}
function output() {
  printf "%s\n" $(<<<"$@" xaggs)
}
function usage() {
  printf "USAGE: %s [args]\n" "$0"
  printf "If no args are provided, rofi will be used for prompting and the selected\n"
  printf "value will be printed to STDOUT\n"
  printf "  -h   Print this help\n"
  printf "  -f   Use fzf instead of rofi\n"
  printf "  -t   The value is typed (via xdotool)\n"
  printf "  -c   The value is copied to the clipboard\n"
}
while getopts ftc opt; do
  case $opt in
    f)
      function prompt() {
        fzf
      }
      ;;
    t)
      function output() {
        sleep 0.5
        xdotool type --delay $STROKE "$(<<<$@ xargs)"
      }
      ;;
    c)
      function output() {
        <<<"$@" xargs | xsel -p
      }
      ;;
    h)
      usage
      exit 0
      ;;
    *)
      usage
      exit 1
      ;;
  esac
done

GOTP="Generate OTP"

IFS=$'\t' ENTRY=($(_rook ls | column -s $'\t' -o $'\t' -t -H 1 | prompt Entry | sed -E 's/\t +/\t/g;s/ +\t/\t/g'))
[[ $? -ne 0 || -z "$ENTRY" ]] && exit

typeset -A KEYS
while read -r line; do
  [[ -z "$SKIP" ]] && SKIP=1 && continue
  key="${line%%:*}"
  if [[ -n "$key" && "$key" != "${line#*:}" ]]; then
    val="$(<<<${line#*:} xargs)"
    KEYS+=(${key} ${val})
  fi
  shopt -s nocasematch
  [[ "$key" =~ "otp" ]] && KEYS+=("$GOTP")
  shopt +s nocasematch
done < <(_rook show "${ENTRY[@]}")

[[ "${#KEYS[@]}" -eq 0 ]] && printf "No matches" && exit 1


SEL=$(printf "%s\n" "${!KEYS[@]}" | sort | prompt Key)
[[ $? -ne 0 ]] && exit

case "$SEL" in
  "$GOTP")
    VAL=$(_rook show -o "${ENTRY[@]}")
    ;;
  *)
    VAL="${KEYS[$SEL]}"
    [[ "${VAL}" = '******' ]] && VAL="$(_rook show -f "$SEL" "${ENTRY[@]}")"
    ;;
esac

output "${VAL}"
