# $Id: gencerts.sh,v 1.1 2007/07/22 23:59:18 blaine Exp $

# JMX Accelerated Howto and its sample code are copyrighted with the
# BSD license available in the Howto document.
# Copyright (c) 2004-2007, Axis Data Management Corp

# This script generates test certificate keystores from (required) keystores
# containing private keys.

Failout {
    echo "$@" 1>&2
    exit 1
}

rm -f server-cert.store client1-cert.store client1.cert server.cert client2-cert.store client2.cert

keytool -export -alias server.rec -keystore server.store -file server.cert -storepass pwdSstore ||
Failout 'Failed to export server cert'
keytool -export -alias client1.rec -keystore client1.store -file client1.cert -storepass pwdC1store ||
Failout 'Failed to export client1 cert'
keytool -export -alias client2.rec -keystore client2.store -file client2.cert -storepass pwdC2store ||
Failout 'Failed to export client2 cert'

yes |
keytool -import -trustcacerts -keystore server-cert.store -alias server -file server.cert -storepass changeit ||
Failout 'Failed to import server cert'
yes |
keytool -import -trustcacerts -keystore client1-cert.store -alias client1 -file client1.cert -storepass changeit ||
Failout 'Failed to import client1 cert'
yes |
keytool -import -trustcacerts -keystore client2-cert.store -alias client2 -file client2.cert -storepass changeit ||
Failout 'Failed to import client2 cert'

# Since the commands above are so verbose, we need to make it clear to user
# that everything has succeeded.
echo -e '\n100% success'
