#!/bin/sh

user_agent="OSMTileProxy/1.0 (operated by algobase.com)"

osm=/disk1/osm


client_ip=$1

tile=$2
file=$osm/$tile

r=$(($RANDOM % 3))

abc="abc"
c=${abc:$r:1}

if [[ $tile == *watercolor* ]]; then
  url="https://stamen-tiles-$c."$tile
else
  url="https://$c."$tile
fi

log=$osm/user/$client_ip

if [ -f $file ]; then
  echo "EXISTING:" $tile >> $log
  exit;
fi

echo "DOWNLOAD:" $url >> $log 

mkdir -p $(dirname $file)

curl --user-agent "$user_agent" --max-time 5.0 -L -k -s -o $file -X GET $url

if [ $? != 0 ]; then
  echo "ERROR: $?" >> $log
  rm -f $file
fi



