#!/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/>.

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

BIN_DIR="$EXEC_DIR/../bin"
DB_DIR=`../bin/dispatcher --show-dir`
ACTION=
NAME=

IFS=$'&'
for KEY_VAL in $QUERY_STRING; do
{
  if [[ ${KEY_VAL:0:7} == "action=" ]]; then
    ACTION="${KEY_VAL:7}"
  elif [[ ${KEY_VAL:0:5} == "name=" ]]; then
    NAME=`echo "${KEY_VAL:5}" | $BIN_DIR/uncgi`
  fi
}; done
unset IFS

REQUEST_METHOD=
echo "Content-Type: text/html; charset=utf-8"
echo
if [[ ! ${NAME:0:9} == "Template:" ]]; then
{
  echo -e "\
<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n\
	\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n\
<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n\
<head>\n\
  <meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" lang=\"en\"></meta>\n\
  <title>OSM3S Template</title>\n\
</head>\n\
<body>\n\
\n\
<p>Template name &quot;$NAME&quot; doesn't start with &quot;Template:&quot;.</p>\n\
"
  exit 0
}; fi

if [[ $ACTION == "show" ]]; then
{
  echo -e "\
<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n\
	\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n\
<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n\
<head>\n\
  <meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" lang=\"en\"></meta>\n\
  <title>OSM3S Template</title>\n\
</head>\n\
<body>\n\
\n\
<h1>Template &quot;$NAME&quot;</h1>\n\
<pre>\n\
"
../bin/escape_xml <"$DB_DIR/templates/$NAME"
echo -e "\
</pre>\n\
</body>\n\
</html>\n\
\n"
};
elif [[ $ACTION == "fetch" ]]; then
{
  echo -e "\
<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n\
	\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n\
<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n\
<head>\n\
  <meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" lang=\"en\"></meta>\n\
  <title>OSM3S Template</title>\n\
</head>\n\
<body>\n\
\n\
<h1>Template &quot;$NAME&quot;</h1>\n\
<pre>\n\
"
  wget -O "$DB_DIR/templates/$NAME" "http://wiki.openstreetmap.org/w/index.php?action=raw&title=$NAME"
  echo -e "\
</pre>\n\
</body>\n\
</html>\n\
\n"
};
fi
