#! /bin/csh -f
#
# take a number of C source code files, and extract there CTEX comment
# sections into a (la)tex file.
# Needs accompanying ctex.awk
#
set p=$0
set prog=$p:t
if ($#argv == 0) then
  echo "Usage: $prog [-latex|-tex] [-section|-subsection|-centerline] file(s)"
  echo "Extract TEX comments from C code which are structured as follows:"
  echo ""
  echo "    /*CTEX"
  echo "     *	... (la)tex stuff ..."
  echo "     */"
  echo "Output is to standard output."
  exit 0
endif

set mode=tex
set head=none
set header=1
set footer=1
set index=0
set ctexawk=$NEMO/src/scripts/ctex.awk

set n=0

foreach a ($*)
  switch ($a)
  case -ind*:
	set index=1
	breaksw
  case -te*:
	set mode=tex
	breaksw
  case -la*:
	set mode=latex
	breaksw
  case -ra*:
	set mode=raw
	breaksw
  case -se*:
	set head=section
	breaksw
  case -su*:
	set head=subsection
	breaksw
  case -ce*:
	set head=centerline
	breaksw
  case -nohead*:
	set header=0
	breaksw
  case -nofoot*:
	set footer=0
	breaksw
  default:
        set aa=`echo $a | sed 's/_/\\_/g'`
	if ($n == 0) then
            if ($mode != raw) then
                echo "% Do not edit this file; created by $prog on `date`"
            endif
	    if ($header) then
              if ($mode == latex) then
	        echo "\documentclass{report}"
                echo "\begin{document}"
              endif
	    endif
        endif	
        if ($head == section) then
            echo "\section{$aa:r}"
	else if ($head == subsection) then
            echo "\subsection{$aa:r}"
	else if ($head == centerline) then
            echo "\centerline{$aa:r}"
        endif
	if ($mode == latex) echo "\index{potential, $aa}"
        $ctexawk $a
        @ n++
	breaksw
  endsw
end
if ($footer) then
  if ($mode == tex) then
    echo '\bye'
  else if ($mode == latex) then
    echo '\end{document}'
  endif
endif
