#!/bin/sh
# autopkgtest: Run service and tests binding
set -eu

output="$(mktemp)"

cleanup()
{
    status=$?
    trap - 0
    /etc/init.d/shoelaces stop || true
    rm -f "$output"
    exit "$status"
}

trap cleanup 0

/etc/init.d/shoelaces start
/etc/init.d/shoelaces status

if ! wget \
    --content-on-error \
    --output-document="$output" \
    --retry-connrefused \
    --server-response \
    --timeout=10 \
    --tries=5 \
    http://127.0.0.1:8081/
then
    echo "Shoelaces did not return a successful HTTP response" >&2
    if [ -s "$output" ]; then
        echo "Response body:" >&2
        cat "$output" >&2
    fi
    /etc/init.d/shoelaces status || true
    if command -v journalctl >/dev/null; then
        journalctl --no-pager --unit=shoelaces.service --lines=100 || true
    fi
    exit 1
fi

echo "Shoelaces returned a successful HTTP response"
