#!/usr/bin/env python3
# -*-mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8 -*-

import sys
import os
import pexpect

ROLE="proxy" # noqa
KEY="u/S9464F2C2825D2FE3"
PASS="foo"
LOCALE="en_US.UTF-8"

p = pexpect.spawnu('sh /var/local/bin/pinentry3.bash')
p.logfile = sys.stdout
p.timeout = 1

print(dir(p))

p.expect("OK .*\r")
p.send("OPTION grab\r")
p.expect("OK.*")
p.send("OPTION ttyname=" + os.environ['GPG_TTY'] +"\r")
p.expect("OK.*")
p.send("OPTION ttytype=xterm\r")
p.expect("OK.*")
p.send("OPTION lc-ctype=" +LOCALE +"\r")
p.expect("OK.*")
p.send("OPTION lc-messages=en_US.UTF-8\r")
p.expect("OK.*")
p.send("OPTION allow-external-password-cache\r")
p.expect("OK.*")
p.send("OPTION default-ok=_OK\r")
p.expect("OK.*")
p.send("OPTION default-cancel=_Cancel\r")
p.expect("OK.*")
p.send("OPTION default-yes=_Yes\r")
p.expect("OK.*")
p.send("OPTION default-no=_No\r")
p.expect("OK.*")
p.send("OPTION default-prompt=PIN:\r")
p.expect("OK.*")
p.send("OPTION default-pwmngr=_Save in password manager\r")
p.expect("OK.*")
p.send("OPTION default-cf-visi=Do you really want to make your passphrase visible on the screen?\r")
p.expect("OK.*")
p.send("OPTION default-tt-visi=Make passphrase visible\r")
p.expect("OK.*")
p.send("OPTION default-tt-hide=Hide passphrase\r")
p.expect("OK.*")
p.send("GETINFO pid\r")
p.expect("OK.*")
# if str(p.match).find("D " +str(os.getpid())) < 0: raise RuntimeError(p.match)
print(str(p.match))
p.send("SETKEYINFO "+ KEY +"\r")
p.expect("OK.*")
p.send("SETDESC Enter passphrase%0A\r")
p.expect("OK.*")
p.send("SETPROMPT Passphrase\r")
p.expect("OK.*")
p.send("GETPIN\r")
# p.expect("Enter passphrase")
#p.expect("key: " +KEY)
p.expect("Passphrase")
p.send(PASS +"\r")
#p.expect("D " +PASS +"\r\nOK.*")
p.expect("OK.*")
print(str(p.before))
p.send("BYE\r")
p.expect("OK closing connection\r")
p.expect(pexpect.EOF)

p.kill(0)
