#!/bin/sh

set -efu

if [ -z "$AUTOPKGTEST_TMP" ]; then
    AUTOPKGTEST_TMP=tmp
    mkdir -p ${AUTOPKGTEST_TMP}
fi

SRCDIR=$(pwd)

cp -rv ${SRCDIR}/debian/tests ${AUTOPKGTEST_TMP}
cd ${AUTOPKGTEST_TMP}/tests

#get cert fingerprint
fingerprint=`openssl x509 -noout -fingerprint -sha256 -inform pem -in /etc/dovecot/private/dovecot.pem | cut -d '=' -f2`

#start dovecot if its not running
if [ ! -f /run/dovecot/master.pid ]; then
    dovecot
fi
sleep 5
# wait for dovecot to start running
for i in `seq 1 5`; do
	doveadm -f flow instance list | grep -q 'running=yes'
	if [ $? -eq 0 ]; then
		break
	fi
	sleep 5
done
sleep 1
python3 -c "import imaplib2; imaplib2.IMAP4_SSL(host='localhost')"
dovecot stop
