#!/bin/sh
# Copyright 1991-1997 by Norman Ramsey.  All rights reserved.
# See file COPYRIGHT for more information.
# $Id: noweave.nw,v 1.6 1998/08/17 00:10:34 nr Exp nr $
LIB=/usr/pkg/share/noweb
markup=$LIB/markup backend=$LIB/totex
wrapper= delay= args= markopts= noweboptions= autodefs= verbose= shortxref=
noquote="-noquote" docanchor= noindex="-noindex"
filtera= filterb= filterc= filterd= filtere= filterf= filterg=
# following supported by change in totex back end
noquote=
using_index= using_xref=
pass2setcmd="set ignore"
for i do
  pass2setcmd="$pass2setcmd '$i'"
done
arg=
while [ $# -gt 0 ]; do
  case $1 in
    -latex)      if [ "$wrapper" != "none" ]; then wrapper=latex; fi ;;
    -tex)        wrapper=tex ;;
    -html)       if [ "$wrapper" != "none" ]; then wrapper=html; fi; 
                 backend="$LIB/tohtml -localindex"
                 noquote=""; docanchor="-docanchor 10" ;;
    -latex+html) if [ "$wrapper" != "none" ]; then wrapper=latex; fi; 
                 backend="$LIB/tohtml -localindex -raw"
                 noquote=""; docanchor="-docanchor 10" ;;
    -ascii)      wrapper=none
                 backend="$LIB/toascii" ;;
    -troff)      wrapper=mm
                 backend="$LIB/toroff" ;;
    -n)          wrapper=none ;;
    -backend)    backend="$2" ; shift ;;
    -markup)     markup="$2"  ; shift ;;
    -filter)    shift                           ;;
    -x)                           using_xref=1  ;;
    -index)            noindex= ; using_index=1 ;;                 
    -indexfrom) shift; noindex= ; using_index=1 ;;
    -autodefs) newfilter="$LIB/autodefs.$2"
               if [ -r $newfilter ]; then if   [ -z "$filtera" ]; then
                                            filtera="$newfilter"
                                          elif [ -z "$filterb" ]; then
                                            filterb="$newfilter"
                                          elif [ -z "$filterc" ]; then
                                            filterc="$newfilter"
                                          elif [ -z "$filterd" ]; then
                                            filterd="$newfilter"
                                          elif [ -z "$filtere" ]; then
                                            filtere="$newfilter"
                                          elif [ -z "$filterf" ]; then
                                            filterf="$newfilter"
                                          elif [ -z "$filterg" ]; then
                                            filterg="$newfilter"
                                          else
                                            echo "$0: ran out of filters" 1>&2
                                            exit 1
                                          fi
               else echo "$0: don't know how to find definitions for '$2'" 1>&2; exit 1
               fi
               shift
               ;;
    -showautodefs) foundautodefs=
                   for i in $LIB/autodefs.*; do
                     if [ -r $i ]; then
                       echo "This `basename $0` supports -autodefs $i" | 
                                           sed "s@$LIB/autodefs\.@@" 1>&2
                       foundautodefs=$i
                     fi
                   done
                   if [ -z "$foundautodefs" ]; then
                     echo "This `basename $0` does not support -autodefs"
                   fi; exit 1 ;;
    -option) newopt="$2"; shift
             if [ "X$newopt" = "Xshortxref" ]; then shortxref="-shortxref"; fi
             if [ "X$newopt" = "Xlongxref" ];  then shortxref="-longxref";  fi
             if [ -z "$noweboptions" ] ; then noweboptions="$newopt"
             else noweboptions="$noweboptions,$newopt"
             fi ;;
    # -nodelay) delay= ;;
    -delay)   delay="-delay" ; wrapper=none ;;
    -t*) markopts="$markopts $1" ;;
    -v) echo 'RCS version name $Name:  $' 1>&2
        echo 'RCS id $Id: noweave.nw,v 1.6 1998/08/17 00:10:34 nr Exp nr $' 1>&2
        verbose=1 ;;
    -)  arg="$arg $1" ;;
    -*) echo "$0: Unrecognized option '$1'" 1>&2 ; echo "Usage: $0 [options] [files]" 1>&2
                                                   echo "Options recognized are:" 1>&2
                                                   echo "-latex            Emit LaTeX with headers and trailers (default)." 1>&2
                                                   echo "-tex              Emit plain TeX with headers and trailers." 1>&2
                                                   echo "-html             Emit HTML with headers and trailers." 1>&2
                                                   echo "-latex+html       Assume LaTeX in documentation, but use HTML in code." 1>&2
                                                   # echo "-ascii            Emit ASCII." 1>&2
                                                   echo "-troff            Emit troff (actually GNU groff)." 1>&2
                                                   echo " -n               Don't use any header or trailer." 1>&2
                                                   echo "-markup frontend  Parse input with frontend (e.g., numarkup)." 1>&2
                                                   echo "-x                Use the default cross-referencer (needs LaTeX or HTML)." 1>&2
                                                   echo "-index            Create index using identifiers defined in input files." 1>&2
                                                   echo "-indexfrom defs   Create index of identifers listed in file defs." 1>&2
                                                   echo "-filter cmd       Filter through 'cmd' before weaving; cmd could prettyprint" 1>&2
                                                   echo "                  or perform other functions." 1>&2
                                                   echo "-autodefs lang    Source is in language 'lang'; find definitions automatically." 1>&2
                                                   echo "-showautodefs     List languages that can be used with -autodefs" 1>&2
                                                   echo '-option opt       Add \noweboptions{opt} to header (latex only)' 1>&2
                                                   echo "-delay            Delay markup until after first documentation chunk." 1>&2
                                                   echo "-tk               Expand tab stops every k columns" 1>&2
                                                   echo "-t                Copy tabs to the output" 1>&2
                                                   echo "-v                Print pipeline and RCS info on standard error" 1>&2; exit 1 ;;
    *)  arg="$arg $1" ;;
  esac
  shift
done
if [ -n "$using_xref" -a -n "$using_index" ]; then
  echo "$0: you may not use -x with -index or -indexfrom (drop the -x)" 1>&2
  exit 1
fi
if [ -z "$wrapper" ]; then wrapper=latex; fi
eval $pass2setcmd
shift
while [ $# -gt 0 ]; do
  case $1 in
    -filter) 
        newfilter="$2" ; shift ;                  if   [ -z "$filtera" ]; then
                                                    filtera="$newfilter"
                                                  elif [ -z "$filterb" ]; then
                                                    filterb="$newfilter"
                                                  elif [ -z "$filterc" ]; then
                                                    filterc="$newfilter"
                                                  elif [ -z "$filterd" ]; then
                                                    filterd="$newfilter"
                                                  elif [ -z "$filtere" ]; then
                                                    filtere="$newfilter"
                                                  elif [ -z "$filterf" ]; then
                                                    filterf="$newfilter"
                                                  elif [ -z "$filterg" ]; then
                                                    filterg="$newfilter"
                                                  else
                                                    echo "$0: ran out of filters" 1>&2
                                                    exit 1
                                                  fi ;;
    -x) newfilter="$LIB/noidx $docanchor $delay"; if   [ -z "$filtera" ]; then
                                                    filtera="$newfilter"
                                                  elif [ -z "$filterb" ]; then
                                                    filterb="$newfilter"
                                                  elif [ -z "$filterc" ]; then
                                                    filterc="$newfilter"
                                                  elif [ -z "$filterd" ]; then
                                                    filterd="$newfilter"
                                                  elif [ -z "$filtere" ]; then
                                                    filtere="$newfilter"
                                                  elif [ -z "$filterf" ]; then
                                                    filterf="$newfilter"
                                                  elif [ -z "$filterg" ]; then
                                                    filterg="$newfilter"
                                                  else
                                                    echo "$0: ran out of filters" 1>&2
                                                    exit 1
                                                  fi ;;
    -index)  
        newfilter="$LIB/finduses $noquote" ;      if   [ -z "$filtera" ]; then
                                                    filtera="$newfilter"
                                                  elif [ -z "$filterb" ]; then
                                                    filterb="$newfilter"
                                                  elif [ -z "$filterc" ]; then
                                                    filterc="$newfilter"
                                                  elif [ -z "$filterd" ]; then
                                                    filterd="$newfilter"
                                                  elif [ -z "$filtere" ]; then
                                                    filtere="$newfilter"
                                                  elif [ -z "$filterf" ]; then
                                                    filterf="$newfilter"
                                                  elif [ -z "$filterg" ]; then
                                                    filterg="$newfilter"
                                                  else
                                                    echo "$0: ran out of filters" 1>&2
                                                    exit 1
                                                  fi
        newfilter="$LIB/noidx $docanchor $delay"; if   [ -z "$filtera" ]; then
                                                    filtera="$newfilter"
                                                  elif [ -z "$filterb" ]; then
                                                    filterb="$newfilter"
                                                  elif [ -z "$filterc" ]; then
                                                    filterc="$newfilter"
                                                  elif [ -z "$filterd" ]; then
                                                    filterd="$newfilter"
                                                  elif [ -z "$filtere" ]; then
                                                    filtere="$newfilter"
                                                  elif [ -z "$filterf" ]; then
                                                    filterf="$newfilter"
                                                  elif [ -z "$filterg" ]; then
                                                    filterg="$newfilter"
                                                  else
                                                    echo "$0: ran out of filters" 1>&2
                                                    exit 1
                                                  fi ;;
    -indexfrom)
        newfilter="$LIB/finduses $noquote $2";    if   [ -z "$filtera" ]; then
                                                    filtera="$newfilter"
                                                  elif [ -z "$filterb" ]; then
                                                    filterb="$newfilter"
                                                  elif [ -z "$filterc" ]; then
                                                    filterc="$newfilter"
                                                  elif [ -z "$filterd" ]; then
                                                    filterd="$newfilter"
                                                  elif [ -z "$filtere" ]; then
                                                    filtere="$newfilter"
                                                  elif [ -z "$filterf" ]; then
                                                    filterf="$newfilter"
                                                  elif [ -z "$filterg" ]; then
                                                    filterg="$newfilter"
                                                  else
                                                    echo "$0: ran out of filters" 1>&2
                                                    exit 1
                                                  fi
        newfilter="$LIB/noidx $docanchor $delay"; if   [ -z "$filtera" ]; then
                                                    filtera="$newfilter"
                                                  elif [ -z "$filterb" ]; then
                                                    filterb="$newfilter"
                                                  elif [ -z "$filterc" ]; then
                                                    filterc="$newfilter"
                                                  elif [ -z "$filterd" ]; then
                                                    filterd="$newfilter"
                                                  elif [ -z "$filtere" ]; then
                                                    filtere="$newfilter"
                                                  elif [ -z "$filterf" ]; then
                                                    filterf="$newfilter"
                                                  elif [ -z "$filterg" ]; then
                                                    filterg="$newfilter"
                                                  else
                                                    echo "$0: ran out of filters" 1>&2
                                                    exit 1
                                                  fi
        shift ;;
  esac
  shift
done
(
header=
case $wrapper in
  none) ;;
  latex)  header="@header $wrapper $noweboptions" 
          trailer="@trailer $wrapper" ;;
  *)  header="@header $wrapper$arg" 
      trailer="@trailer $wrapper" ;;
esac
if [ -n "$header" ]; then echo "$header"; fi
PATH="$PATH:$LIB" 
export PATH
if [ -n "$verbose" ]; then
  if [ -n "$header" -o -n "$trailer" ]; then
    parenpfx="("
    if [ -n "$header" ]; then echo "(echo $header" 1>&2; parenpfx=" "; fi
    echo "$parenpfx$markup" $markopts $arg 1>&2
    if [ -n "$trailer" ]; then echo " echo $trailer" 1>&2; fi
    echo ") |" 1>&2
  else
    echo $markup $markopts $arg '|' 1>&2
  fi
  if   [ -n "$filtera" ]; then echo $filtera '|' 1>&2; fi
  if   [ -n "$filterb" ]; then echo $filterb '|' 1>&2; fi
  if   [ -n "$filterc" ]; then echo $filterc '|' 1>&2; fi
  if   [ -n "$filterd" ]; then echo $filterd '|' 1>&2; fi
  if   [ -n "$filtere" ]; then echo $filtere '|' 1>&2; fi
  if   [ -n "$filterf" ]; then echo $filterf '|' 1>&2; fi
  if   [ -n "$filterg" ]; then echo $filterg '|' 1>&2; fi
fi
if [ -n "$verbose" ]; then echo $backend $noindex $delay $shortxref 1>&2; fi
if   [ -z "$filtera" ]; then
  $markup $markopts $arg
elif [ -z "$filterb" ]; then
  $markup $markopts $arg | eval $filtera
elif [ -z "$filterc" ]; then
  $markup $markopts $arg | eval $filtera | eval $filterb
elif [ -z "$filterd" ]; then
  $markup $markopts $arg | eval $filtera | eval $filterb | eval $filterc
elif [ -z "$filtere" ]; then
  $markup $markopts $arg | eval $filtera | eval $filterb | eval $filterc |
                           eval $filterd
elif [ -z "$filterf" ]; then
  $markup $markopts $arg | eval $filtera | eval $filterb | eval $filterc |
                           eval $filterd | eval $filtere
elif [ -z "$filterg" ]; then
  $markup $markopts $arg | eval $filtera | eval $filterb | eval $filterc |
                           eval $filterd | eval $filtere | eval $filterf
else
  $markup $markopts $arg | eval $filtera | eval $filterb | eval $filterc |
                           eval $filterd | eval $filtere | eval $filterf |
                           eval $filterg
fi
if [ -n "$trailer" ]; then echo $trailer; fi
) |
                                $backend $noindex $delay $shortxref
exit $?
