#!/bin/sh
#
# Copyright © 2019 Chris Lamb <lamby@debian.org
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, you can find it on the World Wide
# Web at http://www.gnu.org/copyleft/gpl.html, or write to the Free
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
# MA 02110-1301, USA.

set -eu

CACHE_DIR="${1}"
PIPELINE="${2}"

Checksum_input () {
	# Local files that, if changed, should result in a rebuild of the test
	# packages.
	find \
		t/bin/build-test-packages \
		lib/Test/ \
		-type f -print0 | sort -z | xargs -0 sha1sum

	# Rebuild if any build-dependency or installed package changes
	(
		apt -q -y --print-uris build-dep . 2>/dev/null | \
			grep MD5Sum: | cut -d' ' -f2 | cut -d_ -f1-2;
			dpkg -l | awk '{ print $2 "_" $3 }'
	) | sort
}

CHECKSUM="$(Checksum_input | sha1sum | cut -d ' ' -f1)"
CACHE_FILENAME="${CACHE_DIR}/${PIPELINE}-${CHECKSUM}.tar.xz"

# get prequisites early, otherwise tar fails for lack of xz-utils
env DEBIAN_FRONTEND=noninteractive apt-get -q -y -o dir::cache::archives="${CACHE_DIR}" build-dep .

mkdir -p .cache

echo "I: Showing artifacts in .cache" >&2
ls -al .cache >&2

echo "I: Looking for ${CACHE_FILENAME}" >&2

if [ -f "${CACHE_FILENAME}" ]
then
    echo "I: Extracting ${CACHE_FILENAME}" >&2
    rm -rf debian/test-out/packages
    tar xfJ "${CACHE_FILENAME}"
fi

t/bin/build-test-packages

echo "I: Removing obsolete test package artifacts from .cache" >&2
find .cache \
     -maxdepth 1 \
     -type f \
     -regextype posix-egrep \
     -regex "^\.cache/${PIPELINE}-[[:xdigit:]]{40}\.tar\.xz\$" \
     -print \
     -delete

echo "I: Removing old-style artifacts (no pipeline in name) from .cache" >&2
find .cache \
     -maxdepth 1 \
     -type f \
     -regextype posix-egrep \
     -regex '^\.cache/[[:xdigit:]]{40}\.tar\.xz$' \
     -print \
     -delete

echo "I: Creating ${CACHE_FILENAME}" >&2
mkdir -p "$(dirname "${CACHE_FILENAME}")"
tar cfJ "${CACHE_FILENAME}" debian/test-out/packages

cp -v "${CACHE_FILENAME}" test-packages.tar.xz

echo "I: Showing artifacts in .cache" >&2
ls -al .cache >&2
