#!/bin/sh
#+
# acquire_slit_alignment_exposure -- take a DEIMOS slitmask alignment exposure
#
# Purpose:
#	Acquire a slitmask alignment exposure with optional 
#	windowing, and restore the original parameters afterwards.
#
# Usage:
#	acquire_slit_alignment_exposure [-D] [-a <ampmode>] [-b <binning>] \
#		[-e <time(s)>] [-f <name>] [-w <mode>] 
# 
# Options:
#	-a = ampmode [default=current]
#	-b = binning mode [default=current]
#	-e = exposure time (sec) [default=current]
#	-f = filter name [default=current]
#	-w = CCD windowing mode (Manual|FullFrame|MaskArea|AlignArea) [default=current]
# 
# Output:
#	transcript to stdout
# 
# Restrictions:
#	None
# 
# Exit values:
#	 0 = normal completion
#	 1 = wrong number of arguments
#
# Example:
#	1) Acquire fullframe image with current filter, window mode, 
#	and exposure time:
#		acquire_slit_alignment_exposure
#
#	2) Acquire using current binning factor, windowed to mask area:
#		acquire_slit_alignment_exposure -w maskarea
#
#	3) Acquire using current binning factor, current window area
#       with exposure time set to 20s
#		acquire_slit_alignment_exposure -e 20
# 
#-
# Modification history:
#	2012-May-25	GDW	Original version (adapted from LRIS)
#	2013-Oct-07	GDW	Added filter
#	2014-Jun-21	GDW	Added ampmode
#-----------------------------------------------------------------------

cmd=`basename $0`
default=default
ampmode=$default	# default is to use current ampmode
binning=$default	# default is to use current binning
exptime=$default	# default is to use current exptime
filter=$default		# default is to use current filter
wmode=$default		# default is to use current windowing
debug=0
usage="Usage: $cmd [-a <ampmode>] [-b <binning>] [-e <time(s)>] [-f <filter>] [-w <window_mode>] "
tempfile=/tmp/$cmd.$$
state_file=/tmp/.save_state.$$

# restore the original state...
cleanup()
{
    printf "Starting cleanup..."
    trap "" 0 1 2 9 15
    restore_state -clobber -verify $state_file
    s=$?

    # remove the tempfile created in the earlier restore process...
    if [ -f $tempfile ]; then
	\rm $tempfile
    fi

    # remove the state file if the restore operation was successful...
    if [ $s -eq 0 ]; then
	\rm $state_file
    else 
	printf "[$cmd] ERROR: problem restoring keyword from file $state_file -- abort!\n\a"
	exit 1
    fi
    printf "done.\n"
}

# loop over command-line args...
while [ $# -gt 0 ]; do
    case "$1" in
	-D*) shift ; debug=1;;
	-a*) shift ; ampmode="$1"; shift ;;
	-b*) shift ; binning="$1"; shift ;;
	-e*) shift ; exptime="$1"; shift ;;
	-f*) shift ; filter="$1"; shift ;;
	-w*) shift ; wmode="$1"; shift ;;
	-*)  printf "$usage\n\a" && exit 1;;
    esac
done

# verify that we have the correct number of remaining args...
# (NOTE: $# is the number of args)
if [ $# -ne 0 ]; then
    printf "$usage\n\a"
    exit 1
fi


# check for debug...
if [ $debug = 0 ]; then
    foo=""
else
    printf "[$cmd] DEBUG MODE enabled\n"
    foo="echo"
    printf "ampmode=$ampmode\n"
    printf "binning=$binning\n"
    printf "exptime=$exptime\n"
    printf "filter=$filter\n"
    printf "wmode=$wmode\n"
fi

# save current state...
save_state -clobber -detector $state_file

# verify that the state file was written...
if [ -f $state_file ]; then
    if [ $debug ]; then
	printf "Contents of state file ${state_file}:\n"
	cat $state_file
	printf "------------------------------------------------------------------------\n\n"
    fi
else
    printf "[$cmd] ERROR: state file $state_file not found -- abort!\n"
    exit 1
fi

# make sure that there is no exposure taking place...
$foo wfi -nowait

# define what to do on interrupt...
trap cleanup 0 1 2 9 15

# allocate a file for settings...
touch $tempfile
if [ $? -ne 0 ]; then
    printf "[$cmd] ERROR: unable to create temp file $restore_file -- abort!\n\a"
    exit 1
fi

# reconfig ccd...
printf "deiccd object %s align\n" `slitmask` >> $tempfile
printf "deiccd mosmode Direct\n"             >> $tempfile
printf "deiccd obstype Object\n"             >> $tempfile

# include zeroth order command...
slider=`slider`
if [ "$slider" = "3" ]; then
    printf "deimot g3tltnam Zeroth_Order\n" >> $tempfile
elif [ "$slider" = "4" ]; then
    printf "deimot g4tltnam Zeroth_Order\n" >> $tempfile
else
    printf "[$cmd] ERROR: you must select slider 3 or 4 to run this command\n"
    exit 1
fi

# put additional settings into file...
if [ "$ampmode" = "$default" ]; then
    :
elif [ "$ampmode" = "A" ]; then
    printf "deiccd ampmode SINGLE:A\n" >> $tempfile
elif [ "$ampmode" = "B" ]; then
    printf "deiccd ampmode SINGLE:B\n" >> $tempfile
elif [ "$ampmode" = "A+B" ]; then
    printf "deiccd ampmode DUAL:A+B\n" >> $tempfile
else
    printf "[$cmd] WARNING: ignoring bad ampmode value $ampmode\n\a"
fi

if [ "$binning" != "$default" ]; then
    printf "deiccd binning $binning\n" >> $tempfile
fi

if [ "$exptime" != "$default" ]; then
    printf "deiccd ttime $exptime\n" >> $tempfile
fi

if [ "$filter" != "$default" ]; then
    printf "deimot DWFILNAM $filter\n" >> $tempfile
fi

if [ "$wmode" != "$default" ]; then
    printf "deiccd autopane $wmode\n" >> $tempfile
fi


# execute restore state if we got any changes...
if [ -s $tempfile ]; then
    command="$foo restore_state -verify $tempfile"
    $command
    if [ $? -ne 0 ]; then
	printf "[$cmd] ERROR: bad return from $command -- abort!\n\a"
	exit 1
    fi
fi

# now acquire an image...
printf "Starting goi.\n"
$foo goi

# cleanup is invoked at the end...
