#!/usr/bin/env bash

# Copyright 2008, 2009, 2010, 2011, 2012 Roland Olbricht
#
# This file is part of Overpass_API.
#
# Overpass_API is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# Overpass_API is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Overpass_API.  If not, see <http://www.gnu.org/licenses/>.

# $1 - remote source
# $2 - local destination
fetch_file()
{
  wget -q -O "$2" "$1"
};

EXEC_DIR="`dirname $0`/"
if [[ ! ${EXEC_DIR:0:1} == "/" ]]; then
{
  EXEC_DIR="`pwd`/$EXEC_DIR"
};
fi

BIN_DIR="$EXEC_DIR/../bin"

# Set BASE_URL to your own server's external name
BASE_URL="http://localhost/api"

echo "Content-Type: text/html; charset=utf-8"
echo
echo -e "\
<html>\n\
<head>\n\
  <meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" lang=\"en\"></meta>\n\
  <title>Server status</title>\n\
</head>\n\
<body>\n\
  <h1>Outside view on the server</h1>\n\
  <h2>Empty, simple request</h2>\n\
<pre>\n"

fetch_file "$BASE_URL/interpreter?data=<print/>" "/tmp/ping.xml"
$BIN_DIR/escape_xml <"/tmp/ping.xml"

echo -e "</pre>\
  <h2>Meta request</h2>\n\
<pre>\n"

fetch_file "$BASE_URL/interpreter?data=<query type='way'><has-kv k='name' v='Lipkenskothen'/></query><print mode='meta'/><recurse type='way-node'/><print mode='meta'/>" "/tmp/ping.xml"
$BIN_DIR/escape_xml <"/tmp/ping.xml"

echo -e "</pre>\
  <h2>Area request</h2>\n\
<pre>\n"

fetch_file "$BASE_URL/interpreter?data=<coord-query lat='51.25' lon='7.15'/><print mode='ids_only'/>" "/tmp/ping.xml"
$BIN_DIR/escape_xml <"/tmp/ping.xml"

echo -e "</pre>\
</body>\n\
</html>\n"
