#!/bin/sh
set -e

echo finding current map release date...
#VERVAR="$(wget -q -O - http://garmin.na1400.info/routable.php | grep '^var version = ')"
#<input type="hidden" name="version" value="13-04-2011">
VERVAR="$(wget -q -O - http://garmin.openstreetmap.nl/ | grep '<input.*name=.version.')"
VERVAR="$(echo $VERVAR | grep -o 'value=.*')" 
VERVAR="$(echo $VERVAR | awk -F= '{print $2}')" 
VERVAR="$(echo $VERVAR | perl -pe "s/[>\"'\s;]//g")"
if [ -z "$VERVAR" ]; then
	echo unable to determine version
	exit 1;
fi
echo released: $VERVAR;
exit 0;
BASE=http://planetosm.oxilion.nl/~lambertus/garmin/routable/$VERVAR/

WORKDIR=$HOME/routable/$VERVAR
OUTDIR=$WORKDIR/out
WWWDIR=$HOME/www/garmin/Lambertus/$VERVAR

mkdir -p $OUTDIR $WORKDIR
if ! [ -e "$WWWDIR" ]; then
	ln -s $OUTDIR $WWWDIR
	rm $HOME/www/garmin/Lambertus/latest
	ln -s $OUTDIR $HOME/www/garmin/Lambertus/latest
fi

touch --reference=$OUTDIR start_stamp

# This lists all the areas in the world in KML format
WORLD="$WORKDIR/world.php"
if ! [ -e "$WORLD" ]; then
	echo downloading world kml file...
	wget -nv -O "$WORLD" http://garmin.na1400.info/world.php
fi

ls --time-style=full-iso -ld $OUTDIR start_stamp
AA="$WORKDIR/all-areas"
if ! [ -e "$AA" ]; then
	echo creating area list: $AA
	# list all the state polygons
	for i in state-polys/*.poly; do
		cat $i;
		echo;
	done | time perl readpoly.pl "$WORLD" > "$AA"
	# and filter the world's areas through that list
	# output any intersecting areas into all-areas
fi

ls --time-style=full-iso -ld $OUTDIR start_stamp
# get the images listed in all areas
TOTAL=`cat "$AA" | wc -l`
NR=0
for img in `cat "$AA"`; do
	NR=$((NR+1))
	F="$WORKDIR/$img.img"
	[ -e "$F" ] && continue;
	printf 'fetching %d/%d\n' $NR $TOTAL
	wget -nv $BASE/$img.img -O "$F"
done

# mkgmap pukes on empty image files.  But, on 404 and other
# errors wget creates 0-length files.  Find and kill them:
find $WORKDIR -name '*.img' -size 0b -exec rm '{}' \;

# create gmapsupp files broken up into various sizes
ls --time-style=full-iso -ld $OUTDIR start_stamp
for i in 500 1000 2000 4000; do
	perl partials.pl "$WORKDIR" $i || break;
done

cp ~/routable/README $OUTDIR/
ls --time-style=full-iso -ld $OUTDIR start_stamp
ls -ld  $OUTDIR start_stamp 
if [ $OUTDIR -nt start_stamp ]; then
	echo sending mail;
cat << _EOF | mail -s "Whole-US Garmin Map update - $VERVAR" dave@sr71.net talk-us@openstreetmap.org
These are based off of Lambertus's work here:

	http://garmin.na1400.info/routable.php

I wrote scripts to joined them myself to lessen the impact of doing a
large join on Lambertus's server.  I've also cut them in large longitude
swaths that should fit conveniently on removable media.  

	http://daveh.dev.openstreetmap.org/garmin/Lambertus/$VERVAR

There have been some missing tiles from Lambertus's map (the red
rectangles),  I don't see any at the moment, so you may want to update
if you had issues with the last set.

If you buy a new card (especially SDHC), some are FAT16 from the
factory.  I had to reformat it to let me create a >2GB file.

I got a report that the 2GB version doesn't fit on a 2GB card.  I reduced
the amount of space that each version uses by 5%.  Hopefully that will let
things fit better and allow for more space for stuff like track logs.

-- Dave

_EOF

fi

rm start_stamp
