#!/bin/sh
# Script fuer das Positionsupdate auf der Karte
# Manuel Raeber
# 16. Januar 2004


# Daten- und Scriptverzeichnis
map_dir='/tools/map/'

# Umrechnunhstool Long/Lat -> karthesiche Koordinaten
proj_tool='/usr/local/bin/proj'

# Bildbearbeitungstool
img_tool='/usr/local/bin/convert'

# Ur Karte (ohne Route)
main_map='africa_grinten.tif'

# Zielkarte 100%, unkomprimiert
route_map='route.tif'

# Webkarten 20%,40%,60%,80%,100% in jpg
route_20='r20.jpg'
route_40='r40.jpg'
route_60='r60.jpg'
route_80='r80.jpg'
route_100='r100.jpg'

# Positionsdatei in geogr. Lenge/Breite (fuer proj)
route_longlat='route.gps'

# Tabellendaten mit Datum,Laenge,Breite,Kommentar
route_tab='day_tab.gps'

# Updatedatei von Inmarsat
route_update='pos.gps'

# Route umgerechnet auf Karte in Pixel
route_pixel='route.map'

# HTML Dateien fuer Kartendarstellung
route20_html='r20.html'
route40_html='r40.html'
route60_html='r60.html'
route80_html='r80.html'
route100_html='r100.html'

# HTML Datei für Numerische Standortdarstellung und Tageskommentar
day_html='day_tab.html'

# Zielverzeichnis fuer Webserver der Zielkarte
web_dir='/srv/www/htdocs/fratz.li/'

# Minimaler Abstand der Datums-Beschriftung in Pixel auf der Zielkarte
min_text_dist=70

# Script nur asufuehren, wenn Positionsupdate vorhanden

if test -f $map_dir$route_update
then

# Originalkarte nach Zielkarte kopieren
cp $map_dir$main_map $map_dir$route_map

# route_longlat (nur fuer proj) loeschen (wird aus route_tab neu erzeugt)
rm $map_dir$route_longlat

# Aktuelles Datum und Positionsupdate mit Kommentar an Tagesinfo anhaengen
dat=`date +"%a, %d.%m.%Y"`
echo -e "$dat \t`cat $map_dir$route_update`" >> $map_dir$route_tab

# Day-Tab HTML generieren

echo -e "<html>\n<head>\n<title>Tages-Tabelle</title>\n</head>\n\
        <body text=\"#FFFFFF\" bgcolor=\"#000000\" link=\"#FFFF00\" \
        vlink=\"#FFFF00\" alink=\"#FFFF00\">" > $web_dir$day_html
echo -e "<table BORDER COLS=4 WIDTH=\"100%\" >\n" >> $web_dir$day_html
echo -e "<tr>\n<td>Datum</dt>\n<td>L&auml;nge</td>\n<td>Breite</td>\n\
        <td>Tages-Kommentar</td>\n</tr>" >> $web_dir$day_html

anz_line=`wc $map_dir$route_tab | awk '{ print $1 }'`

for (( i = 1; i <= anz_line; i++ ))
do
  datu=`tail +$i $map_dir$route_tab | head -n1 | cut -f1`
  long=`tail +$i $map_dir$route_tab | head -n1 | cut -f2`
  lati=`tail +$i $map_dir$route_tab | head -n1 | cut -f3`
  comm=`tail +$i $map_dir$route_tab | head -n1 | cut -f4`
  long_web=`echo $long | sed -e "s/+/E/g" | sed -e "s/-/W/g"`
  lati_web=`echo $lati | sed -e "s/+/N/g" | sed -e "s/-/S/g"`
  long_calle=`echo $long | sed -e "s/+//g"`
  lati_calle=`echo $lati | sed -e "s/+//g"`
  datu_calle=`echo ${datu:4:11}`
 
  # Schreiben der Day-Tab Zeile in HTML
  echo -e "<tr>\n<td><a href=
http://www.calle.com/info.cgi?lat=$lati_calle&long=$long_calle&name=$datu_calle&kind=illum
>$datu</a></td>\n<td>$long_web&deg;</td>\n<td>$lati_web&deg;</td>\n<td>$comm </td>\n</tr>" >>
$web_dir$day_html

  # Schreiben von route_longlat fuer proj
  echo -e "$long\t$lati" >> $map_dir$route_longlat

done
echo -e "</table>\n</body>\n</html>" >> $web_dir$day_html

# Umrechnung von Long/Lat nach Pixel (mit Proj4)

$proj_tool -m 1:5393 +proj=vandg +x_0=1024670 +y_0=-5161101 $map_dir$route_longlat >
$map_dir$route_pixel

# Abarbeiten der einzelnen Positionen und inkrementelles erstellen der Karte.
# Manipulation der Karte mittels ImageMagick

last_x=0
last_y=0

anz_line=`wc $map_dir$route_pixel | awk '{ print $1 }'`

for (( i = 2; i <= anz_line; i++ ))
do
  pre=`expr $i - 1`
  datu=`tail +$pre $map_dir$route_tab | head -n1 | cut -f1`
  x1=`tail +$pre $map_dir$route_pixel | head -n1 | cut -f1`
  y1=`tail +$pre $map_dir$route_pixel | head -n1 | cut -f2`
  x2=`tail +$i $map_dir$route_pixel | head -n1 | cut -f1`
  y2=`tail +$i $map_dir$route_pixel | head -n1 | cut -f2`

  # Negatives Vorzeichen entfernen
  y1=`echo ${y1:1:9}`
  y2=`echo ${y2:1:9}`

  # Distanz zur letzten Beschriftung berechnen ( /1 ist Workaround fuer scale Bug in bc)
  dist=`echo "scale=0; sqrt(($last_x - $x1)^2 + ($last_y - $y1)^2)/1" | bc`

#  x_dat=`expr $x1 + 10`
  mdatu=`echo ${datu:5:6}`

  # Bearbeiten der Grafik
  $img_tool -fill black -stroke black -linewidth 3 \
            -draw "line $x1,$y1,$x2,$y2" \
            $map_dir$route_map $map_dir$route_map

  if [ $dist -gt $min_text_dist ]
  then
    last_x=$x1
    last_y=$y1
    $img_tool -fill black -stroke black -linewidth 1 -pointsize 20 \
              -font arial -draw "text $x1,$y1 \" $mdatu\"" \
              $map_dir$route_map $map_dir$route_map
  fi
done

# Copyright Infos schreiben

$img_tool -fill black -stroke black -linewidth 1 -pointsize 25 \
          -font arial -draw "text 30,1730 \"$datu, www.fratz.li\"" \
          $map_dir$route_map $map_dir$route_map


# Webkarten in jpg generieren
$img_tool -geometry %20 $map_dir$route_map $map_dir$route_20
$img_tool -geometry %40 $map_dir$route_map $map_dir$route_40
$img_tool -geometry %60 $map_dir$route_map $map_dir$route_60
$img_tool -geometry %80 $map_dir$route_map $map_dir$route_80
$img_tool $map_dir$route_map $map_dir$route_100

#HTML fuer die Kartenanzeige generieren
dat=`date +"%d.%m.%Y um %T"`
long=`cat $map_dir$route_update | cut -f1 | sed -e "s/+/Ost /g" | sed -e "s/-/West /g"`
lati=`cat $map_dir$route_update | cut -f2 | sed -e "s/+/Nord /g" | sed -e "s/-/Sued /g"`
echo -e "<html>\n<head>\n<title>Route</title>\n</head>\n<body text=\"#FFFFFF\"
bgcolor=\"#000000\" link=\"#FFFF00\" vlink=\"#FFFF00\" alink=\"#FFFF00\">\n" >
$web_dir$route20_html
echo -e "<h2>Aktuelle Position: $long&deg; / $lati&deg;</h2>\n<br>\nLetztes update: $dat\n" >>
$web_dir$route20_html
echo -e "<a href=$route40_html>Gr&ouml;ssere Karte</a>" >> $web_dir$route20_html
echo -e "<p><img SRC=\"$route_20\">\n</body>\n</html>" >> $web_dir$route20_html

echo -e "<html>\n<head>\n<title>Route</title>\n</head>\n<body text=\"#FFFFFF\"
bgcolor=\"#000000\" link=\"#FFFF00\" vlink=\"#FFFF00\" alink=\"#FFFF00\">\n" >
$web_dir$route40_html
echo -e "<h2>Aktuelle Position: $long&deg; / $lati&deg;</h2>\n<br>\nLetztes update: $dat\n" >>
$web_dir$route40_html
echo -e "<a href=$route60_html>Gr&ouml;ssere Karte</a>" >> $web_dir$route40_html
echo -e "<p><img SRC=\"$route_40\">\n</body>\n</html>" >> $web_dir$route40_html

echo -e "<html>\n<head>\n<title>Route</title>\n</head>\n<body text=\"#FFFFFF\"
bgcolor=\"#000000\" link=\"#FFFF00\" vlink=\"#FFFF00\" alink=\"#FFFF00\">\n" >
$web_dir$route60_html
echo -e "<h2>Aktuelle Position: $long&deg; / $lati&deg;</h2>\n<br>\nLetztes update: $dat\n" >>
$web_dir$route60_html
echo -e "<a href=$route80_html>Gr&ouml;ssere Karte</a>" >> $web_dir$route60_html
echo -e "<p><img SRC=\"$route_60\">\n</body>\n</html>" >> $web_dir$route60_html

echo -e "<html>\n<head>\n<title>Route</title>\n</head>\n<body text=\"#FFFFFF\"
bgcolor=\"#000000\" link=\"#FFFF00\" vlink=\"#FFFF00\" alink=\"#FFFF00\">\n" >
$web_dir$route80_html
echo -e "<h2>Aktuelle Position: $long&deg; / $lati&deg;</h2>\n<br>\nLetztes update: $dat\n" >>
$web_dir$route80_html
echo -e "<a href=$route100_html>Gr&ouml;ssere Karte</a>" >> $web_dir$route80_html
echo -e "<p><img SRC=\"$route_80\">\n</body>\n</html>" >> $web_dir$route80_html

echo -e "<html>\n<head>\n<title>Route</title>\n</head>\n<body text=\"#FFFFFF\"
bgcolor=\"#000000\" link=\"#FFFF00\" vlink=\"#FFFF00\" alink=\"#FFFF00\">\n" >
$web_dir$route100_html
echo -e "<h2>Aktuelle Position: $long&deg; / $lati&deg;</h2>\n<br>\nLetztes update: $dat\n" >>
$web_dir$route100_html
echo -e "<p><img SRC=\"$route_100\">\n</body>\n</html>" >> $web_dir$route100_html


# Webkarten in Webdirectory kopieren
cp $map_dir$route_20 $web_dir
cp $map_dir$route_40 $web_dir
cp $map_dir$route_60 $web_dir
cp $map_dir$route_80 $web_dir
cp $map_dir$route_100 $web_dir

# Loeschen des Positionsupdate

rm $map_dir$route_update

fi