#!/bin/bash

. /usr/share/nvm/init-nvm.sh

if (( ROFI_RETV == 0 ))
then
    echo -en "\0prompt\x1fPassword for\n"
    temp="$(mktemp)"
    passbolt find > "$temp"
    awk 'NR >1 { str = substr($0, 0, 58) ; gsub(/ +$/, "", str) ;print str"|"$NF}' < "$temp" | while IFS="|" read -r name uuid
    do
        echo -e "$name\0info\x1f$uuid"
    done

else 
    (
    data="$(passbolt get "$ROFI_INFO" | gpg -q)"
    if jq '.' <<<"$data" &>/dev/null
    then
        password="$(jq -r '.password' <<<"$data")"
    else
        password="$data"
    fi

    xclip -i -selection clipboard <<<"$password"
    ) > /dev/null
    exit 0
fi
