#!/bin/sh

# $Id: yaouh.sh,v 1.4 2009/02/05 09:03:37 guru Exp $
#
# idea basada en yaouh0.3_alpha.py
# http://wiki.openmoko.org/wiki/Yaouh
#
# Matthias Apitz <guru@unixarea.de>, Feb 2009


osmdir=~guru/Maps/OSM
baseURL=http://tile.openstreetmap.org/

cd ${osmdir}

test -f lastrun && {
    printf "Found a file [%s] and will skip all tiles newer then this;\n" ${osmdir}/lastrun
    printf "if this is not what you want, just remove this file.\n"
    printf . ; sleep 1
    printf . ; sleep 1
    printf . ; sleep 1
    printf . ; sleep 1
    printf . ; sleep 1
    printf "\n"
    }

find * -name *.png -print > /tmp/maps.$$

while read file; do

    printf "%s ... " ${file}
    test lastrun -ot ${file} && {
        printf "skipped\n"
	continue
    }

    remoteMd5=`curl -A yaouh.sh -I ${baseURL}${file} --stderr /dev/null | grep ETag | cut -d\" -f 2`
    localMd5=`md5 -q ${file}`

    if [ "${remoteMd5}" != "${localMd5}" ]; then
       wget --user-agent yaouh.sh -q ${baseURL}${file} -O ${file}
       printf "updated\n"
    else
       printf "\n"
    fi

done < /tmp/maps.$$

# cleanup
#
rm /tmp/maps.$$
