#!/usr/bin/expect -f

# keytool -import -file testcert.pem -alias abcd -keystore abcd.j
# ks
# Enter keystore password:
# Keystore password is too short - must be at least 6 characters
# Enter keystore password:
# Re-enter new password:
# ...
# Trust this certificate? [no]:  yes
# Certificate was added to keystore

#
#
#Enter keystore password:
#keytool error: java.lang.Exception: Certificate not imported, alias <vczm> already exists
#


if {[llength $argv] == 0} {
	send_user "Usage: addcert \'<cert_file>\' \'<password>\' \'<outputfile>\'\n"
	exit 1
}

set certfile [lindex $argv 0]
set password [lindex $argv 1]
set outfile [lindex $argv 2]

log_user 0
spawn keytool -import -file $certfile -alias vczm -keystore "$outfile"
expect {
	timeout { send_user "timed out\n"; exit 11}
	eof { exit 12 }
	"Enter keystore password:"
}
send "$password\r"
expect {
	timeout { send_user "timed out\n"; exit 21 }
	eof { send_user "eof\n";exit 22 }
	"Certificate was added to keystore" { send_user "Certificate was added to keystore\n"; exit 0 }
	"Certificate not imported" { send_user "Failed to add certificate to keystore\n"; exit 23 }
	"password was incorrect" { send_user "Keystore password incorrect\n"; exit 24 }
	"Re-enter new password:"
}
send "$password\r"
expect {
	timeout { send_user "timed out\n"; exit 31 }
	eof { exit 32 }
	"Trust this certificate"
}
send "yes\r"
#exp_internal 1
expect {
	"Certificate was added to keystore" { send_user "Certificate was added to keystore\n" }
	timeout { exit 41 }
	eof { exit 42 }
}
sleep 1
