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

BUF="$QUERY_STRING&"

SKETCH_PARAMS=
BRIM_PARAMS=

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

BIN_DIR="$EXEC_DIR/../bin"

IFS=$'&'
for KEY_VAL in $QUERY_STRING; do
{
  if [[ ${KEY_VAL:0:8} == "network=" ]]; then
  {
    NETWORK="${KEY_VAL:8}"
    NETWORK_=`echo "${KEY_VAL:8}" | $BIN_DIR/uncgi`
  };
  elif [[ ${KEY_VAL:0:4} == "ref=" ]]; then
  {
    REF="${KEY_VAL:4}"
    REF_=`echo "${KEY_VAL:4}" | $BIN_DIR/uncgi`
  };
  elif [[ ${KEY_VAL:0:10} == "pivot-lon=" ]]; then
    SKETCH_PARAMS="$SKETCH_PARAMS --pivot-lon=${KEY_VAL:10}"
  elif [[ ${KEY_VAL:0:6} == "scale=" ]]; then
    SKETCH_PARAMS="$SKETCH_PARAMS --scale=${KEY_VAL:6}"
  elif [[ ${KEY_VAL:0:10} == "font-size=" ]]; then
    SKETCH_PARAMS="$SKETCH_PARAMS --stop-font-size=${KEY_VAL:10}"
  elif [[ ${KEY_VAL:0:6} == "debug=" ]]; then
    DEBUG="${KEY_VAL:6}"
  fi
}; done
unset IFS

BASEDIR=`mktemp -d /tmp/osm3s_draw_line_XXXXXX`

echo -e "\
<osm-script> \
 \
<union> \
  <query type=\"relation\"> \
    <has-kv k=\"network\" v=\"$NETWORK_\"/> \
    <has-kv k=\"ref\" v=\"$REF_\"/> \
  </query> \
  <recurse type=\"relation-node\" into=\"__\"/> \
  <recurse type=\"relation-way\"/> \
  <recurse type=\"way-node\"/> \
</union> \
<print mode=\"body\"/> \
 \
</osm-script>
" >$BASEDIR/request.1

if [[ -z $REF ]]; then
{                     
  echo "Content-Type: text/plain; charset=utf-8"
  echo                                          
  echo "An empty value for ref is not allowed."  

  exit 0
};    
fi      

CORRESPONDENCES=`$BIN_DIR/bbox_brim_query --only-corrs $BRIM_PARAMS`

if [[ $CORRESPONDENCES -gt 0 ]]; then
{
  echo "Content-Type: text/plain; charset=utf-8"
  echo                                          
  echo "Correspondences are for the geographical map currently not supported."  

  exit 0
};
else
{
  if [[ $DEBUG == "full-query" ]]; then
  {
    echo "Content-Type: text/plain; charset=utf-8"
    echo

    REQUEST_METHOD=
    $BIN_DIR/osm3s_query --no-mime <$BASEDIR/request.1 2>&1

    echo
    echo "$BIN_DIR/draw_route_svg $SKETCH_PARAMS"
  };
  else
  {
    REQUEST_METHOD=
    $BIN_DIR/osm3s_query --quiet --no-mime <$BASEDIR/request.1 >$BASEDIR/answer.1
  };
  fi;

  echo "Content-Type: image/svg+xml; charset=utf-8"
  echo

  $BIN_DIR/draw_route_svg $SKETCH_PARAMS <$BASEDIR/answer.1
};
fi
