#!/bin/bash

# Set up the ability to do test suites.  This should only be run from the current working directory!

# Author: Daniel Kahn Gillmor <dkg@fifthhorseman.net>

set -e

if [ -z "$TEST_HOST" ]; then
    printf "You need to set the TEST_HOST environment variable\n" >&2
    exit 1
fi

if [ . != "$(dirname "$0")" ]; then
    printf "You should only run this mod-gnutls test suite from the t/ directory of the mod_gnutls source.\n" >&2
    exit 1
fi

genkey() {
    dir="$1"
    uid="$2"

    mkdir -p "$dir"
    chmod 0700 "$dir"

    certtool --generate-privkey > "$dir"/secret.key
    PEM2OPENPGP_EXPIRATION=86400 PEM2OPENPGP_USAGE_FLAGS=authenticate,certify pem2openpgp "$uid" < "$dir"/secret.key | GNUPGHOME="$dir" gpg --import
    fpr="$(GNUPGHOME="$dir" gpg --with-colons --list-secret-keys --fingerprint | grep ^fpr: | cut -f 10 -d :)"

    # set ultimate ownertrust on my own secret key:
    printf "%s:6:\n"  "$fpr" | GNUPGHOME="$dir" gpg --import-ownertrust

    GNUPGHOME="$dir" ~/src/monkeysphere/msva-perl/openpgp2x509 "$uid" > "$dir"/x509.pem
}

genkey "$PWD/authority" "Testing Authority"
genkey "$PWD/server" "https://${TEST_HOST}"
genkey "$PWD/client" "Test User <test0@modgnutls.test>"

certtool -q --load-privkey=server/secret.key  --template=server.template > server/server.req


mkdir -p logs cache outputs
touch setup.done
