#!/bin/sh
set -Ceu
cp -a tests/template-pkg "$AUTOPKGTEST_TMP/canary_path"
cd "$AUTOPKGTEST_TMP/canary_path"

export so_name=libfoo.so.5
export so_file=$so_name
export so_link=$so_name

DEB_RULES_REQUIRES_ROOT=no \
DEB_BUILD_OPTIONS=noopt debian/rules binary

cd debian

DEB_ADA_SOURCE_DIR=usr/share/ada/adainclude
DEB_GNAT_PROJECT_DIR=usr/share/gpr

DEB_HOST_MULTIARCH=`dpkg-architecture -qDEB_HOST_MULTIARCH`
DEB_LIB_DIR=usr/lib/$DEB_HOST_MULTIARCH
DEB_ADA_LIB_INFO_DIR=$DEB_LIB_DIR/ada/adalib

DEB_GNAT_VERSION=`gnatmake --version | sed 's/.* \([0-9]\+\)\.[0-9.]\+$/\1/;q'`

echo 'Sources'
test -e libfoo-dev/$DEB_ADA_SOURCE_DIR/foo/foo.adb
test -e libfoo-dev/$DEB_ADA_SOURCE_DIR/foo/foo.ads

echo 'Static archive'
test -e libfoo-dev/$DEB_LIB_DIR/libfoo.a

echo 'Project'
test -e libfoo-dev/$DEB_GNAT_PROJECT_DIR/foo.gpr

echo 'ALI files'
test -e libfoo-dev/$DEB_ADA_LIB_INFO_DIR/foo/foo.ali

echo 'debug-prefix-map'
# There should be no match, dh_ada_library replaces
#       -fdebug-prefix-map=$AUTOPKGTEST_TMP/canary_path=/usr/src/libfoo-1
# with
#       -fdebug-prefix-map=/build=/usr/src/libfoo-1
status=0
grep /canary_path lib*-dev/$DEB_ADA_LIB_INFO_DIR/*/*.ali libfoo-dev/$DEB_GNAT_PROJECT_DIR/foo.gpr  || status=$?
test $status = 1

echo 'Shared library'
test -e libfoo5/$DEB_LIB_DIR/$so_file

echo 'Development symbolic link'
test -h libfoo-dev/$DEB_LIB_DIR/libfoo.so
test $(readlink -v libfoo-dev/$DEB_LIB_DIR/libfoo.so) = $so_link

echo 'ada:Depends'
grep "^Depends: gnat (<< $(($DEB_GNAT_VERSION + 1))), gnat (>= $DEB_GNAT_VERSION), gnat-$DEB_GNAT_VERSION, gnat-$DEB_GNAT_VERSION-[0-9a-f]\{8\}, libfoo5 (= 1)\$" libfoo-dev/DEBIAN/control

echo 'ada:Provides'
grep '^Provides: libfoo-dev-[0-9a-f]\{8\}$' libfoo-dev/DEBIAN/control

echo 'OK'
