# Copyright 2000 Earthstar Geographics.
#
# Script:  ESG_Geolink_Screen.erb
# Date:    March 2000
# Author:  Eric Augenstein (eric@es-geo.com)
# Summary: Geolinks all or current active window to Screen mode.  
#	     Does not link windows whose datum, projection or 
#          rotation does not match that of the first window.  
#          Uses the current window as the first one linked, 
#          therefore it becomes the 'master' window.

include "lib/BE_Startup.erb"

if current window then goto get_alg_info

say warning "Please open at least one geocoded image or algorithm first."
exit

get_alg_info:
	copy algorithm from window
	$alg_datum = get algorithm datum
	$alg_proj = get algorithm projection
	$alg_coordsys = get algorithm coordsys

if ($alg_coordsys == raw) then goto need_geo_image
if ($alg_datum == "RAW") then goto need_geo_image
if ($alg_proj == "RAW") then goto need_geo_image
goto get_win_info

need_geo_image:
say warning "Your image has a 'raw' datum, projection or coordinate type. You can only geolink geocoded images."
exit


# Find and tag the current, first and last window.

get_win_info:
$master_win = current window
$winfirst = first window
$winlast = last window

# We're starting with the current window active. Get the 
# master window (alg) info to compare with others, then
# link it to Screen.

get_alg_parms:
select $master_win
copy algorithm from window
first surface
first raster layer
	$ref_desc = get algorithm description
	$ref_datum = get algorithm datum
	$ref_proj = get algorithm projection
	$ref_rot = get algorithm rotation

set window geolink mode to screen

# Check other windows against the first one and link
# them if they match, otherwise skip over.

select $winfirst

link_other_windows:
$wincurr = current window
if ($wincurr == $master_win) then goto check_for_last

copy algorithm from window
first surface
	$curr_desc = get algorithm description
	$curr_datum = get algorithm datum
	$curr_proj = get algorithm projection
	$curr_rot = get algorithm rotation

if ($curr_datum != $ref_datum) then goto info_mismatch
if ($curr_proj != $ref_proj) then goto info_mismatch
if ($curr_rot != $ref_rot) then goto info_mismatch

set window geolink mode to screen

# Check if we're at the last window, exit out if yes.

check_for_last:
if ($wincurr == $winlast) then goto finished
next window
goto link_other_windows

# There was a parm mismatch with the master window, so
# check if we're at the end, or move onto next window.

info_mismatch:
if ($wincurr == $winlast) then goto finished
next window
goto link_other_windows

finished:
exit
