#!/bin/bash

set -e

srcdir="$(pwd)"

cd debian/tests/docker

# build image
docker build --rm -t cyrus-test -f Dockerfile.sid .

# get local repository path
source_list="/etc/apt/sources.list.d/autopkgtest.list"
p=$(perl -pe 's#.*file://([^ ]+).*#$1#' $source_list)

# run it
#
# The Cassandane suite comes from this source tree, not from the archived
# https://github.com/cyrusimap/cassandane repository; bind-mount it into
# the container and let _cassandane() copy it to a writable location.
docker run --rm \
       -v ${source_list}:${source_list} \
       -v ${p}:${p} \
       -v "${srcdir}/cassandane":/srv/cassandane.src:ro \
       -e BUILD_CYRUS_FROM_SOURCE="no" \
       -e CASSANDANESRC="/srv/cassandane.src" \
       -e CASSANDANEOPTS="Cyrus::ImapTest Cyrus::ACL Cyrus::Delete Cyrus::MIME Cyrus::Search Cyrus::URLAuth Cyrus::XApplePushService" \
       cyrus-test
