#!/usr/bin/env bash

# Copyright 2008, 2009, 2010, 2011, 2012 Roland Olbricht
#
# This file is part of PT_Diagrams.
#
# PT_Diagrams 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.
#
# PT_Diagrams 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 PT_Diagrams.  If not, see <http://www.gnu.org/licenses/>.

ARG_1=`echo $QUERY_STRING | awk -F [=,\&] '{ print $1; }'`
ARG_2=`echo $QUERY_STRING | awk -F [=,\&] '{ print $2; }'`
ARG_3=`echo $QUERY_STRING | awk -F [=,\&] '{ print $3; }'`
ARG_4=`echo $QUERY_STRING | awk -F [=,\&] '{ print $4; }'`
ARG_5=`echo $QUERY_STRING | awk -F [=,\&] '{ print $5; }'`
ARG_6=`echo $QUERY_STRING | awk -F [=,\&] '{ print $6; }'`
ARG_7=`echo $QUERY_STRING | awk -F [=,\&] '{ print $7; }'`
ARG_8=`echo $QUERY_STRING | awk -F [=,\&] '{ print $8; }'`

BASEDIR=`mktemp -d /tmp/osm3s_sketch_route_XXXXXX`

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

if [[ ( $ARG_2 == "backspace" ) || ($ARG_2 == "backtime") || ($ARG_2 == "debug") ]]; then
{
echo -e "\
<osm-script> \
 \
<union> \
  <id-query type=\"relation\" ref=\"$ARG_1\"/> \
  <recurse type=\"relation-node\"/> \
</union> \
<print mode=\"body\"/> \
 \
</osm-script> \
" >$BASEDIR/req
};
elif [[ -n $ARG_2 ]]; then
{
echo -e "\
<osm-script> \
 \
<union> \
  <union> \
    <id-query type=\"relation\" ref=\"$ARG_1\"/> \
    <id-query type=\"relation\" ref=\"$ARG_2\"/> \
  </union> \
  <recurse type=\"relation-node\"/> \
</union> \
<print mode=\"body\"/> \
 \
</osm-script> \
" >$BASEDIR/req
};
else
{
echo -e "\
<osm-script> \
 \
<union> \
  <id-query type=\"relation\" ref=\"$ARG_1\"/> \
  <recurse type=\"relation-node\"/> \
</union> \
<print mode=\"body\"/> \
 \
</osm-script> \
" >$BASEDIR/req
};
fi

REQUEST_METHOD=
../bin/osm3s_query --quiet --no-mime <$BASEDIR/req >$BASEDIR/result

if [[ $ARG_2 == "backspace" ]]; then
{
  echo "Content-Type: image/svg+xml; charset=utf-8"
  echo

  #gunzip </tmp/nodes_csv_result.2 | ../bin/sketch_route_svg --backspace
  ../bin/sketch_route_svg --backspace <$BASEDIR/result
};
elif [[ $ARG_2 == "backtime" ]]; then
{
  echo "Content-Type: image/svg+xml; charset=utf-8"
  echo

  #gunzip </tmp/nodes_csv_result.2 | ../bin/sketch_route_svg --backtime
  ../bin/sketch_route_svg --backtime <$BASEDIR/result
};
elif [[ ($ARG_2 == "debug") || ($ARG_3 == "debug") ]]; then
{
  echo "Content-Type: text/plain"
  echo
  cat <$BASEDIR/result
};
else
{
  echo "Content-Type: image/svg+xml; charset=utf-8"
  echo

  #gunzip </tmp/nodes_csv_result.2 | ../bin/sketch_route_svg
  ../bin/sketch_route_svg <$BASEDIR/result
};
fi
