#!/bin/sh

export LC_ALL=C.UTF-8

# hack for lxc
IP=`ip route get 1.1.1.1 | sed -n -e's/.*src //; s/ .*//; p; q'`
echo ">>>> IP address is $IP"

HOSTNAME=`cat /etc/hosts| grep '127.0.1.1' | awk '{print $NF; exit}'`
echo ">>>> Hostname was: $HOSTNAME"

if [ -z $HOSTNAME ]; then
    HOSTNAME=autopkgtest
    hostname $HOSTNAME
    echo $HOSTNAME > /etc/hostname
fi

echo "$IP $HOSTNAME.debci $HOSTNAME" >> /etc/hosts

echo ">>>> /etc/hosts now has:"
cat /etc/hosts

/usr/sbin/dscreate from-file debian/tests/debci.inf 2>&1

for subsys in CA KRA OCSP TKS; do
    pkispawn --debug -s $subsys -f debian/tests/deploy.cfg
    if [ $? != 0 ]; then
        lower=`echo $subsys | tr '[:upper:]' '[:lower:]'`
        echo ">>>> $subsys spawn failed:"
        cat /var/log/pki/pki-$lower-spawn.*.log
        exit 1
    fi
done

for subsys in TKS OCSP KRA CA; do
    pkidestroy --debug -i pki-tomcat -s $subsys
    if [ $? != 0 ]; then
        lower=`echo $subsys | tr '[:upper:]' '[:lower:]'`
        echo ">>>> $subsys destroy failed:"
        cat /var/log/pki/pki-$lower-destroy.*.log
        exit 0
    fi
done

echo ">>>> All done!"
