#!perl   # just to get emacs into perl mode when editing this

package preliminary;

use strict;
use English '-no_match_vars';
use GameConfig;
use Getopt::Long;

local $WARNING = 1;     # same as usual -w option
local $OUTPUT_AUTOFLUSH = 1;


Getopt::Long::config("default");
Getopt::Long::config(qw( auto_abbrev bundling permute ));

my %opt;
my %optstate = ();
# defaults:
$opt{config} = "bin/packets.config";

Getopt::Long::GetOptions
  (\%opt, 
   "config=s", 
   "xdvi|x", "latex|l",
   "only" => \&inclusion, "except" => \&inclusion,
   "help|h" => sub { die "\n" })
  or &usage;
# fields are left in @ARGV

sub inclusion {
  my ($flag, $val) = @_;
  defined $optstate{"inclusion"} and &usage;  # can't do it twice
  $optstate{"inclusion"} = $flag eq "except"; # default "do this extract?"  
}

sub usage { 
  die "Syntax: gmX preliminary [options] [fields]
    Order of arguments is completely irrelevant; single-character
      switches may be bundled (-abc for -a -b -c).
    --only causes only the given (list-type) extractables to be done;
    --except causes all except the given ones to be done.
    If neither is specified, the default is --except if no fields
    are given (ie do everything) and --only if any are (ie do only those).
    --help, -h           print this usage message and exit
    --config=configfile  use 'configfile' instead of bin/packets.config
    --latex, -l          latex the list-type .tex's into their DVI dirs
    --xdvi, -x           xdvi the list-type DVI dirs' .dvi
  See html/printing-packets.html for details.\n";
}

$opt{xdvi} or $opt{latex} 
  or print "At least one of --latex, --xdvi must be specified.\n" and &usage;

### Now that we know what the config file is, read it:
my %config;  # misc config file information
my %extract; # config file defns of extractable fields (incl charsheets)
GameConfig::packets_config($opt{config}, \%config, \%extract);

### Now that we know what the extracts are, determine inclusion:
# If not specified, we do only the given fields, but all if none given
defined $optstate{"inclusion"} or $optstate{"inclusion"} = not @ARGV;
# Set all to default
for my $field (grep { $extract{$_}{"type"} eq "list" } keys %extract) { 
  $extract{$field}{"do"} = $optstate{"inclusion"};
}
# Flip the specified ones
while (my $arg = shift) {
  my $field = GameConfig::match_extractable(\%extract, $arg);
  $extract{$field}{"type"} eq "list"
    or die "$field is not a list-type extractable\n";  
  $extract{$field}{"do"} = not $optstate{"inclusion"};
}

my @dofield = sort grep { $extract{$_}{"do"} } keys %extract
  or die "no extractable fields specified to process\n";

### Ok, now do it.
for my $field (@dofield) {
  print "\t$field ";
  # Foo Bar Sheets have .tex in FooBarSheets (or wherever .structure
  # redirects that too) and .dvi in that/DVI (ditto)
  my $compress = $extract{$field}{"compress"};
  GAME::chdir $compress;
  my @file = sort grep { !/^\./ and -f and s/\.tex$// and !/^template$/ 
		       } GM::contents;
  if (@file) {
    print "\n";
    GAME::chdir "$compress/DVI";
    for my $file (@file) {
      print "\t\t$file\n";
      $opt{latex} and GAME::latex "$compress/$file";
      $opt{xdvi} and GM::xdvi $file;
    }
  }
  else {
    print "(none)\n";
  }
}

print "Preliminary ops on list-type extractables done.\n";
