#! /bin/sh
# Populate postgres with osm planet data

echo "Executing $0"

planet=/root/planet-latest.osm.bz2

if [ ! -f "$planet" ]; then
    echo "No planet file found!"
    exit 0
fi
if [ ! -x /usr/bin/osm2pgsql ]; then
    echo "osm2pgsql not installed!"
    exit 0
fi

/etc/init.d/postgresql-8.3 start
sudo -u postgres createuser -Upostgres -S -D -R gis
sudo -u postgres createdb -Upostgres -EUNICODE gis
echo GRANT ALL ON SCHEMA PUBLIC TO gis | sudo -u postgres psql -Upostgres gis
sudo -u postgres createlang -Upostgres plpgsql gis
sudo -u postgres psql gis < /usr/share/postgresql-8.3-postgis/lwpostgis.sql
echo GRANT ALL ON geometry_columns TO gis | sudo -u postgres psql -Upostgres gis
echo GRANT ALL ON spatial_ref_sys TO gis  | sudo -u postgres psql -Upostgres gis
sudo -u postgres /usr/bin/osm2pgsql $planet
/etc/init.d/postgresql-8.3 stop
rm -f "$planet"
exit 0
