#!/bin/sh

CA_INSTALL=0
KRA_INSTALL=0
OCSP_INSTALL=0
TKS_INSTALL=0
TPS_INSTALL=0

# 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

setup-ds --silent --\
	General.FullMachineName=$HOSTNAME.debci\
	General.SuiteSpotUserID=dirsrv\
	General.SuiteSpotGroup=dirsrv\
	slapd.ServerPort=389\
	slapd.ServerIdentifier=pki-tomcat\
	slapd.Suffix=dc=example,dc=com\
	slapd.RootDN="cn=Directory Manager"\
	slapd.RootDNPwd=Secret123

pkispawn -s CA -f debian/tests/deploy.cfg && CA_INSTALL=1
pkispawn -s KRA -f debian/tests/deploy.cfg && KRA_INSTALL=1
pkispawn -s OCSP -f debian/tests/deploy.cfg && OCSP_INSTALL=1
pkispawn -s TKS -f debian/tests/deploy.cfg && TKS_INSTALL=1
pkispawn -s TPS -f debian/tests/deploy.cfg && TPS_INSTALL=1

if [ $TPS_INSTALL = "1" ]; then
    pkidestroy -i pki-tomcat -s TPS || cat /var/log/pki-tps-destroy.*.log
fi

if [ $TKS_INSTALL = "1" ]; then
    pkidestroy -i pki-tomcat -s TKS || cat /var/log/pki-tks-destroy.*.log
fi

if [ $OCSP_INSTALL = "1" ]; then
    pkidestroy -i pki-tomcat -s OCSP || cat /var/log/pki-ocsp-destroy.*.log
fi

if [ $KRA_INSTALL = "1" ]; then
    pkidestroy -i pki-tomcat -s KRA || cat /var/log/pki-kra-destroy.*.log
fi

if [ $CA_INSTALL = "1" ]; then
    pkidestroy -i pki-tomcat -s CA || cat /var/log/pki-ca-destroy.*.log
fi
