#!/usr/bin/env bash
# Export converter for web example

type="$1"
if [ "$type" = "authorUrl" ] ; then
   author="$2"
   authorText="$3"


   declare -A authorUrl
   authorUrl=(
      ["Amund Kvalbein"]="https://simula.no/people/amundk"
      ["Andreas Jungmaier"]="http://www.tdr.wiwi.uni-due.de/team/ehemalige-mitarbeiter/andreas-jungmaier/"
      ["Carsten Hohendorf"]="http://www.tdr.wiwi.uni-due.de/team/ehemalige-mitarbeiter/carsten-hohendorf/"
      ["Christian Henke"]="http://www.av.tu-berlin.de/menue/team/former_fellows/christian_henke/"
      ["Daniel Bialla"]="https://www.uni-due.de/zim/organisation/mitarbeiterInnen/mitarbeiter_details.php?id=104"
      ["Dieter Huth"]="https://www.uni-due.de/zim/organisation/mitarbeiterInnen/mitarbeiter_details.php?id=35"
      ["Ernst Gunnar Gran"]="https://simula.no/people/ernstgr"
      ["Erwin Paul Rathgeb"]="http://www.tdr.wiwi.uni-due.de/team/erwin-p-rathgeb/"
      ["Fu Fa"]="http://cist.hainu.edu.cn/news/showteacherdetail.do?userId=44"
      ["Hakim Adhari"]="http://www.tdr.wiwi.uni-due.de/team/hakim-adhari/"
      ["Irene Rüngeler"]="https://www.fh-münster.de/fb2/personen/mitarbeiter/ruengeler/"
      ["Janardhan R. Iyengar"]="http://www.fandm.edu/janardhan-iyengar"
      ["Jobin Pulinthanath"]="http://www.tdr.wiwi.uni-due.de/team/ehemalige-mitarbeiter/jobin-pulinthanath/"
      ["Johannes Formann"]="http://dc.informatik.uni-essen.de/Formann/all/"
      ["Markus Packeiser"]="http://www.tdr.wiwi.uni-due.de/team/markus-packeiser/"
      ["Martin Becke"]="http://www.familie-becke.de/"
      ["Michael Striewe"]="http://paluno.uni-due.de/das-institut/team/wissenschaftliche-mitarbeiter/michael-striewe/"
      ["Michael Tüxen"]="https://www.fh-muenster.de/fb2/personen/professoren/tuexen/" 
      ["Özgü Alay"]="http://simula.no/people/ozgu"
      ["Paul D. Amer"]="http://www.eecis.udel.edu/~amer/"
      ["Randall R. Stewart"]="http://people.freebsd.org/~rrs/"
      ["Robin Seggelmann"]="http://www.robin-seggelmann.de/"
      ["Sebastian Werner"]="http://www.tdr.wiwi.uni-due.de/team/sebastian-werner/"
      ["Simone Ferlin-Oliveira"]="http://simula.no/people/ferlin"
      ["Thomas Dreibholz"]="https://www.nntb.no/~dreibh"
      ["Xing Zhou"]="http://cist.hainu.edu.cn/news/showteacherdetail.do?userId=66"
      # [""]=""
   )

   
   url="${authorUrl[` echo "$author" | sed -e "s/&nbsp;/ /g"`]}"
   if [ "$url" != "" ] ; then
      echo -n "<a href=\"$url\">$authorText</a>"
   else
      echo >&2 "NOTE: No URL for author $author"
      echo -n "$authorText"
   fi


elif [ "$type" = "location" ] ; then
   place="$2"
   city=""
   region=""
   country=""
   url=""

   if [[ "$place" =~ ^([^,]*)(,[ \t])([^/]*)/(.*)$ ]] ; then   # City, Region/Country
       q=${BASH_REMATCH[0]}
       city=${BASH_REMATCH[1]}
       region=${BASH_REMATCH[3]}
       country=${BASH_REMATCH[4]}
   elif [[ "$place" =~ ^([^,]*)([,/][ \t]*)(.*)$ ]] ; then   # City/Country
       city=${BASH_REMATCH[1]}
       region=""
       country=${BASH_REMATCH[3]}
   elif [[ "$place" =~ ^([^,/]*)$ ]] ; then   # Country only (e.g. Singapore)
       country=${BASH_REMATCH[1]}
   else
       echo >&2 "NOTE: Cannot interpret place $place"
   fi

   
   if [ "$country" != "" ] ; then
      s=""
      if [ "$city" != "" ] ; then
         s="$city, "
      fi
      if [ "$region" != "" ] ; then
         s="$s$region, "
      fi
      s="$s$country"
      url="https://www.openstreetmap.org/search?query=$s"
   fi

   
   if [ "$url" != "" ] ; then
      echo -n "<a href=\"$url\">$place</a>"
   else
      echo >&2 "NOTE: No URL for place $place"
      echo -n "$place"
   fi
fi
