#!/bin/sh
# autopkgtest check: Builds a small application against libcairo2-dev, checking
# if it compiles, links and runs successfully.
# Author: Rafał Cieślak <rafalcieslak256@ubuntu.com> 

set -e

WORKDIR=$(mktemp -d)
trap 'rm -rf "$WORKDIR"' 0 INT QUIT ABRT PIPE TERM
cd "$WORKDIR"

if [ -n "${DEB_HOST_GNU_TYPE:-}" ]; then
    CROSS_COMPILE="$DEB_HOST_GNU_TYPE-"
else
    CROSS_COMPILE=
fi

cat <<EOF > build_test.c
/*
   The source code I used here is the actual cairo's
   HelloWorld from it's FAQ on cairographics.org.
   It should generate a .png file with some text inside. 
*/

#include <cairo.h>
int
main (int argc, char *argv[])
{
        cairo_surface_t *surface =
            cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 240, 80);
        cairo_t *cr =
            cairo_create (surface);

        cairo_select_font_face (cr, "serif", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
        cairo_set_font_size (cr, 32.0);
        cairo_set_source_rgb (cr, 0.0, 0.0, 1.0);
        cairo_move_to (cr, 10.0, 50.0);
        cairo_show_text (cr, "Hello, world");

        cairo_destroy (cr);
        cairo_surface_write_to_png (surface, "hello.png");
        cairo_surface_destroy (surface);
        return 0;
}
EOF

# Deliberately word-splitting, that's how pkg-config works:
# shellcheck disable=SC2046
"${CROSS_COMPILE}gcc" -o build_test build_test.c $("${CROSS_COMPILE}pkg-config" --cflags --libs cairo)
echo "build: OK"
[ -x build_test ]
./build_test
# Also checking if the generated image is actually present
[ -e hello.png ]
echo "run: OK"

