#!/bin/csh -f
#+
#OSIRIS library of scripts
#
#NAME
#     osirisStartGuis -- Start the OSIRIS observing GUIs
#
#SYNOPSIS
#     osirisStartGuis [-t #] [-m] etc...
#
#DESCRIPTION
#     Called via a pulldown or from osirisSetupNight to start GUIs
#
#OPTIONS
#     -t #
#          ID number to attach to all script generated errors, warnings, and
#          questions that are passed to the OGS.  The ID number is separated
#          from the rest of the message with a ";".
#
#     -m
#          simulate calls to RPC servers.  during simulated calls, the 
#          command is echoed (instead of being executed).
#
#     others?
#
#EXAMPLES
#     osirisStartGuis
#
#ENVIRONMENT VARIABLES
#     list of environment variables used
#
#FILES
#     list of files used
#
#SERVERS & KEYWORDS
#     list of the servers and keywords used, for example:
#
#     service = osiris
#          keywords: sfilter/ifilter, sscale
#
#SCRIPTS CALLED
#     list of the scripts called by this script, for example:
#
#     help, syncheck
#
#EXIT STATUS
#     0 - normal exit, no error
#     1 - script aborted by an interrupt
#     2 - syncheck error
#     3 - error parsing command line input
#     other errors...
#
#SEE ALSO
#     ???
#-
#
# Modification History:
# 20041124 - MB:    Initial version created
# 20100517 - jlyke: Broken out from osirisSetupNight
# 20120202 - jlyke: Switched to svncserver1 
# 20151021 - jlyke: Added eventsounds
# 20160411 - jlyke: Changed some GUIs to "up"-grade equivalents
# 20200630 - jlyke: Changes with Imager and host upgrade
#

# Boiler plate for "-h" support for command autohelp.

if ("$1" == "-h") then
    help $0 | more
    exit $status
endif

# Set up to trap interrupts (Ctrl-C, etc.)
onintr abort

# Set default variable values
set cmdpre = ""
set cmdsuf = ""
set idnum = 0
set allguis = 1

# Check for flags
set noglob
while ($#argv != 0)
##echo "${0} ${mode}: Checking ${1}..."
    switch ($1)
	case -t:
	    set CheckStatus = `syncheck -command $1 $2 -pattern int`
	    if ("$CheckStatus" == "OK") then
		set idnum = $2
		shift
	    else
		echo "${0} ${mode}: Invalid script ID number specified <${2}> - using ${idnum}."
	    endif
	    unset Checkstatus
	    breaksw
	case -m:
	    set sim
	    set cmdpre = "echo ${0}: sim:"
	    set cmdsuf = "-m"
	    breaksw
        case -n*:
        case -b*:
        case -o*:
            # -n[one], -b[asic], -o[siris] start only basic osiris GUIs
            set allguis = 0  
            breaksw  
	default:
	    echo "${0}: Invalid command line flag $1 specified."
	    echo "${0}: Usage: $0 [-t #] [-m]"
	    # set the error code for an error with command line input
	    exit 3
	    breaksw
    endsw
    shift
end
unset noglob

set sleep_between_guis = 1

set control_string = "control0"
set drp_string = "control1"
set guider_string = "control2"
#set tel_string = "telstatus"
set tel_string = "control2"
set current_disp = `printenv DISPLAY`

set vnc_comp = `kvncinfo -server`
set statcmd = "kvncstatus -n"
set portcmd = "kvncstatus -q"

# confirm start guis and orp
# jlyke 2006 sep 07 take out IDL stuff as I can't get it to work :(
#echo "Do you want to start the guis and IDL? [Y/N]:"
# Remove the confirmation, just startup GUIs

#echo "Do you want to start the guis (IDL must be started from cmd line) [Y/N]:"
#set confirm=$<

sleep ${sleep_between_guis}

#if (${confirm} == "Y" || ${confirm} == "y") then 
    # set display num for guis
    set gui_display=$current_disp
    set  ql_display=0

    # determine displays to use
    foreach disp (control drp)
      set skip = 0
      set vnc_stat = 0

      # set display-specific params
      switch ($disp)
        case control:
	  set string = $control_string
	  set guis = (oopgui odec osgui otgui)
#	  set guis = (upoopgui upodec uposgui otgui)
	  breaksw
	case drp:
	  set string = $drp_string
	  set guis = (oorgui idl iql2 sql2 oql2 odrp)
#	  set guis = (oorgui)
	  breaksw
      endsw
#      ssh ${vnc_comp} ${statcmd} ${string}
      ${statcmd} ${string}
      if ($status == 0 ) then
#	set port = `ssh ${vnc_comp} ${portcmd} ${string}`
	set port = `${portcmd} ${string}`
        set gui_display = ${vnc_comp}${port}
      else
        echo "\aCannot start GUIs--there is no VNC server running as ${USER}."
	exit 1
      endif
      # start guis
      foreach gui ($guis)
        set gui_count = `ct | grep ${USER} | grep -ic ${gui}`
	if ( $gui_count == 0 ) then
#	  if ( $gui == "idl" ) then
          if ( $disp == "drp" ) then
#            2020-07-16 jlyke commented out xhost
#            xhost + napili
            setenv DISPLAY ${gui_display}
            osiris start ${gui} -D ${gui_display} &
	    setenv DISPLAY ${current_disp}
	  else
	    osiris start ${gui} -D ${gui_display} &
	  endif
	else
	   echo "\a${USER} is already running an instance of ${gui}--skipping."
	endif
      end

    end 
    # wait for guis to initialize
    sleep ${sleep_between_guis}
# endif

# if allguis = 1 (default) start other observing GUIs
if ( $allguis == 1 ) then
  # Give a little more time
  sleep 3
  # launch the pig...
  set string = $control_string
  set port = `${portcmd} ${string}`
  set gui_display = ${vnc_comp}${port}
  setenv DISPLAY ${gui_display}
#  echo "Starting the PIG on $DISPLAY"
#  pig
  # launch ospec_info
  set string = $control_string
  set port = `${portcmd} ${string}`
  set gui_display = ${vnc_comp}${port}
  setenv DISPLAY ${gui_display}
#  echo "Starting the ospec_info $DISPLAY"
#  ospec_info &
  # launch control2 items (MAGIQ, LUI)
  set string = $guider_string
  set port = `${portcmd} ${string}`
  set gui_display = ${vnc_comp}${port}
  setenv DISPLAY ${gui_display}
  # MAGIQ
#  echo "Starting the MAGIQ ObserverUI on $DISPLAY"
#  kixrsh svncserver1 -e sleep_wrapper start_magiq_obs
  ssh -X k1ruts@k1-magiq-server magiq start ObserverUI >& /dev/null &
  # Eventsounds
  ssh -X osiris eventsounds -a osiris &
  # LUI
  echo "Starting the LUI on $DISPLAY"
  kixrsh -l k1obsao k1aoserver startLui
  echo "Starting the WFS intensity display on $DISPLAY"
  kixrsh -l k1obsao k1aoserver wfsstatus.csh
  # Offline DRP
#  echo "Starting the Offline DRP on $DISPLAY"
#  osirisOfflineDRP
  # launch telstatus items (FACSUM, XMET)
  set string = $tel_string
  set port = `${portcmd} ${string}`
  set gui_display = ${vnc_comp}${port}
  setenv DISPLAY ${gui_display}
  echo "Starting FACSUM and XMET on $DISPLAY"
  facsum -k1 &
  met -k1 &
#  xrsh -l k1ruts k1server display_xmet -tel k1 -opt small
endif

# restore initial DISPLAY envar
setenv DISPLAY ${current_disp}

# arrange windows
echo "Arranging Windows"
arrangeWindows

goto done

abort:
# Block of code to handle interrupts.
exit 1

done:
# is there anything that needs to go here?
exit
