#! /bin/sh

set -v

DB=${1:-geoville}
GINA=gina

destroydb $DB

createdb $DB

# Fill the postgres tables

zcat < $GINA.Z | $GEOHOME/conversion/gtopg $DB

$GEOHOME/conversion/make_topo $DB

monitor -c "delete top_polys where top_polys.centroid_fn = f_map_sheet.feat_num" $DB

# Create some GEO sugar tables

$GEOHOME/postgres/makecolors $DB

$GEOHOME/postgres/makepredefs $DB

$GEOHOME/postgres/makeregexp $DB

$GEOHOME/postgres/makedyninfo $DB

$GEOHOME/postgres/makeicons $DB

# Merge the ARGIS spatial/relational tables as it should be.
# Postgres does not need this separation of data !!! Hurray :-)

monitor -c "retrieve into property (PROPERTY.all, f_property.geo_loc) where PROPERTY.feat_num = f_property.feat_num" $DB
monitor -c "destroy PROPERTY" $DB
monitor -c "destroy f_property" $DB

monitor -c "retrieve into intersection (INTERSECTION.all, f_road_int.geo_loc) where INTERSECTION.feat_num = f_road_int.feat_num" $DB
monitor -c "destroy INTERSECTION" $DB
monitor -c "destroy f_road_int" $DB

monitor -c "retrieve into road (ROAD.all, f_road_cl.geo_pline) where ROAD.feat_num = f_road_cl.feat_num" $DB
monitor -c "destroy ROAD" $DB
monitor -c "destroy f_road_cl" $DB

# Define some views
# Note that I did not define update semantics
#
# monitor -c "define view Accident_vw (ACCIDENT.all, intersection.all) where ACCIDENT.INTERSECTION = intersection.ID" $DB
# 
# monitor -c "define view Taxes_vw (TAX.all, property.all) where TAX.ID = property.TAX_ID" $DB
# 
# monitor -c "define view Road_vw (CLDIV=CLASS.DIVISION, CLASS.SURFACE, CLASS.LANE, road.all) where CLASS.CLASS = road.CLASS" $DB
# 
monitor -c "vacuum" $DB
