#!/bin/bash

set -e

# Start the service
service inetsim start

# Wait for the HTTP server to be up
max_date=$(date +%s -d "now + 1 minute")
while true; do
    curl -s --head localhost:80 >/dev/null && break
    if [ $(date +%s) -ge $max_date ]; then
        echo "Timeout waiting for HTTP server!" >&2
        if [ -x /usr/bin/journalctl ]; then
            journalctl -u inetsim | tail -n 100
        fi
	if [ -d /var/log/inetsim ]; then
            tail -n 100 /var/log/inetsim/*.log
        fi
        exit 1
    fi
    sleep 5
done

# Validate HTTP server response
diff -u <(curl -sS localhost:80) /usr/share/inetsim/data/http/fakefiles/sample.html
