#!/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=
STYLE_DIR=/opt/pt_diagrams/

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

BIN_DIR="$EXEC_DIR/../bin"

IFS=$'&'
NETWORK=
NETWORK_=
REF=
REF_=
OPERATOR=
OPERATOR_=
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:9} == "operator=" ]]; then
  {
    OPERATOR="${KEY_VAL:9}"
    OPERATOR_=`echo "${KEY_VAL:9}" | $BIN_DIR/uncgi`
  };
  elif [[ ${KEY_VAL:0:6} == "width=" && -n "${KEY_VAL:6}" ]]; then
    SKETCH_PARAMS="$SKETCH_PARAMS --width=${KEY_VAL:6}"
  elif [[ ${KEY_VAL:0:7} == "height=" && -n "${KEY_VAL:7}" ]]; then
    SKETCH_PARAMS="$SKETCH_PARAMS --height=${KEY_VAL:7}"
  elif [[ ${KEY_VAL:0:10} == "font-size=" && -n "${KEY_VAL:10}" ]]; then
    SKETCH_PARAMS="$SKETCH_PARAMS --stop-font-size=${KEY_VAL:10}"
  elif [[ ${KEY_VAL:0:11} == "force-rows=" && -n "${KEY_VAL:11}" ]]; then
    SKETCH_PARAMS="$SKETCH_PARAMS --rows=${KEY_VAL:11}"
  elif [[ ${KEY_VAL:0:6} == "style=" && -n "${KEY_VAL:6}" ]]; then
  {
    SKETCH_PARAMS="$SKETCH_PARAMS --options=$STYLE_DIR/sketch-line.${KEY_VAL:6}"
    BRIM_PARAMS="$BRIM_PARAMS --options=$STYLE_DIR/sketch-line.${KEY_VAL:6}"
  };
  elif [[ ${KEY_VAL:0:16} == "correspondences=" && -n "${KEY_VAL:16}" ]]; then
  {
    SKETCH_PARAMS="$SKETCH_PARAMS --walk-limit=${KEY_VAL:16}"
    BRIM_PARAMS="$BRIM_PARAMS --size=${KEY_VAL:16}"
  };
  elif [[ ${KEY_VAL:0:18} == "max-cors-per-line=" && -n "${KEY_VAL:18}" ]]; then
    SKETCH_PARAMS="$SKETCH_PARAMS --max-correspondences-per-line=${KEY_VAL:18}"
  elif [[ ${KEY_VAL:0:15} == "max-cors-below=" && -n "${KEY_VAL:15}" ]]; then
    SKETCH_PARAMS="$SKETCH_PARAMS --max-correspondences-below=${KEY_VAL:15}"
  elif [[ ${KEY_VAL:0:6} == "debug=" && -n "${KEY_VAL:6}" ]]; then
  {
    SKETCH_PARAMS="$SKETCH_PARAMS --debug=${KEY_VAL:6}"
    DEBUG="${KEY_VAL:6}"
  };
  fi
}; done
unset IFS

BASEDIR=`mktemp -d /tmp/osm3s_sketch_line_XXXXXX`

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      

if [[ $DEBUG == "yes" ]]; then
{
  echo "Content-Type: text/plain; charset=utf-8"
  echo
  echo $BIN_DIR/bbox_brim_query --network=\"$NETWORK_\" --ref=\"$REF_\" --operator=\"$OPERATOR_\" $BRIM_PARAMS ">"$BASEDIR/request.1
  echo
};
fi

$BIN_DIR/bbox_brim_query --network="$NETWORK_" --ref="$REF_" --operator="$OPERATOR_" $BRIM_PARAMS >$BASEDIR/request.1

REQUEST_METHOD=

if [[ $DEBUG == "yes" ]]; then
{
  cat <$BASEDIR/request.1
  echo
  echo $BIN_DIR/osm3s_query --verbose "<"$BASEDIR/request.1 ">"$BASEDIR/answer.1
  echo
  $BIN_DIR/osm3s_query --verbose <$BASEDIR/request.1 >$BASEDIR/answer.1 2>$BASEDIR/log.1
  echo
  cat <$BASEDIR/log.1
  cat <$BASEDIR/answer.1
  echo $BIN_DIR/sketch_route_svg --network=\"$NETWORK_\" --ref=\"$REF_\" $SKETCH_PARAMS <$BASEDIR/answer.1
  echo
};
else
  $BIN_DIR/osm3s_query --quiet <$BASEDIR/request.1 >$BASEDIR/answer.1
fi;

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

$BIN_DIR/sketch_route_svg --network="$NETWORK_" --ref="$REF_" $SKETCH_PARAMS <$BASEDIR/answer.1
