#!/bin/sh

# Calls a ruby script to download todays daily diff
# applies this to the planet file
# runs the motorway extract and waychains stuff
#
# . ./updatemotorwaysdaily.sh >> updatemotorways.log 2>&1 &

echo  ================= updatemotorwaysdaily.sh =====================
date

#Settings on my laptop
#export OSMOSIS_HOME="/home/harry/planet/osmosis-SNAPSHOT-r23583"
#export PLANET_HOME="/home/harry/planet/waychains"

#Setting on dev.openstreetmap.org
export OSMOSIS_HOME="/home/harrywood/bin/osmosis-SNAPSHOT-r23583"
export PLANET_HOME="/home/harrywood/public_html/waychains"


if  [ ! -d $OSMOSIS_HOME ]
then
   echo OSMOSIS_HOME $OSMOSIS_HOME directory does not exist; exit 1
fi

if  [ ! -d $PLANET_HOME ]
then
   echo OSMOSIS_HOME $PLANET_HOME directory does not exist; exit 1
fi




cd $PLANET_HOME


echo Moving planet-new to replace planet-old 
if [ ! -s $PLANET_HOME/planet-new.osm.pbf ]
then
   echo Yesterdays updated planet $PLANET_HOME/planet-new.osm.pbf does not exist or is zero size. Suggests a failure in the previous update; exit 1
fi 


rm $PLANET_HOME/planet-old.osm.pbf
mv $PLANET_HOME/planet-new.osm.pbf $PLANET_HOME/planet-old.osm.pbf

rm $PLANET_HOME/todays-diff.osc.gz
ruby $PLANET_HOME/download_daily_diff.rb

if  [ ! -f $PLANET_HOME/todays-diff.osc.gz ]
then
   echo Daily diff file $PLANET_HOME/todays-diff.osc.gz file does not exist; exit 1
fi



echo Applying todays-diff.osc.gz changefile
$OSMOSIS_HOME/bin/osmosis \
	--rxc $PLANET_HOME/todays-diff.osc.gz \
	--read-pbf $PLANET_HOME/planet-old.osm.pbf \
	--apply-change \
	--write-pbf $PLANET_HOME/planet-new.osm.pbf omitmetadata=true

date

if  [ ! -f $PLANET_HOME/todays-diff.osc.gz ]
then
   echo $PLANET_HOME/planet-new.osm.pbf file does not exist. Something went wrong with applying the changefile; exit 1
fi



echo Extracting U.S. motorways
rm $PLANET_HOME/us-motorways.osm
$OSMOSIS_HOME/bin/osmosis \
	--read-pbf $PLANET_HOME/planet-new.osm.pbf \
	--bounding-polygon file="$PLANET_HOME/us.poly" idTrackerType=BitSet \
    --way-key-value keyValueList="highway.motorway" \
    --write-xml $PLANET_HOME/us-motorways.osm 
		
#	--tee 3 \
#	--bounding-polygon file="$PLANET_HOME/greaterlondon.poly" idTrackerType=BitSet \
#	--write-xml $PLANET_HOME/greaterlondon.osm \
#	--bounding-box left=-1.4964 top=54.5567 right=-1.605 bottom=54.5061 \
#	--write-xml $PLANET_HOME/darlington.osm

if  [ ! -f $PLANET_HOME/us-motorways.osm ]
then
   echo $PLANET_HOME/us-motorways.osm file does not exist. Something went wrong with extracting US motorways; exit 1
fi

	
	
date
	
echo Simplifying motorways
rm $PLANET_HOME/us-motorways-simplified.osm
$OSMOSIS_HOME/bin/osmosis -p org.openstreetmap.osmosis.plugins.simplify.SimplifyPlugin \
     --read-xml file=$PLANET_HOME/us-motorways.osm \
     --simplify \
     --used-node idTrackerType=BitSet \
     --write-xml $PLANET_HOME/us-motorways-simplified.osm

if  [ ! -f $PLANET_HOME/us-motorways-simplified.osm ]
then
   echo $PLANET_HOME/us-motorways-simplified.osm file does not exist. Something went wrong with simplifying motorways; exit 1
fi


     
     
ruby $PLANET_HOME/waychainsummary.rb $PLANET_HOME/us-motorways-simplified.osm


date
echo DONE


