#!/bin/sh

entry=$(gopass list --flat | bemenu -p Autofill: -l 25 -c -M 500 --fn 'Monospace 14' --tf '#ff4e00' --ff '#dbc077' --hf '#ff4e00')

if [ -n "$entry" ]; then
    username=$(gopass show "$entry" | grep '^username:' | cut -d' ' -f2-)
    password=$(gopass show -o "$entry")

    if [ -z "$password" ]; then
        notify-send "Gopass Error" "No password for $entry"
        exit 1
    fi

    notify-send "Autofill in 1s: Focus username field in form"
    sleep 1

    # Type username, press Tab, then password, then Enter
    wtype "$username"
    wtype -k Tab
    wtype "$password"
    wtype -k Return
fi
