#!/bin/csh -f
#+
# arrangeWindows -- move and resize LGSAO GUIs automatically
#                              on acquisition screen (center)
#
# Purpose:
#	Implement an optimal arranegement of windows for the various
#	LGSAO control GUIs.
#
# Usage:
#	arrangeWindows [-remote]
# 
# Arguments:
#	-remote = attempt to execute the script on the REMOTE host
#		which is displaying the windows.  This increases
#		execution speed by a factor of 10.
# 
# Output:
#	None
# 
# Restrictions:
#	None
# 
# Exit values:
#	 0 = normal completion
#	 1 = wrong number of arguments
#
# Example:
#
#-
# Modification history:
#	2004-Oct-08	GDW	Original version
#	2004-Oct-09	GDW	Added -remote
#       2006-Aug-15     jlyke   Adapted for LGSAO use under XVNC
#       2006-Oct-10     jlyke   split off for each VNC individually
#       2007-Mar-23     jlyke   New windows for NGWFC
#       2007-Nov-07     jlyke   chg ystart+0 to ystart+20 for FVWM2 offset
#       2014-Jun-20     jlyke   Adapt for OSIRIS
#                               Note: Cannot get OSIRIS java to move
#       2015-Oct-21     jlyke   Add eventsounds
#-----------------------------------------------------------------------

set buf = $0
set cmd = $buf:t
set usage = "Usage: $cmd"
set remote = 0
set hoffset = 0
set voffset = 20
set vnc_comp = `kvncinfo -server`

# parse flags...
while ( $#argv >= 1 )
    if ( "$1" =~ \-r* ) then
	set remote = 1
	shift
	continue
    endif

    break
end

# verify args...
if ( $#argv > 0 ) then
  printf "$usage\n"
  exit 1
endif

# run remotely if requested...
if ( $remote ) then
    if ( $?REMOTEHOST ) then
	kixrsh $REMOTEHOST $cmd
    else
	printf "ERROR: REMOTEHOST not defined -- abort!\n"
    endif
endif

if ( $?DISPLAY ) then
  set orig_disp = $DISPLAY
endif

### to rearrange guis in VNC, we need to set via the 
#   size of the screen
#   this also lets us avoid the ~/.olvwmrc file
set xsize = `xwininfo -root | awk '/Width/ {print $2}'`
set ysize = `xwininfo -root | awk '/Height/ {print $2}'`

# process things on screen 0...
#set vnc_comp = `kvncinfo -server`
set port = `kvncstatus -q control0`
setenv DISPLAY ${vnc_comp}${port}
# Virtual Window 1 (upper left)
set xstart = `math 0 x $xsize`
set ystart = `math 0 x $ysize`
set xend   = `math $xstart + $xsize`
set yend   = `math $ystart + $ysize`

# Startup script
#xwit -pop -names "osirisSetupNight"
# PIG
xwit -move `math $xstart+0` `math $ystart+800+$voffset` -names "PIG"
# OOPGUI
xwit -move `math $xstart+0` `math $ystart+0+$voffset` -names "OSIRIS Obser"
# ODEC
xwit -move `math $xstart+476` `math $ystart+0+$voffset` -names "OSIRIS Dataset"
# OSGUI
xwit -move `math $xstart+800` `math $ystart+0+$voffset` -names "OSIRIS Status"
# OTGUI
xwit -move `math $xstart+800` `math $ystart+535+$voffset` -names "OSIRIS Tel"

# process things on screen 1...
#set vnc_comp = `kvncinfo -server`
set port = `kvncstatus -q control1`
setenv DISPLAY ${vnc_comp}${port}
# Virtual Window 1 (upper left)
set xstart = `math 0 x $xsize`
set ystart = `math 0 x $ysize`
set xend   = `math $xstart + $xsize`
set yend   = `math $ystart + $ysize`

# IDL Launcher
xwit -move `math $xstart+0` `math $ystart+0+$voffset` -names "OSIRIS IDL"
# QL2 Imag
xwit -move `math $xstart+0` `math $ystart+230+$voffset` -names "OSIRIS IMAG"
xwit -move `math $xstart+66` `math $ystart+0+$voffset` -names "* Active Window -i"
# QL2 Spec
xwit -move `math $xstart+0` `math $ystart+450+$voffset` -names "OSIRIS SPEC"
xwit -move `math $xstart+594` `math $ystart+0+$voffset` -names "* Active Window -s"
# QL2 ORP
xwit -move `math $xstart+0` `math $ystart+670+$voffset` -names "OSIRIS ONLINE"
# DRP Backbone
xwit -move `math $xstart+180` `math $ystart+620+$voffset` -names "OSIRIS DRP"
# OORGUI
xwit -move `math $xstart+1050` `math $ystart+520+$voffset` -names "OSIRIS Online"
# iconify ql2 IDL terminals
xwit -iconify -names "ql2:"
# process things on screen 2...
#set vnc_comp = `kvncinfo -server`
set port = `kvncstatus -q control2`
setenv DISPLAY ${vnc_comp}${port}
# Virtual Window 1 (upper left)
set xstart = `math 0 x $xsize`
set ystart = `math 0 x $ysize`
set xend   = `math $xstart + $xsize`
set yend   = `math $ystart + $ysize`

# Eventsounds
xwit -move `math $xstart+600` `math $ystart+440+$voffset` -names "Osiris event"
# MAGIQ
#xwit -move `math $xstart+200` `math $ystart+0+$voffset` -names "MAGIQ"
xwit -move `math $xstart+0` `math $ystart+0+$voffset` -names "MAGIQ"
# XMET
xwit -resize 625 340 -names "KECK 1 MET"
xwit -move -0 `math $ystart+630+$voffset` -names "KECK 1 MET"
# WFS Intensity
xwit -iconify -names "WFS Status Beta"
xwit -move `math $xstart+700` `math $ystart+680+$voffset` -names "WFS Inten"
xwit -lower -names "WFS Inten"
# LUI
#xwit -move `math $xstart+200` `math $ystart+830+$voffset` -names "L U I"
xwit -move -0 `math $ystart-$voffset+$voffset` -names "L U I"
xwit -lower -names "L U I"
# FACSUM
xwit -resize 850 500 -names "KECK 1 FACSUM"
xwit -move -0 `math $ystart+120+$voffset` -names "KECK 1 FACSUM"
xwit -raise -names "KECK 1 FACSUM"
#
# Virtual Window 2 (upper right)
set xstart = `math 1 x $xsize`
set ystart = `math 0 x $ysize`
set xend   = `math $xstart + $xsize`
set yend   = `math $ystart + $ysize`

# QL2 FRP
xwit -move `math $xstart+0` `math $ystart+230+$voffset` -names "OSIRIS FRP"
# DRP Backbone
xwit -move `math $xstart+0` `math $ystart+620+$voffset` -names "OSIRIS DRP"
# ODRFGUI
set guiname = "OSIRIS Data Reduction File GUI"
set guiid = `xwininfo -root -all | grep  "$guiname" | awk '{print $1}'`
if ($guiid != "") then
  xwit -move `math $xstart+250` `math $ystart+0+$voffset` -id $guiid
endif

# process things on screen 3...
#set vnc_comp = `kvncinfo -server`
set port = `kvncstatus -q telstatus`
setenv DISPLAY ${vnc_comp}${port}
# Virtual Window 1 (upper left)
set xstart = `math 0 x $xsize`
set ystart = `math 0 x $ysize`
set xend   = `math $xstart + $xsize`
set yend   = `math $ystart + $ysize`


# reset DISPLAY envar
if ( $?orig_disp ) then
  setenv DISPLAY $orig_disp
endif

exit