#! /bin/sh

# This script is a wrapper around GEO which implements 
# dynamic maps, e.g. maps on which changing objects are
# automatically redrawn.

MONITOR=monitor
GEODB=${GEODB:-geo}

ap_oid="-1"

# define our cleanup trap:

trap '
  case $do_exit in Y) echo "Cleaning up, please be patient..." ;; esac

  case $ap_oid in
    -1) exit ;;
    0) $MONITOR -TN -c "delete geo_ap where geo_ap.pid = $$" $GEODB; exit ;;
  esac

  for apr in `$MONITOR -TN -c "retrieve (geo_active_apr.oid) where
	geo_active_apr.ap_oid = \"$ap_oid\"::oid " $GEODB | sed "s/ //g" `
  do
    $MONITOR -TN -c "remove instance rule apr_$apr" $GEODB
  done

  $MONITOR -TN -c "destroy ap_$ap_oid" $GEODB
  $MONITOR -TN -c "delete geo_active_apr
		where geo_active_apr.ap_oid = \"$ap_oid\"::oid" $GEODB
  $MONITOR -TN -c "delete geo_ap where geo_ap.oid = \"$ap_oid\"::oid" $GEODB

  case $do_exit in Y) exit ;; esac
' 1 2 3 15

echo "Killing old rules and portals from died GEO processes..."

cnt=1
for gp in `$MONITOR -TN -c "retrieve (geo_ap.all, geo_ap.oid)" $GEODB`
do

  case $cnt in
  1) hostname=$gp; cnt=2; continue ;;
  2) pid=$gp; cnt=3; continue ;;
  3) oid=$gp; cnt=1;;
  esac

  if rsh $hostname kill -0 $pid | grep 'No such process'
  then
    echo "Pid $pid on $hostname is no longer alive"
    do_exit=N
    ap_oid=$oid
    kill -2 $$
    ap_oid="-1"
  else
    echo "Pid $pid on $hostname is still alive"
  fi
done

echo 'Fill geo_ap...'

hostname=`hostname`
do_exit=Y

ap_oid=0
$MONITOR -TN -c "append geo_ap(hostname= \"$hostname\", pid=$$)" $GEODB
ap_oid=`$MONITOR -TN -c "retrieve (geo_ap.oid)
	  where geo_ap.hostname = \"$hostname\"
	  and geo_ap.pid = $$" $GEODB | sed 's/ //g' `

echo "Asynch portal is: ap_$ap_oid"

$MONITOR -TN -c "create ap_$ap_oid(geo_relation= char16, the_oid= oid,
		change_flag= int4) archive = none" $GEODB

echo 'Defining rules...'

cnt=1
for mon in `$MONITOR -TN -c "retrieve (geo_monitors.all, geo_monitors.oid)" $GEODB`
do

  case $cnt in
  1) relation=$mon; cnt=2; continue ;;
  2) flags=$mon; cnt=3; continue ;;
  3) mon_oid=$mon; cnt=1;;
  esac

  echo "Relation: $relation Flags: $flags..."

  for f in 1 2 4
  do
    if test `expr $flags / $f % 2` -eq 1
    then

      echo "Flag: $f"

      $MONITOR -TN -c "append geo_active_apr (monitor_oid= $mon_oid,
	change_flag= $f, ap_oid= $ap_oid)" $GEODB
      rule_oid=`$MONITOR -TN -c "retrieve (geo_active_apr.oid) where
	geo_active_apr.ap_oid = \"$ap_oid\"::oid
	and geo_active_apr.monitor_oid = \"$mon_oid\"::oid
	and geo_active_apr.change_flag = $f" $GEODB | sed 's/ //g' `

      case $f in
	1) action="delete"; cur_oid="current.oid" ;;
	2) action="replace"; cur_oid="current.oid" ;;
	4) action="append"; cur_oid="new.oid" ;;
      esac

      $MONITOR -TN -c "define instance rule apr_$rule_oid is
	on $action to $relation do [
	append ap_$ap_oid (geo_relation= \"$relation\",
	  the_oid= $cur_oid, change_flag= $f)
	notify ap_$ap_oid
	]
      " $GEODB
    fi
  done
done

echo 'Starting GEO...'

# GEOAP="ap_$ap_oid,5" $GEOHOME/geo -noover -nortree -merge "$@"
$GEOHOME/geo -noover -nortree -merge "$@"

# Clean up

kill $$
