#!/bin/csh -f
#+
# new_day -- set up directory, filename prefix, etc.
#
# Purpose:
#	Prepare the instrument for a new day of observing by
#	locating the data disk with the most available space and
#	creating a science data directory there.  Also:
#	= sets the science mosaic outdir keyword appropriately
#	= sets the science image filename prefix based on the UT date
#	= creates an "fcs" subdirectory for FCS images
#	= sets the FCS outdir keyword appropriately
#	= sets the FCS image filename prefix based on the UT date
#	= copies over any FCS reference files from the old data directory, 
#		IF the old data directory was also owned by the same user
#	= reset next science and FCS image numbers to 1
#	= enable saving to disk for both science and FCS system
#
# Usage:
#	newday [-test] [disk]
# 
# Flags:
#	-test = execute only if no directory is already set up for today
#
# Arguments:
#	disk = 1-digit number of disk to be used for data storage.  
#		Default is to let the newdir command select the best disk.
# 
# Output:
#	none
# 
# Restrictions:
#	none
# 
# Exit values:
#	 0 = normal completion
#	 1 = wrong number of arguments
#
# Example:
#	1) Set up new directories for today:
#		newday
#
#	2) Set up new directories on disk /sdata1004:
#		newday 4
# 
# 	3) Set up new directories only if not already done:
#		newday -test
#-
# Modification history:
#	2002-Aug-26	GDW	Original version
#	2002-Sep-15	GDW	Added FCS copy
#	2002-Sep-29	GDW	Fixed FCS copy
#	2005-Apr-05	GDW	- Forced new data dirs to be allocated on 
#				  /sdata1003 (via "newdir 3") due to disk pb
#				- Configure alternative paths and save
#				  in DISKLIST keyword
#	2005-Sep-05	GDW	- Force newdir 4
#	2005-Feb-28	GDW	- undo force newdir 4
#				- add "-test" flag
#-----------------------------------------------------------------------

set buf = $0
set cmd = $buf:t
set usage = "Usage: $cmd"
set do_test = 0

# parse flags...
while ( $#argv > 0 )

  # check for -no_init flag...
  if ( "$1" =~ \-t* ) then
    set do_test = 1
    shift
    continue
  endif

  # exit flag check if no flags remain...
  break

end

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

# check for disk request...
if ( $#argv >= 1 ) then
    set disk = $1
    shift
endif

# perform check for existing directory...
if ( $do_test == 1 ) then
    set ut_date = `ut_date`
    set outdir = `outdir`
    if ( "$outdir" =~ *$user/$ut_date ) then
	printf "${cmd}: Output directory $outdir already defined.\n"
	exit 0
    endif
endif

# grab old output directory...
set old_outdir = `outdir`
set old_outdir_fcs = $old_outdir/fcs

# allocate new directories on all disks for safety...
printf "Setting up alternate directories on all disks...\n"
set disklist = /tmp
foreach i ( 4 3 2 1 )
    newdir $i >& /dev/null
    if ( $status == 0 ) then
	set disklist = `outdir`:${disklist}
    endif
end
modify -s deiccd disklist = $disklist

# create new science data directory and set outdir...
if ( $?disk ) then
    printf "Forcing new data directory onto /sdata100${disk}...\n"
    newdir $disk
else
    newdir
endif

if ( $status != 0 ) then
    printf "ERROR: aborted due to error creating output directory\n"
    exit
endif

# create FCS directory and set outdir...
set outdir = `outdir`
set outdir_fcs = ${outdir}/fcs
if ( ! -e $outdir_fcs ) then
    mkdir $outdir_fcs
endif
modify -service deifcs outdir=${outdir_fcs} silent
echo "Setting FCS outdir to $outdir_fcs"

# copy in any existing FCS reference files IF previous outdir 
# was also owned by me...
echo $old_outdir | fgrep /${user}/ > /dev/null
if ( $status == 0 ) then
    ls $old_outdir_fcs/fcsref.*.ref >& /dev/null
    if ( $status == 0 ) then
	set old_fcs_ref_files = ( $old_outdir_fcs/fcsref.*.ref )
	if ( $#old_fcs_ref_files > 0 ) then
	    printf "Copying old FCS reference files...\n"
	    \cp $old_fcs_ref_files $outdir/fcs
	endif
    else
	    printf "No old FCS reference files found.\n"
    endif
endif

# set outfile for science data...
set buf = `ut_date -n`
set yy = $buf[1]
set mm = $buf[2]
set dd = $buf[3]
modify -s deiccd outfile = "d${mm}${dd}_"

# set FCS outfile...
modify -s deifcs outfile = "fcs${mm}${dd}_"

# reset frame numbers to 1...
frame 1
modify -s deifcs frameno=1

# set TODISK on for science and FCS...
modify -s deiccd todisk=t
modify -s deifcs todisk=t

# verify available disk space...