#!/bin/csh -f
#+
#OSIRIS library of scripts
#
#NAME
#     osirisSetupUser - prepares instrument and environment for a new user
#
#SYNOPSIS
#     osirisSetupUser date
#
#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
#     script_name 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
# 20051020 - MB: Commented out creation of unused directories
# 20060905 - jlyke: uncommented FRP, FRP/DRFs dir
# 20130522 - jlyke: Added symlink to RPBconfig.xml
# 20190221 - jlyke: Change date pattern from [01] to [0-2] for 2020
# 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

# date is passed in in YYMMDD format
if ( $#argv == 1 ) then
    if ($1 =~ [0-2][0-9][01][0-9][0-3][0-9] ) then
        set date = $1
    endif
else
    # 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

set DATA_ROOT = ${OSIRIS_DATA_ROOT}

# confirm ${DATA_ROOT}
#echo -n "Confirm make user tree for ${USER} in ${DATA_ROOT} [Y/N]:"
#set confirm=$<
#if (${confirm} != "Y" && ${confirm} != "y") then 
#    echo Aborting making of user data tree for ${USER}.
#    exit
#endif

# make user tree
if (!( -d ${DATA_ROOT}/${USER} ) ) then
    mkdir ${DATA_ROOT}/${USER}
endif

###commented out on 6/23/2005 by MB since we don't want the OORP to use
###individual user's calib files - they should use the archived ones...
# make calib tree
#if (!( -d ${DATA_ROOT}/${USER}/calib ) ) then 
#    mkdir ${DATA_ROOT}/${USER}/calib
#    mkdir ${DATA_ROOT}/${USER}/calib/SPEC
#    mkdir ${DATA_ROOT}/${USER}/calib/SPEC/darks
#    mkdir ${DATA_ROOT}/${USER}/calib/SPEC/flats
#    mkdir ${DATA_ROOT}/${USER}/calib/SPEC/wcal
#    mkdir ${DATA_ROOT}/${USER}/calib/SPEC/rectification
#    mkdir ${DATA_ROOT}/${USER}/calib/IMAG
#    mkdir ${DATA_ROOT}/${USER}/calib/IMAG/darks
#    mkdir ${DATA_ROOT}/${USER}/calib/IMAG/flats
#    mkdir ${DATA_ROOT}/${USER}/calib/IMAG/psf
    # the following are needed if the DRP backbone
    #  is run by someone other than ${USER}
#    chmod -R g+w ${DATA_ROOT}/${USER}/calib
#endif

# make date tree
if (!( -d ${DATA_ROOT}/${USER}/${date} ) ) then
    mkdir ${DATA_ROOT}/${USER}/${date}
    mkdir ${DATA_ROOT}/${USER}/${date}/tmp
    mkdir ${DATA_ROOT}/${USER}/${date}/ddf
    mkdir ${DATA_ROOT}/${USER}/${date}/drf
    mkdir ${DATA_ROOT}/${USER}/${date}/SPEC
    mkdir ${DATA_ROOT}/${USER}/${date}/SPEC/raw
    mkdir ${DATA_ROOT}/${USER}/${date}/SPEC/cal
    mkdir ${DATA_ROOT}/${USER}/${date}/SPEC/FRP
    mkdir ${DATA_ROOT}/${USER}/${date}/SPEC/FRP/DRFs
    mkdir ${DATA_ROOT}/${USER}/${date}/SPEC/ORP
    mkdir ${DATA_ROOT}/${USER}/${date}/SPEC/ORP/DRFs
    mkdir ${DATA_ROOT}/${USER}/${date}/IMAG
    mkdir ${DATA_ROOT}/${USER}/${date}/IMAG/raw
    #mkdir ${DATA_ROOT}/${USER}/${date}/IMAG/FRP
    #mkdir ${DATA_ROOT}/${USER}/${date}/IMAG/FRP/DRFs
    #mkdir ${DATA_ROOT}/${USER}/${date}/IMAG/ORP
    #mkdir ${DATA_ROOT}/${USER}/${date}/IMAG/ORP/DRFs
    # give raw data dirs group w permission
    chmod g+w ${DATA_ROOT}/${USER}/${date}/tmp
    chmod g+w ${DATA_ROOT}/${USER}/${date}/SPEC/raw
    chmod g+w ${DATA_ROOT}/${USER}/${date}/IMAG/raw
    # the following are needed if the DRP backbone
    #  is run by someone other than ${USER}
    # not needed anymore - MB - 20051020
    #chmod g+ws ${DATA_ROOT}/${USER}/${date}/SPEC/ORP
    #chmod g+ws ${DATA_ROOT}/${USER}/${date}/IMAG/ORP
    #chmod g+ws ${DATA_ROOT}/${USER}/${date}/drf
    #chmod g+ws ${DATA_ROOT}/${USER}/${date}/SPEC/ORP/DRFs
    #chmod g+ws ${DATA_ROOT}/${USER}/${date}/IMAG/ORP/DRFs

    # Sidecar server appears to require fully open permissions
    chmod -R 777 ${DATA_ROOT}/${USER}/${date}     
endif

# make symbolic links in ~/osiris/
if (!( -d ${HOME}/osiris ) ) then
    mkdir ${HOME}/osiris
endif

/usr/bin/rm -f ${HOME}/osiris/ddf_queue
ln -s ${DATA_ROOT}/${USER}/${date}/ddf ${HOME}/osiris/ddf_queue

/usr/bin/rm -f ${HOME}/osiris/drf_queue
ln -s ${DATA_ROOT}/${USER}/${date}/drf ${HOME}/osiris/drf_queue

/usr/bin/rm -f ${HOME}/osiris/imag_raw
ln -s ${DATA_ROOT}/${USER}/${date}/IMAG/raw ${HOME}/osiris/imag_raw

/usr/bin/rm -f ${HOME}/osiris/spec_raw
ln -s ${DATA_ROOT}/${USER}/${date}/SPEC/raw ${HOME}/osiris/spec_raw

/usr/bin/rm -f ${HOME}/osiris/spec_cal
ln -s ${DATA_ROOT}/${USER}/${date}/SPEC/cal ${HOME}/osiris/spec_cal

/usr/bin/rm -f ${HOME}/osiris/spec_orp
ln -s ${DATA_ROOT}/${USER}/${date}/SPEC/ORP ${HOME}/osiris/spec_orp

/usr/bin/rm -f ${HOME}/osiris/spec_frp
ln -s ${DATA_ROOT}/${USER}/${date}/SPEC/FRP ${HOME}/osiris/spec_frp

if ( -e ${HOME}/RPBconfig.xml ) then
  /usr/bin/rm -f ${HOME}/RPBconfig.xml
endif
ln -s /kroot/rel/default/data/RPBconfig.xml ${HOME}/RPBconfig.xml

goto done

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

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