#!/bin/perl -w
#+
# save_state -- store instrument settings for future restoration
#
# Purpose:
#	Generate a file listing the current INSTRUMENT settings.  This file
#	is similar in form to the files generated by XHIRES.  The
#	contents of the file can be parsed later by the restore_state
#	function in order to recover the original configuration.  By
#	default, the data will be written to a file in the user's home 
#	directory.  The user can provide an optional file name to use
#	instead. 
#
# Usage:
#	save_state [-all] [-detector] [-motors] [-info] [-fcs] [-tv] \
#		   [-clobber] [-verbose] [save_file]
# 
# Flags:
#	-(a)ll		save all INSTRUMENT parameters except INFO keywords;
#			equivalent to -dmtf
#	-(d)etector 	save CCD parameters
#	-(m)otors	save all motor settings
#	-(i)nfoman	save INFOMAN-only keywords when saving
#			CCD settings; these include FRAMENO, OUTDIR,
#			OUTFILE, TODISK, OBJECT, OBSERVER
#	-(f)cs		save flexure compensation system parameters
#	-(t)v		save tv guider parameters
#	-(c)lobber	clobber existing save file
#	-(v)erbose	print diagnostic output
#	
# Arguments:
#	save_file = name of the save file to generate.  The default is
#		to create the file ~/.save_state.YYYY-MMM-DD
# 
# Output:
#	Current INSTRUMENT settings are stored in the save file
# 
# Restrictions: 
#	- Unless the clobber flag is set, this script will not
#       	overwrite an existing save file 
#	- Hatch and lamp settings are never saved 
# 
# Exit values:
#	 0 = normal completion
#	 1 = wrong number of arguments
#
# Example:
#	1) Save detector and motor keywords to default file:
#		save_state
#
#	2) Save detector, motor and infoman keywords to default file:
#		save_state -i
# 
# 	3) Save detector, motor and infoman keywords to file ~/state1
#		save_state -i ~/state1
#
#	4) Overwrite existing default state file:
#		save_state -c
#-
# Modification history:
#	2002-Feb-28	GDW	Original version
#	2002-Jun-28	GDW	Adapted for DEIMOS
#	2003-Jul-29	GDW	Added AUTOPANE support
#	2003-Aug-26	GDW	Changed DWFOCRAW to FOCUSVAL
#	2003-Aug-28	GDW	Added FCS keywords
#	2006-Sep-14	GDW	Fixed handling of TV keywords
#	2012-Mar-15	GDW	Update to use 'strict'
#-----------------------------------------------------------------------

use strict;
do "/home/deimos/src/procs/info/state_defs.pl";

# variable declarations...
my( $value);
my( %option);
my( $save_ccd, $save_tv, $save_mot, $save_info, $save_fcs);
my( $clobber);
my( $verbose);
my( $debug);
my( $i);
my( $usage) = "Usage: save_state [-all] [-detector] [-motors] [-infoman] [-fcs] [-tv]\n [-clobber] [-verbose] [save_file]\n";
my( $max_args) = 1;
my( $save_file);
my( $keyword);
my( $lib);
my( $separator) = "#-----------------------------------------------------------------------\n";

# list subsystem keywords...
my(@ccd_keywords)  = qw( AMPLIST AMPMODE ANTIBLM AUTOPANE
			 AUTOSHUT BINNING CCDGAIN CCDSPEED ERASECNT KEEPPREP 
			 MOSMODE NSUBINT OBSTYPE OVRFLUSH POSTLINE 
			 POSTPIX PRECOL PREFLUSH PRELINE ROWSHFT 
			 TTIME WINDOW);
my(@info_keywords) = qw( FRAMENO OUTDIR OUTFILE TODISK OBJECT OBSERVER);
my(@mot_keywords)  = qw( DWFILNAM FOCUSVAL GRATENAM G3TLTWAV G4TLTWAV
			 SLMSKNAM);
my(@tv_keywords)   = qw( TVFILNAM TVFOCRAW);
my(@fcs_keywords)  = qw( FCSMODE);

# process switches...
$i = 0;
while( $i<=$#ARGV){
  $_ = $ARGV[$i];
  if( m/^-(.)/){
    $option{$1} = 1;
    shift;
  } else {
    $i++;
  }
}

# check for help flag...
exec "help save_state" if $option{"h"} or $option{"?"};

# check subsystems...
$save_ccd=1 if $option{"d"};
$save_mot=1 if $option{"m"};
$save_tv=1  if $option{"t"};
$save_fcs=1 if $option{"f"};

# if no systems specified, save ccd and motors...
if ($option{"a"}){
  $save_ccd=1;
  $save_mot=1;
  $save_tv=1;
  $save_fcs=1;
} elsif((not $save_ccd) and (not $save_mot) and (not $save_tv)){
  $save_ccd=1;
  $save_mot=1;
}

# check additional options...
$save_info=1 if $option{"i"};
$clobber=1   if $option{"c"};
$verbose=1   if $option{"v"};

# output...
if ( $verbose){
  print "Verbose mode enabled\n"    if $verbose;
  print "Clobber mode enabled\n"    if $clobber;
  print "Saving CCD settings\n"     if $save_ccd;
  print "Saving infoman settings\n" if $save_info;
  print "Saving motor settings\n"   if $save_mot;
  print "Saving TV settings\n"      if $save_tv;
  print "Saving FCS settings\n"     if $save_fcs;
}

# process remaining arguments...
if( $#ARGV >= 0){
  $save_file = shift
} else {
  $save_file = $X::save_file_default;
}

# complain if unparsed arguments remain...
die "$usage" if $#ARGV >= 0;

# check existing file...
if ( -e $save_file){
  if( $clobber){
    unlink $save_file
  } else {
    die "ERROR: save file $save_file exists; use -clobber to overwrite\n";
  }
}

# delete any old default save files, if a new one is to be written...
if( defined( $X::rootname) && $save_file eq $X::save_file_default) {
  unlink <$X::rootname*>
}

# open the output file...
open( SAVEFILE, ">$save_file")
  or die "Can't open save file $save_file";

# write header...
my( $buf) = scalar(localtime);
print SAVEFILE "$separator";
print SAVEFILE "# File:\t\t$save_file\n";
print SAVEFILE "# Generated on:\t$buf\n";
print SAVEFILE "# Generated by:\t$0\n";
print SAVEFILE "$separator";

# add infoman keywords if required...
if( $save_info){ push @ccd_keywords, @info_keywords};

# save CCD keywords...
if( $save_ccd){
  print SAVEFILE "# BEGIN CCD keywords\n";
  $lib = 'deiccd';
  foreach $keyword (@ccd_keywords) {
    $value = &Show( $lib, $keyword);
    &Save( $lib, $keyword, $value) if defined($value);
  }
  print SAVEFILE "# END CCD keywords\n";
}

# save FCS keywords...
if( $save_fcs){
  print SAVEFILE "# BEGIN FCS keywords\n";
  $lib = 'deifcs';
  foreach $keyword (@fcs_keywords) {
    $value = &Show( $lib, $keyword);
    &Save( $lib, $keyword, $value) if defined($value);
  }
  print SAVEFILE "# END FCS keywords\n";
}

# motor keywords...
if( $save_mot){
  print SAVEFILE "# BEGIN motor keywords\n";
  $lib = 'deimot';
  foreach $keyword (@mot_keywords) {
    $value = &Show( $lib, $keyword);
    &Save( $lib, $keyword, $value) if defined($value);
  }
  print SAVEFILE "# END motor keywords\n";
}

# TV keywords...
if( $save_tv){
  print SAVEFILE "# BEGIN tv keywords\n";
  $lib = 'deimot';
  foreach $keyword (@tv_keywords) {
    $value = &Show( $lib, $keyword);
    &Save( $lib, $keyword, $value) if defined($value);
  }
  print SAVEFILE "# END tv keywords\n";
}

# clean up and die...
close(SAVEFILE);
exit;
