#! /bin/sh

set -v

DB=${1:-geo}
LREL=${2:?'Need argument 2 (line relation'}
REL=${3:-top_polys}
RESOL=${4:-1}

monitor -c "destroy pol_tmp" $DB
monitor -c "retrieve into pol_tmp ($LREL.all)" $DB

monitor -c "define index pol_tmp_index on pol_tmp using btree (id int4_ops)" $DB

monitor -c "destroy $REL" $DB
monitor -c "create $REL (centroid_fn= int4, geo_bbox= box, pgn= POLYGON2)" $DB

monitor -c "vacuum" $DB

monitor -TN -c "retrieve (poly_topology.centroid_fn, pol_tmp.pln) \
  where poly_topology.e_or_i != 'i'\
  and poly_topology.edge_fn = pol_tmp.id sort by centroid_fn" $DB |
$GEOHOME/conversion/topo.awk.copy.ge $DB $REL $RESOL

monitor -c "destroy pol_tmp" $DB
