#!/bin/sh

entry=$(gopass list --flat | ~/.config/rofi/launchers.d/rofi_launcher_autofill.sh)

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"
    sleep 0.2
    wtype -k Tab
    sleep 0.2
    wtype "$password"
    sleep 0.2
    wtype -k Return
fi
