#!/pkg/expect/bin/expect -f

# create specified keyring with one key in it, exit status correct.

# generic subroutine for the suspect error cases.
proc die {s} {
    global pgp argv0

    set str "Unknown exit status"
    switch $s {
    1	{ set str "Unknown error" }
    2	{ set str "Timeout from PGP" }
    3	{ set str "Password incorrect" }
    4	{ set str "Bad signature" }
    5	{ set str "No output file" }
    6	{ set str "No keys matched" }
    }
    puts stderr "$argv0: $str"
    exit $s
}

if {$argc != 2} {
    puts stderr "Usage: $argv0 keyring keyfile\n"
    exit 255
}

set timeout 100
log_user 0

exec rm -f pubring.pkr [lindex $argv 0]
set pgp [spawn -noecho pgpk -a +BATCHMODE=on [lindex $argv 1]]

expect {
    "Enter pass phrase:"	{ system kill $pgp; die 3 }
    -re "Error:|Cannot open|Unable"
				{ die 1 }
    "BAD signature"		{ die 4 }
    "No keys"			{ die 6 }
    timeout			{ die 2 }
}

# Stupid PGP has trouble switching keyrings, so I have to do it instead.
exec cp pubring.pkr [lindex $argv 0]

exit 0
