#!/bin/perl -I/usr/local/lib/perl5
#+
# consort2 -- print sorted list of current configurable elements
#
# Purpose:
#	Output a list showing which filters, gratings, and/or
#	slitmasks are currently loaded.  By default, prints out all
#	list of slitmasks, gratings, and science filters.
#	Use command-line flags to select only certain types or to
#	suppress labels.
#
# Usage:
#	consort [-s] [-f] [-g] [-t] [-n]
#
# Arguments:
#	-s = show slitmask list
#	-f = show filter list
#	-g = show grating list
#	-t = show tv filter list
#	-n = print no labels (only element names)
#
# Output:
#	The list of requested elements is printed to STDOUT
#
# Restrictions:
#	Dependent on information obtained from the DREMEL output file
#	mapRON.cfg
#
# Exit values:
#	 0 = normal completion
#	 1 = wrong number of arguments
#
# Example:
#	1) Print the list of all elements:
#		[101] deimos@polo> consort
#		# GRATINGS
#		     1 NonOp
#		     2 Mirror
#		     3 1200G
#		     4 600ZD
#		# FILTERS
#		     1 GG455
#		     2 V
#		     3 R
#		     4 Z
#		     5 BAL12
#		     6 I
#		     7 OG550
#		# SLITMASKS
#		     2 2219.E
#		     3 Long1.0
#		     4 2152.W
#		     5 2149.W
#		     6 2155.WB
#		     7 3152.E
#		     8 GOH_foo
#		     9 3155.E
#		    10 3217.W
#		    11 3211.W
#		    12 3215.W
#		    13 Empty_13
#
#	2) Print the list of gratings:
#		[102] deimos@polo> consort -g
#		# GRATINGS
#		     1 NonOp
#		     2 Mirror
#		     3 1200G
#		     4 600ZD
#
#	3) Print the list of slitmasks, with no labels:
#		[103] deimos@polo> consort -n -s
#		2219.E
#		Long1.0
#		2152.W
#		2149.W
#		2155.WB
#		3152.E
#		GOH_foo
#		3155.E
#		3217.W
#		3211.W
#		3215.W
#		Empty_13
#-
# Modification history:
#	2003-Jul-31	GDW	Original version
#	2006-Sep-14	GDW	Added TV filters
#       2009-Mar-18     MK      Adapted from consort: modified 
#                               to work with consortWriteDeimos
#-----------------------------------------------------------------------

use Getopt::Std;

#This had to be hard coded for the call from consortWriteDeimos to work
my( $MAPRON ) = "/local/kroot/data/deimot/dyna/mapRON.cfg";
my( $usage) = "Usage: $0 [ -s | -g | -f | -t | -n ]\n";

# set defaults...
my( $print_all ) = 0;
my( $print_slitmasks) = 0;
my( $print_gratings) = 0;
my( $print_filters) = 0;
my( $print_tvfilters) = 0;
my( $print_labels) = 1;
my( %slitmask, %grating, %filter, %tvfilter);

# process command line switches...
getopts("sgfnt");
unless (defined($opt_s) or
	defined($opt_g) or
	defined($opt_f) or
	defined($opt_t)){
  $print_all=1 
};
$print_slitmasks = 1 if (defined($opt_s) or $print_all);
$print_gratings  = 1 if (defined($opt_g) or $print_all);
$print_filters   = 1 if (defined($opt_f) or $print_all);
$print_tvfilters = 1 if (defined($opt_t));
$print_labels    = 0 if (defined($opt_n));



# parse file...
open MAPRON, "<$MAPRON" or die "Can't find file $MAPRON";
while( <MAPRON> ){
  chomp;
  next unless m/^deimot\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.+)$/;
  $stage = $2;
  $position = $4;
  $name = $6;

  # skip bogus entries (home positions, etc.)...
  next unless ($position > 0);

  if ( $stage eq 'SLMSK' ){ $slitmask{$position} = $name};
  if ( $stage eq 'DWFIL' ){ $filter{$position}   = $name};
  if ( $stage eq 'GRATE' ){ $grating{$position}  = $name};
  if ( $stage eq 'TVFIL' ){ $tvfilter{$position} = $name};
};
close MAPRON;

# print results...
if( $print_gratings){
  print "# GRATINGS\n" if $print_labels;
  foreach $key (sort numerically keys %grating){
    printf "%6d=", $key if $print_labels;
    print "$grating{$key}\n";
  }
}

if( $print_filters){
  print "# FILTERS\n" if $print_labels;
  foreach $key (sort numerically keys %filter){
    printf "%6d=", $key if $print_labels;
    print "$filter{$key}\n";
  }
}

if( $print_slitmasks){
  print "# SLITMASKS\n" if $print_labels;
  foreach $key (sort numerically keys %slitmask){
    printf "%6d=", $key if $print_labels;
    print "$slitmask{$key}\n";
  }
}

if( $print_tvfilters){
  print "# TVFILTERS\n" if $print_labels;
  foreach $key (sort numerically keys %tvfilter){
    printf "%6d=", $key if $print_labels;
    print "$tvfilter{$key}\n";
  }
}

sub numerically { $a <=> $b; }
