#!/bin/bash
#
pushd $(dirname $0) >/dev/null
SCRIPTPATH=$(pwd -P)
popd >/dev/null

# Crap on command failure
set -e
#
MYPROGNAME=$(basename $0)
AUTHORITY="pbz@ogopogo.biz"
FQDNS="guanabana.bernatchez.net kiwi.bernatchez.net"
OTHER_SUBDOMAINS="repo relay"
HOSTVALIDITY_WEEKS=104
HOSTTAG="bzadmin"
CLIENTVALIDITY_WEEKS=52
CLIENTTAG="bzadmin"
CLIENTUSERS="gituser relay tunneladmin ubuntu"
CLIENTHOSTS="guanabana.bernatchez.net kiwi.bernatchez.net"
function autohelp {
    cat <<EOF
Use: ${MYPROGNAME}
        To see this help
  
    Before you start.
    These defaults in ${MYPROGNAME} may be changed to what you want.
        AUTHORITY=${AUTHORITY}
        FQDNS=${FQDNS}
        OTHER_SUBDOMAINS=${OTHER_SUBDOMAINS}
        HOSTVALIDITY_WEEKS=${HOSTVALIDITY_WEEKS}
        HOSTTAG=${HOSTTAG}
        CLIENTVALIDITY_WEEKS=${CLIENTVALIDITY_WEEKS}
        CLIENTTAG=${CLIENTTAG}
        CLIENTUSERS=${CLIENTUSERS}
        CLIENTHOSTS=${CLIENTHOSTS}

Use: ${MYPROGNAME} newcas
        To create databse in current directory ${MYDIR} and create host and client signing
        authorities.  You will be asked for their passphrases by this script.
        Preferably have these entries prepped in KEEPASSX with passphrases: 
            ssh cert authority key: ${AUTHORITY} ${AUTHORITY}_hostca_${HOSTTAG}
            ssh cert authority key: ${AUTHORITY} ${AUTHORITY}_clientca_${CLIENTTAG}
    
Use: ${MYPROGNAME} certpubs
        To sign certificates for the 6 (at this writing) .pub keys in temppubs/ subdirectory.
        Do this once after creating the database.

Use: ${MYPROGNAME} certserver
        To set up ${FQDNS} (with ${OTHER_SUBDOMAINS}) for host authentication.
        Do this once after creating the database.

Use: ${MYPROGNAME} publish
        To generate parts/sshclient.md and export certificate files to certificates/
        under the directory certificate_content. Do this last.
EOF
}

if ! [[ -d "temppubs" ]]
then
    autohelp
    echo NO SUB-DIRECTORY temppubs.
    echo Try a different current directory than this:
    pwd
    exit 0
fi

if ! [[ -d "certificate_content/certificates" ]]
then
    autohelp
    echo NO SUB-DIRECTORY "certificate_content/certificates/".
    echo Try a different current directory than this:
    pwd
    exit 0
fi

if ! [[ -d "certificate_content/parts" ]]
then
    autohelp
    echo NO SUB-DIRECTORY "certificate_content/parts/".
    echo Try a different current directory than this:
    pwd
    exit 0
fi

if [ "${1}" == "newcas" ]
then
    echo certlocal newdbase
    certlocal newdbase
    echo certlocal newhostca -a ${AUTHORITY} -f ${FQDNS} -o ${OTHER_SUBDOMAINS} -v ${HOSTVALIDITY_WEEKS} -t ${HOSTTAG}
    certlocal newhostca -a ${AUTHORITY} -f ${FQDNS} -o ${OTHER_SUBDOMAINS} -v ${HOSTVALIDITY_WEEKS} -t ${HOSTTAG}
    echo certlocal newclientca -a ${AUTHORITY} -u ${CLIENTUSERS} -c ${CLIENTHOSTS} -v ${CLIENTVALIDITY_WEEKS} -t ${CLIENTTAG}
    certlocal newclientca -a ${AUTHORITY} -u ${CLIENTUSERS} -c ${CLIENTHOSTS} -v ${CLIENTVALIDITY_WEEKS} -t ${CLIENTTAG}
cat <<EOF
    Databse containing hostca and clientca for ${AUTHORITY} has been created.
EOF
elif [ "${1}" == "certpubs" ]
then
    echo certlocal certifyclient temppubs/id_tunnel_admin -u tunneladmin relay
    certlocal certifyclient temppubs/id_tunnel_admin -u tunneladmin relay
    echo certlocal certifyclient temppubs/guava_relay_guanabana -u relay
    certlocal certifyclient temppubs/guava_relay_guanabana -u relay
    echo certlocal certifyclient temppubs/lemon_relay_guanabana -u relay
    certlocal certifyclient temppubs/lemon_relay_guanabana -u relay
    echo certlocal certifyclient temppubs/mangobiche_relay_guanabana -u relay
    certlocal certifyclient temppubs/mangobiche_relay_guanabana -u relay
    echo certlocal certifyclient temppubs/tamarindo_relay_guanabana -u relay
    certlocal certifyclient temppubs/tamarindo_relay_guanabana -u relay
    echo certlocal certifyclient temppubs/platanoverde_relay_guanabana -u relay
    certlocal certifyclient temppubs/platanoverde_relay_guanabana -u relay

elif [ "${1}" == "certserver" ]
then
    echo certserver certifyservers -a ${AUTHORITY} -t ${HOSTTAG} -f ${FQDNS} -o ${OTHER_SUBDOMAINS} -v ${HOSTVALIDITY_WEEKS}
    certserver certifyservers -a ${AUTHORITY} -t ${HOSTTAG} -f ${FQDNS} -o ${OTHER_SUBDOMAINS} -v ${HOSTVALIDITY_WEEKS}
cat <<EOF
    Server host(s) ${FQDNS} (with ${OTHER_SUBDOMAINS}) now certified.
EOF
elif [ "${1}" == "publish" ]
then
    echo certlocal publish -r 
    certlocal publish -r 
else
    autohelp
fi
