+#!/bin/bash
+
+LOCKFILE=/home.local/osm/run/.osmupdate.lock
+CHANGESFILE=/home.local/osm/run/osmosis-changes.osc.gz
+EXPIRYFILE=/home.local/osm/run/expiredtiles.lst
+OSMOSISWORKDIR=/home.local/osm/.osmosis
+WEBSTATEFILE=/var/www/lastdbupdate.txt
+# Note: set the expire-tiles to 3 less than the real maximum,
+# as this makes the tilelist MUCH shorter, and the tiles get rerendered
+# anyways due to the metatile-layout!
+UPDATECMD="/usr/bin/osm2pgsql --slim --cache=8000 --append --database=osm \
+ --style=/home.local/osm/dbstyle/osm2pgsql-new.style \
+ --hstore --expire-tiles=17-17 \
+ --expire-output=$EXPIRYFILE $CHANGESFILE"
+
+cleanup()
+{
+ rm -f "$LOCKFILE"
+ exit 1
+}
+
+trytofeedupdate()
+{
+ echo "Trying to feed updates into databse..."
+ echo "Executing: $UPDATECMD"
+ $UPDATECMD
+ if [ $? == 0 ] ; then
+ echo "Update went fine, removing changes-file"
+ rm -f $CHANGESFILE
+ else
+ echo "Update failed with error $?"
+ fi
+}
+
+echo "Starting update script: `date`"
+if [ -e "$LOCKFILE" ] ; then
+ echo "Lockfile exists - ABORTING"
+ exit 1
+fi
+touch "$LOCKFILE" || exit 9
+trap cleanup 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 \
+ 20 21 22 23 24 25 26 27 28 29 30 31
+if [ -e $CHANGESFILE ] ; then
+ echo "Changesfile exists - will try to feed update to Database"
+ trytofeedupdate
+fi
+if [ -e $CHANGESFILE ] ; then
+ echo "Changesfile STILL exists - ABORTING"
+ cleanup
+ exit 1
+fi
+echo "Downloading changes with OSMOSIS..."
+echo "Executing: /usr/bin/osmosis --read-replication-interval workingDirectory=$OSMOSISWORKDIR --simplify-change --write-xml-change $CHANGESFILE"
+/usr/bin/osmosis --read-replication-interval workingDirectory=$OSMOSISWORKDIR --simplify-change --write-xml-change $CHANGESFILE
+if [ $? != 0 ] ; then
+ echo "Osmosis execution failed. ABORTING"
+ cleanup
+ exit 1
+fi
+trytofeedupdate
+if [ -e $CHANGESFILE ] ; then
+ echo "Changesfile STILL exists - update must have failed. ABORTING"
+ cleanup
+ exit 1
+fi
+echo "Update seems to have been successful."
+grep timestamp $OSMOSISWORKDIR/state.txt | sed -e 's/timestamp=//g' -e 's/T/ /g' -e 's/\\:/:/g' > $WEBSTATEFILE
+if [ -e $EXPIRYFILE ] ; then
+ echo "Expiryfile exists. Telling tirex to rerender the tiles mentioned there..."
+ echo "Number of lines: `wc -l $EXPIRYFILE`"
+ cat $EXPIRYFILE | /home.local/osm/bin/osmtilecleanup.pl \
+ --action rerenderexpiredlist --zoom 13-20 \
+ --rrs /home.local/osm/bin/osmrerenderwrapper.sh \
+ /var/lib/tirex/tiles/osm
+ cat $EXPIRYFILE | /home.local/osm/bin/osmtilecleanup.pl \
+ --action rerenderexpiredlist --zoom 13-20 \
+ --rrs /home.local/osm/bin/osmrerenderwrapper.sh \
+ /var/lib/tirex/tiles/osmde
+ rm $EXPIRYFILE
+fi
+echo "Ending update script cleanly: `date`"
+cleanup
+exit 0
+