#!/bin/sh

########################################
#
# nts: NTS wrapper script to safely call NTS in Java.
#
# Copyright (C) 2001 Bernd Raichle.  Public domain.
#
#
# History:
#   2001/09/13 br v0.1
#     first version
#   2001/09/14 br v0.2
#     added -log-extension, -dvi-extension, version()
#     debugged and extended argument parsing
#   2001/09/18 br v0.3
#     added version of NTS
#     allowed options -<name> <value> in addition to -<name>=<value>
#     replaced occurences of `.fmt' by `.nfmt'
#

version=0.3


######################################################################
# abort(errmsg)
#   print `errmsg' to stderr and exit with error code 1
######################################################################
abort()
{
  echo "$progname: $1." >&2
  exit 1
}

###############################################################################
# version()
#   display version numbers and exit
###############################################################################
version()
{
  echo "$progname: NTS wrapper script version $version"

  echo "--- NTS version is:"
  find_nts_class
  java $nts_class_arg < /dev/null | head -1
  echo "--- NTS is using the following Java JDK version:"
  java -version
  echo "--- NTS and this script are using the kpathsea library:"
  kpsewhich -version
  exit 1
}

###############################################################################
# help()
#   display help message and exit
###############################################################################
help()
{
  cat <<END_OF_HELP
Usage: $progname [OPTION]... [TEXNAME[.tex]] [COMMANDS]
   or: $progname [OPTION]... \FIRST-LINE
   or: $progname [OPTION]... &FMT ARGS
END_OF_HELP

  cat <<'END_OF_HELP'
  Run NTS on TEXNAME, usually creating TEXNAME.dvi.
  Any remaining COMMANDS are processed as NTS input, after TEXNAME is read.
  If an existing .nfmt file is given using &FMT or the -fmt=FMT option, use it.
  Else use `NAME.nfmt', where NAME is the program invocation name, most
  commonly `tex'.  To be a bit smart, if the given .nfmt does not exist and
  contains the string `nts', the format file search is tried with `nts'
  removed or replaced by the string `tex'.

  Alternatively, if the first non-option argument begins with a backslash,
  interpret all non-option arguments as a line of TeX input.

  Alternatively, if the first non-option argument begins with a &, the
  next word is taken as the FMT to read, overriding all else.  Any
  remaining arguments are processed as above.

  If no arguments or options are specified, prompt for input.

-fmt=FMTNAME             use FMTNAME instead of program name
-ini                     be NTS' variant of initex, for dumping formats
-interaction=STRING      set interaction mode (STRING=batchmode/nonstopmode/
                          scrollmode/errorstopmode)
-kpathsea-debug=NUMBER   set path searching debugging flags according to
                          the bits of NUMBER
-log-extension=EXT       use EXT as the file name extension of the protocol
                          file (default is `log')
-dvi-extension=EXT       use EXT as the file name extension of the .dvi file
                          (default is `dvi')
-progname=STRING         set program (and fmt) name to STRING
-help                    display this help and exit
-version                 output version information and exit

Email bug reports to nts@dante.de.
END_OF_HELP

  exit
}


###############################################################################
# run_nts()
#   run NTS by calling java with NTS Jar file and appropriate arguments
###############################################################################
run_nts()
{
  #echo "run_nts(>$common_args< >$nts_class_arg< >$rest_args<)"
  java $common_args $nts_class_arg $rest_args
}



###############################################################################
# find_nts_class()
#   locate either the NTS jar file or the directory with the Nts.class file 
###############################################################################
find_nts_class()
{
  jarfile=nts.rt.jar
  classfile=Nts.class
  jarloc=`kpsewhich --format='other binary files' --progname=nts "$jarfile"`

  if test -f "$jarloc"; then
    nts_class_arg="-jar $jarloc"
  else
    classloc=`kpsewhich --format='other binary files' --progname=nts "$classfile"`

    test -f "$classloc" || abort "NTS Java Executable $jarfile or $classfile not found"

    classpath=`dirname "$classloc"`
    nts_class_arg="-classpath $classpath Nts"
  fi
}






########################################
#
# Initialize variables
#

progname=`basename $0`

prog_name=''
format_name="$progname"
interaction_mode=''
log_extension=''
dvi_extension=''


########################################
#
# Argument parsing ...
#
while
  test $# -gt 0 || break
  case "$1" in
    --progname=*|-progname=*)
        prog_name=`expr "$1" : '-*progname=\(.*\)'`
	format_name="$prog_name"
	;;
    --progname|-progname)
        shift; prog_name="$1"; format_name="$prog_name" ;;
    --fmt=*|-fmt=*)
        format_name=`expr "$1" : '-*fmt=\(.*\)'` ;;
    --fmt|-fmt)
        shift; format_name="$1" ;;
    \&*)
	format_name=`expr "$1" : '\&\(.*\)'` ;;
    --ini|-ini)
	format_name='' ;;
    --help|-help)
        help ;;
    --version|-version)
	version ;;
    --interaction=*|-interaction=*)
        interaction_mode=`expr "$1" : '-*interaction=\(.*\)'` ;;
    --interaction|-interaction)
        shift; interaction_mode="$1" ;;
    --kpathsea-debug=*|-kpathsea-debug=*)
	kpathsea_debug=`expr "$1" : '-*kpathsea-debug=\(.*\)'`
	export KPATHSEA_DEBUG
	KPATHSEA_DEBUG="$kpathsea_debug"
	;;
    --kpathsea-debug|-kpathsea-debug)
	kpathsea_debug="$1"
	export KPATHSEA_DEBUG
	KPATHSEA_DEBUG="$kpathsea_debug"
	;;
    --mktex=*|-mktex=*) ;;
    --mktex|-mktex) shift ;;
    --no-mktex=*|-no-mktex=*) ;;
    --no-mktex|-no-mktex) shift ;;
    --log-extension=*|-log-extension=*)
	log_extension=`expr "$1" : '-*log-extension=\(.*\)'` ;;
    --log-extension|-log-extension)
	shift; log_extension="$1" ;;
    --dvi-extension=*|-dvi-extension=*)
	dvi_extension=`expr "$1" : '-*dvi-extension=\(.*\)'` ;;
    --dvi-extension=*|-dvi-extension=*)
	shift; dvi_extension="$1" ;;
    "") break ;;
    *)  break ;;
  esac
do test $# -gt 0 && shift; done



########################################
#
# Test installed Java version ...
#
java_version=`java -version 2>&1 | head -1 | sed 's/^.*version "\(.*\)"/\1/'`

case "$java_version" in
  0.*|1.0*|1.1*)
    echo 'NTS needs Java JDK 1.2 or newer to work,'
    echo "Your standard Java version $java_version is too old."
    echo 'Please update to JDK 1.2 or 1.3 and make sure that the'
    echo 'command >java< points to the newer Java version.'
    exit 1
    ;;
  1.2*|1.3*)
    ;;
  "")
    echo 'Warning: Your standard Java version supplies no version number'
    echo '         I proceed with fingers crossed ...'
    ;;
  *)
    # Can we assume that NTS will run with JDK > 1.3?
    ;;
esac


########################################
#
# Locate NTS Jar file ...
#
find_nts_class



########################################
#
# Locate given NTS format file
#
fmtloc=''

if test ! -z "$format_name"; then

  format_names="$format_name"

  # This script wants to be smart:
  # If the format name contains the string "nts",
  # add more possible format names where "nts" is removed
  # or replaced by "tex".
  # If this script is called "nts", the format "tex" can be found,
  # for "ntslatex" the format "latex" can be found etc.
  #
  case "$format_name" in
    nts*)
        new_format_name=`echo "$format_name" | sed 's/^nts//'`
        format_names="$format_names $new_format_name"
	new_format_name=`echo "$format_name" | sed 's/nts/tex/'`
        format_names="$format_names $new_format_name"
	;;
    *nts)
        new_format_name=`echo "$format_name" | sed 's/nts$//'`
        format_names="$format_names $new_format_name"
        new_format_name=`echo "$format_name" | sed 's/nts/tex/'`
        format_names="$format_names $new_format_name"
	;;
    *nts*)
        new_format_name=`echo "$format_name" | sed 's/nts/tex/'`
        format_names="$format_names $new_format_name"
	;;
  esac

  #echo "fmtnames= $format_names."

  for fmt in $format_names; do

    fmtloc=`kpsewhich --format='other binary files' --progname=nts "${fmt}.nfmt"`

    #echo "FmtLoc: $fmt => $fmtloc"

    if test ! -z "$fmtloc" -a -f "$fmtloc"; then
      #echo "found"
      format_name="$fmt"
      break;
    fi
  done

fi


#
# A format file name was given, thus we are not NTS' initex (-ini).
# If we can't find the NTS .nfmt file, it is better to abort
# than leaving the user with an unexpected * prompt...
#
if test ! -z "$format_name" -a -z "$fmtloc"; then
  abort "Can't find requested NTS .nfmt file >$format_name<"
fi



########################################
#
# Create Argument list for NTS
#
common_args=''
rest_args=''


#
# 1. Create special arguments 
#
test -z "$fmtloc" || \
    common_args="$common_args -Dnts.fmt=$fmtloc"
test -z "$interaction_mode" || \
    common_args="$common_args -Dnts.interaction=$interaction_mode"
test -z "$prog_name" || \
    common_args="$common_args -Dnts.progname=$prog_name"
test -z "$log_extension" || \
    common_args="$common_args -Dnts.log.extension=$log_extension"
test -z "$dvi_extension" || \
    common_args="$common_args -Dnts.dvi.extension=$dvi_extension"

#
# 2. Add all remaining arguments from command line
#
rest_args="$@"

run_nts

#
# -- end of file --
