#!/bin/bash

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.

# Emulates pinentry programs, with a static PIN
# This can be used to bypass PIN requirement while using GnuPG/scdaemon
# and automation. Another solution is to use PSCS/OpenSC.

# Obviously, this is a hack, even if it's pretty damn clean.
# This means, do not use this if you do not know what you are doing.

# If you don't know what to put here,
# you probably shouldn't be using this.
# 123456 is the default for OpenPGP smartcards
USER_PIN=STRIKEfreedom@Qu1to


function tolower()
{
	echo $1 | tr '[A-Z]' '[a-z]'
}

function reply_ok()
{
	echo "OK $*"
}

function reply_d()
{
	echo "D $*"
	echo "OK"
}

function error()
{
	echo "ERR 83886355 unknown command"
}

function main()
{
	echo "OK Your orders please"
	while read reply; do
			reply=$(tolower "$reply")
			case $reply in
				"getpin")
					reply_d $USER_PIN
				;;
				"bye")
					reply_ok "closing connection"
					break
				;;
				"getinfo pid")
					reply_d $BASHPID
				;;
				*)
				# This generally includes OPTION, SETDESC, SETPROMPT
				# i.e. things we don't care for if we're not displaying
				# a prompt
					reply_ok
				;;
			esac
	done
}

main

exit 0
