#!/bin/bash
# [example: whole Czech Republic]
#$1 zoom (lowest number)		[9]
#$2 zoom (highest number)		[any]
#$3 column in $1 zoom (lowest number)	[273]
#$4 column in $1 zoom (highest number)	[282]
#$5 row in $1 zoom (lowest number)	[171]
#$6 row in $1 zoom (highest number)	[176]

col_min="$3"
col_max="$4"
row_min="$5"
row_max="$6"

progressfile="osmdw_progress"
conffile="osmdw.conf"
sleep_min="3"
sleep_max="25"
osm_dir="/home/lhc/dwmaps/OSM"
debug="1"

# Example conffile
#-----------------------------
#sleep_min=2
#sleep_max=5
#-----------------------------

if [ -f "$progressfile" ]; then
	prg="$(tail -n 1 "$progressfile")"
	#The last downloaded tile
	zoom="$(echo "$prg" | cut -d ' ' -f 1)"
	col="$(echo "$prg" | cut -d ' ' -f 2)"
	row="$(echo "$prg" | cut -d ' ' -f 3)"
	zoom_start="$1"
	zoom_delta="$(( zoom-zoom_start ))"
	zoom_times="$(( 2**zoom_delta ))"
	#left one is simply multiplied by 2^zoom
	col_min="$(( col_min*zoom_times ))"
	#right one simply multiplied by 2^zoom will be just left-most sub-tile.
	#but right+1 multiplied by 2^zoom will be the left-most sub-tile of the
	#next tile. Substract one and it's the right-most.
	col_max="$(( (col_max+1)*zoom_times-1 ))"
	row_min="$(( row_min*zoom_times ))"
	row_max="$(( (row_max+1)*zoom_times-1 ))"
else
	zoom="$1"
	col="$3"
	row="$5"

	#the fresh-started script will not download the first tile at all
	#download the first tile:

	if ! [ -d "$osm_dir/$zoom" ]; then mkdir "$osm_dir/$zoom"; fi
	if ! [ -d "$osm_dir/$zoom/$col" ]; then mkdir "$osm_dir/$zoom/$col"; fi
	wget -O "$osm_dir/$zoom/$col/$row.png" "http://b.tile.openstreetmap.org/$zoom/$col/$row.png"	
	retval="$?"
	if [ "$debug" -ge "1" ]; then echo "Wget retval: $retval"; fi

	#store the tile to file - if the script die before downloading it, it
	#It will be handy if commandline will be lost before rerun,
	#because it's stored after # character.
	echo "$zoom $col $row # $@" >> "$progressfile"

	sleep_time=17; #just some random number
	if [ "$debug" -ge "1" ]; then echo "New sleep_time: $sleep_time"; fi	
	sleep "$sleep_time" 

	
fi

isdone="no"
while [ "$isdone" != "yes" ]; do
	##-## DEBUG random end of script ##-## x="$(( RANDOM % 3 ))"; echo "$x"; if [ "$x" -eq "0" ]; then isdone="yes"; fi;
	#check done; if done continue
	#compute next tile 

	if [ "$debug" -ge "1" ]; then echo "New cycle: zoom: $zoom, row: $row, col: $col"; fi
	
	if [ "$row" -lt "$row_max" ]; then
		(( row+=1 ))
		if [ "$debug" -ge "1" ]; then echo "New row: $row"; fi
	else
		#go to next col
		row="$row_min" #upper on
		if [ "$col" -lt "$col_max" ]; then
			(( col+=1 ))
			if [ "$debug" -ge "1" ]; then echo "New col: $col, new row: $row"; fi
		else
			if [ "$zoom" -ge "$2" ]; then
				#we're done
				echo "Download completed."
				isdone="yes"
				if [ "$debug" -ge "1" ]; then 
					echo "Last: zoom: $zoom, row: $row, col: $col (row is resetted to the first in col already)"
					echo "Last boudaries: col: $col_min - $col_max row: $row_min - $row_max"
				fi #debug
				continue
			fi
			#go to next zoom
			(( zoom+=1 ))
			#adjust boundaries
			zoom_times="2"
			#left one is simply multiplied by 2^zoom
			col_min="$(( col_min*zoom_times ))"
			#right one simply multiplied by 2^zoom will be just left-most sub-tile.
			#but right+1 multiplied by 2^zoom will be the left-most sub-tile of the
			#next tile. Substract one and it's the right-most.
			col_max="$(( (col_max+1)*zoom_times-1 ))"
			row_min="$(( row_min*zoom_times ))"
			row_max="$(( (row_max+1)*zoom_times-1 ))"

			col="$col_min"
			row="$row_min"
			
			if [ "$debug" -ge "1" ]; then 
				echo "New zoom: $zoom, New row: $row, New col: $col"
				echo "New boudaries col: $col_min - $col_max row: $row_min - $row_max"
			fi #debug

		fi
	fi

	#wget it

	retval="1"
	while [ "$retval" -ne "0" ]; do
		if ! [ -d "$osm_dir/$zoom" ]; then mkdir "$osm_dir/$zoom"; fi
		if ! [ -d "$osm_dir/$zoom/$col" ]; then mkdir "$osm_dir/$zoom/$col"; fi
		wget -O "$osm_dir/$zoom/$col/$row.png" "http://b.tile.openstreetmap.org/$zoom/$col/$row.png"	
		retval="$?"
		if [ "$debug" -ge "1" ]; then echo "Wget retval: $retval"; fi
		
		#if wget bad, sleep random and continue
	
		if [ "$retval" != "0" ]; then
			echo "Wget wasn't successfull, sleeping for a while and trying again"
		else
			#store the last tile to file
			echo "$zoom $col $row" >> "$progressfile"
		fi
	
		#sleep random

		if [ -f "$conffile" ]; then
			sleep_min="$(grep '^sleep_min=' "$conffile" | sed 's/^sleep_min=//')"
			sleep_max="$(grep '^sleep_max=' "$conffile" | sed 's/^sleep_max=//')"
			if [ "$debug" -ge "1" ]; then echo "New sleep from $conffile max: $sleep_max, min: $sleep_min"; fi
		fi
		sleep_delta="$(( sleep_max-sleep_min+1 ))"
		sleep_time="$(( RANDOM % sleep_delta + sleep_min ))"
		if [ "$debug" -ge "1" ]; then echo "New sleep_time: $sleep_time"; fi
		sleep "$sleep_time"	
	done
done
