#!/bin/sh
#
# usage:
#   - save it as ~/bin/pinentry-auto
#   - .profile .zprofile:
#     if [ -n "${SSH_CLIENT-}" ]; then export PINENTRY_USER_DATA=USE_CURSES; fi
#   - gpg-agent.conf `pinentry-program bin/pinentry-auto`

# pe=/usr/bin/pinentry-qt
# by defaulting to this alternative, we could place this script at
# `/usr/bin/pinentry` to avoid extra configuration elsewhere,
# however, it might be overwritten by upgrades
pe=/etc/alternatives/pinentry

case "${PINENTRY_USER_DATA-}" in
  *USE_CURSES*) pe=/usr/bin/pinentry-curses ;;
  *USE_GTK2*) pe=/usr/bin/pinentry-gtk-2 ;;
  *USE_GNOME3*) pe=/usr/bin/pinentry-gnome3 ;;
  *USE_X11*) pe=/usr/bin/pinentry-x11 ;;
  *USE_QT*) pe=/usr/bin/pinentry-qt ;;
  *USE_TTY*) pe=/usr/bin/pinentry-tty ;;
  *) printf '%s\n' "${PINENTRY_USER_DATA}" >>/tmp/pinentry.log ;;
esac

exec ${pe} "$@"
