#!/bin/sh

set -eu

cd "${AUTOPKGTEST_TMP:-/tmp}"

cat > app.rb <<EOF
require "sinatra"
get "/" do
  "HELLO"
end
EOF

ruby app.rb -p 9999 &
pid=$!

trap "kill $pid && wait" INT TERM EXIT

tries=0
while [ $tries -lt 10 ]; do
  sleep 1
  set -x
  if curl --silent --fail -o output http://localhost:9999/; then
    if grep HELLO output; then
      exit 0
    fi
  fi
  set +x
  tries=$((tries + 1))
done

echo "Timeout out waiting for expected output"
exit 1
