#!/bin/bash
# Copyright © 2019-2026 Collabora Ltd.
# SPDX-License-Identifier: BSD-3-Clause
# (See debian/copyright)

set -e
set -u
set -x

# Test that we can cross-compile a simple project using Autotools.

if [ -z "${DEB_HOST_ARCH-}" ] || [ "$DEB_HOST_ARCH" = "${DEB_BUILD_ARCH-}" ]; then
    echo "1..0 # SKIP DEB_HOST_ARCH not configured"
    exit 0
fi

echo "1..1"

tmpdir="$(mktemp -d)"
cleanup () {
    rm -fr "$tmpdir"
}
trap cleanup 0 INT QUIT ABRT PIPE TERM
top_srcdir="$(pwd)"

DEB_BUILD_GNU_TYPE="$(dpkg-architecture -a"$DEB_HOST_ARCH" -qDEB_BUILD_GNU_TYPE)"
DEB_HOST_GNU_TYPE="$(dpkg-architecture -a"$DEB_HOST_ARCH" -qDEB_HOST_GNU_TYPE)"

cp -a examples/autotools "$tmpdir/srcdir"

cd "$tmpdir/srcdir"
NOCONFIGURE=1 ./autogen.sh

mkdir "$tmpdir/builddir"
cd "$tmpdir/builddir"
../srcdir/configure --build="$DEB_BUILD_GNU_TYPE" --host="$DEB_HOST_GNU_TYPE"

make -C "$tmpdir/builddir" V=1

file "$tmpdir/builddir/hello"
"$top_srcdir/tests/assert-correct-ldso.sh" "$DEB_HOST_ARCH" "$tmpdir/builddir/hello"

make -C "$tmpdir/builddir" V=1 clean

echo "ok 1"

# vim:set sw=4 sts=4 et ft=sh:
