#!/bin/csh
# front end to mapit program

# where the mapit program and data files live
set MAPDIR = "/usr/pkg/share/mapit"

# the mapit command line...
set MAPIT = "/usr/pkg/libexec/mapit"
setenv ARGS ""
set OUTPUT = "diplomacy.map.$$"
set EXIT = 0

# what to do with the output
set COMMAND = 'echo output is in $OUTPUT'

# by default, we map a regular diplomacy game
setenv MAPPS   "$MAPDIR/regular.map.ps"
setenv MAPINFO "$MAPDIR/regular.info"

while ($#argv)
  switch ($1)
  case -b:  # make new borders
	setenv ARGS "$ARGS $1"
	breaksw
  case -e:  # execute the specified command
	shift
	set COMMAND = $1
	breaksw
  case -M:  # message (override)
  case -m:  # message (default)
  case -T:  # title (override)
  case -t:  # title (default)
	setenv ARGS "$ARGS $1 '$2'"
	shift
	breaksw
  case -n:  # make this the name of postscript file
	shift
	set OUTPUT = "$1.ps"
	breaksw
  case -o:  # make this the name of file, without suffix
	shift
	set OUTPUT = $1
	breaksw
  case -p: # print.  assumes default printer accepts postscript
	set COMMAND = 'echo "output is in $OUTPUT, printing"; lpr $OUTPUT'
	breaksw
  case -s:  # only print final status summary map
	setenv ARGS "$ARGS $1"
	breaksw
  case -v: # variant if followed by name or (ghost)view if not
	if (-e $MAPDIR/$2.map.ps && -e $MAPDIR/$2.info) then
	  setenv MAPPS   "$MAPDIR/$2.map.ps"
	  setenv MAPINFO "$MAPDIR/$2.info"
	  echo "$2 map selected"
	  shift
	else
	  set COMMAND = 'ghostview -magstep 1 -landscape -nocenter $OUTPUT'
	endif
	breaksw
  case -vc: # colour variant if followed by name
	if (-e $MAPDIR/$2.cmap.ps && -e $MAPDIR/$2.info) then
	  setenv MAPPS   "$MAPDIR/$2.cmap.ps"
	  setenv MAPINFO "$MAPDIR/$2.info"
	  echo "$2 colour map selected"
	  shift
	else
	  echo "unknown colour variant: $2"
	  exit 1
	endif
	breaksw
  case -h:
HELP:
	echo "-b        : make new borders"
	echo "-e <cmd>  : execute the specified command after producing map"
	echo "-h        : display list of switches and variants"
	echo "-M <text> : message (override)"
	echo "-m <text> : message (default)"
	echo "-n <name> : set output file to be <name>.ps"
	echo "-o <name> : set output file to be <name>"
	echo "-p        : print map to default printer"
	echo "-s        : only produce final status summary map"
	echo "-T <text> : title (override)"
	echo "-t <text> : title (default)"
	echo "-v        : execute ghostview after producing map"
	echo "-v <var>  : use black & white map for specified variant"
	echo "-vc <var> : use colour map for specified variant"
	echo ""
	echo "Supported Variants (default is B&W regular):"
	set VARIANTS = $MAPDIR/*.map.ps
	foreach VAR ($VARIANTS)
	  set VAR = $VAR:t
	  set VAR = $VAR:r
	  echo -n "$VAR:r "
	end
	echo ""
	exit $EXIT
  default:
	echo "unknown switch (or variant): $1"
	echo ""
	set EXIT = 1
	goto HELP
  endsw

  shift
end

eval $MAPIT $ARGS > $OUTPUT
eval $COMMAND

