#!/usr/bin/env bash
# Very basic interface for rbw using rofi
if [ $(hostname) = "syl" ]; then
       if [ $XDG_SESSION_TYPE = "x11" ]; then
           style="laptop-rbw"
           echo "this is x11"
       else
           style="laptop-wayland"
           echo "this is wayland"
       fi
else 
    style="desktop-rbw"
    #echo "this is not hidpi"
fi

export YDOTOOL_SOCKET=/tmp/ydotools

# Get all password files and create an array
root=~/.password-store
CACHE=~/.local/tmp/pass_rofi
seat=seat0

rbw sync
rbw list

list_passwords() {
    rbw list
}

passwords=$(rbw list)

prompt='BW'
if [ $XDG_SESSION_TYPE = "x11" ]; then
    SECRET=$(list_passwords | rofi -sync -i -p "${prompt}" -dmenu -theme ~/.config/rofi/launchers-git/$style.rasi
             # $@ &
             # c=0
             # while ! xprop -f _KDE_NET_WM_BLUR_BEHIND_REGION 32c -set _KDE_NET_WM_BLUR_BEHIND_REGION 0 -id $(xdotool search -class 'rofi') ; do
             #     sleep .1 
             #     c=$((c+1))
             #     [[ c = 50 ]] && exit; # stop script window didn't appear after 5 seconds
             # done
          )
else
    SECRET=$(list_passwords | rofi -no-lazy-grab -sync -i -p "${prompt}" -dmenu -theme ~/.config/rofi/launchers-git/$style.rasi)
fi

# Ask whether pass, user or both are required
options=("Password" \
             "User" \
             "User and password" \
             "QR-Code" \
	     "OTP")

if [ $XDG_SESSION_TYPE = "x11" ]; then
    option=$(printf '%s\n' "${options[@]%}" | rofi -i -sync -dmenu -p "" -theme ~/.config/rofi/launchers-git/$style.rasi
             # $@ &
             # c=0
             # while ! xprop -f _KDE_NET_WM_BLUR_BEHIND_REGION 32c -set _KDE_NET_WM_BLUR_BEHIND_REGION 0 -id $(xdotool search -class 'rofi') ; do
             #     sleep .1 
             #     c=$((c+1))
             #     [[ c = 50 ]] && exit; # stop script window didn't appear after 5 seconds
             # done
          )
else
    option=$(printf '%s\n' "${options[@]%}" | rofi -i -sync -dmenu -p "" -theme ~/.config/rofi/launchers-git/$style.rasi)
fi

# echo $option

case ${option} in
    Password )
	echo "${SECRET}"
	ydotool type --key-delay 6ms $(rbw get "${SECRET}")
	;;
    User )
	echo "$(rbw get --full "${SECRET}" | rg Username: | awk '{print $2}')"
	ydotool type --key-delay 6ms "$(rbw get --full "${SECRET}" | rg Username: | awk '{print $2}')"
	;;
    "User and password" )
	ydotool type --key-delay 6ms $(rbw get --full "${SECRET}" | rg Username: | awk '{print $2}')
	ydotool key 15:1 15:0
	ydotool type --key-delay 6ms $(rbw get "${SECRET}")
	;;
    "QR-Code" )
	if [[ $SECRET =~ wifi$ ]]; then
	    # Produce a valid wifi QR-code
	    WIFISSID=$(pass get_user "${SECRET}")
	    WIFIPASS=$(pass get_pass "${SECRET}")
	    WIFIQR="WIFI:T:WPA;S:${WIFISSID};P:${WIFIPASS};;"
	    qrencode -s 8 -o - $WIFIQR | feh --title "pass: QR-WIFI" -
	else
	    # Only password
	    pass show -q1 ${SECRET}
	fi
	;;
    OTP )
	ydotool type --key-delay 6ms $(rbw code "${SECRET}")
	;;
esac

# wl-copy -o -s ${seat} ${PASSWD_PASS}
