#!/bin/csh -f
#+
#OSIRIS library of scripts
#
#NAME
#     osirisSetupNight - brief description of script function
#
#SYNOPSIS
#     osirisSetupNight arg1 mode [-t #] [-m] etc...
#
#DESCRIPTION
#     description of the script
#
#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
#     osirisSetupNight junk1 -t 123
#          What would happen?
#
#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
# 20190221 - jlyke: Change date pattern from [01] to [0-2] for 2020
# 20200630 - jlyke: Restore osirisSetupUser behavior now with unified homedir
# 20200825 - jlyke: Run getProgname locally instead of on svncserver1
# 20210127 - jlyke: Update Object and Comment values
#
# alias echo so we can ring the bell
alias echo /usr/bin/echo

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

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

# Boilerplate for syncheck.
# Note that the boiler plate should be transparent for all usages,
# but to make correct use of syncheck you will need to specify the
# correct pattern.

set noglob
set CheckStatus = `syncheck -command $0 $* -pattern {text}` 
unset noglob

if ("$CheckStatus" != "OK") then
    help $0 | more
    exit 2
endif

# End of help/syncheck boiler plate.

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

# confirm OSIRIS_DATA_ROOT
if (`printenv OSIRIS_DATA_ROOT` == '') then
    echo "$OSIRIS_DATA_ROOT not set.  Please set to appropriate directory."
    exit
endif 

set DATA_ROOT = ${OSIRIS_DATA_ROOT}

# set default variable values
set unavail = ""
set startguis = 1
set getdate = 1
set allguis = 1

# check for arguments
while ( $#argv != 0 )
  switch ($1)
    case [0-2][0-9][01][0-9][0-3][0-9]:
      set date = $1
      set getdate = 0
      breaksw
    case -nog*:
      set startguis = 0
      breaksw
    case -b*:
    case -o*:
      # -b[asic] or -o[siris] guis only
      set allguis = 0
      breaksw
    default:
      help $0 | more
      exit 2
      breaksw
  endsw
  shift
end

if ( $getdate == 1 ) then
  # get current date
  #set date=`date -u "+%y%m%d"`
  # ut_date -n returns "yyyy mm dd", we need "yymmdd"
  set date = `ut_date -n | cut -c 3- | sed 's/\ //g'`
endif

# check to see if data dir tree populated
#if (!( -d ${DATA_ROOT}/${USER}/${date}) ) then
#    echo ${DATA_ROOT}/${USER}/${date} does not exist.
#    echo "creating directories and symlinks using osirisSetupUser $date"
#    osirisSetupUser $date
#endif
# directory existence check is within osirisSetupUser
# while separate home directories on osiris and osirisbuild
#foreach comp (osiris osirisbuild)
#  ssh $comp osirisSetupUser $date
#end
# 2020-06-30 jlyke 
#            comment out the "foreach" loop just above 
#            now have common home dirs and renamed osirisbuild to napili
osirisSetupUser $date

# set default values for detector servers. keywords set are:
# outdir, testdir, framenum
echo Setting default keywords for detector servers...
foreach service (osds oids)
  if (`osirisCheckServer ${service}` == 1) then
    if (`show -s ${service} -terse connected` == 1) then
      switch (${service})
        case oids:
          modify -s ${service} outdir=${DATA_ROOT}/${USER}/${date}/IMAG/raw
	  modify -s ${service} comment="OSIRIS Imager"
          breaksw
        case osds:
	  modify -s ${service} outdir=${DATA_ROOT}/${USER}/${date}/SPEC/raw
	  modify -s ${service} comment="OSIRIS Spectrograph"
	  breaksw
        endsw

	#modify -s ${service} init=1
	modify -s ${service} testdir=${DATA_ROOT}/${USER}/${date}/tmp
	modify -s ${service} framenum=0
	modify -s ${service} getkw=1
	modify -s ${service} getdcskw=1
	modify -s ${service} getaokw=1
	modify -s ${service} sampmode=3
        modify -s ${service} numreads=1
        modify -s ${service} coadds=1
        modify -s ${service} itime=2000
	modify -s ${service} extension="fits"
	modify -s ${service} writeall=0
	modify -s ${service} calcreads=0
	modify -s ${service} object="<none>"
    else
      echo "--- Warning: ${service} not connected - default keywords not set."
    endif
  else
	echo "--- Warning: ${service} not running - default keywords not set."
  endif
end
modify -s osiris setnum=0

# check user and set Progname (ProjCode)
if ($USER == osrseng) then 
  set progname = ENG
else 
  set progname = `getProgname`
endif
modify -s osiris progname=$progname

# start GUIs
if ( $startguis == 1 ) then
  if ( $allguis == 1 ) then
    set flag = ""
  else
    set flag = "-b"
  endif
  osirisStartGuis $flag
endif

# unlock and home mechanisms
osirisHomeMech

goto done

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

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