#!/bin/bash

set -e

# Check if Nginx is installed (see the test's dependencies).
# if not we are on an arch where LuaJIT is not available.
if [ ! -x /usr/sbin/nginx ]; then
	echo "Nginx is not installed, assuming we are on arch where LuaJIT is not available"
	exit 0
fi

rm /etc/nginx/sites-enabled/default
cat <<EOF > "/etc/nginx/sites-enabled/lua"
server {
	listen 80 default_server;

	location /ping {
		content_by_lua 'ngx.say("PONG")';
	}
}
EOF

nginx -t
invoke-rc.d nginx restart
curl --silent --fail -o /dev/null -w "response_code: %{http_code}\n" http://127.0.0.1/ping
