#!/bin/sh
#
# Copyright 1991 by Norman Ramsey.  All rights reserved.
# See file COPYRIGHT for more information.
# set -x
LIB=/usr/pkg/share/noweb
markup=$LIB/markup opt= arg= filters=
width=72 subst='gsub("\\*/", "* /", s)' format='/* %%-%ds */'

while [ $# -gt 0 ]; do
	case $1 in
        -ml|-m3) format='(* %%-%ds *)' ;        
                 subst='gsub("\\*\\)", "* )", s); gsub("\\(\\*", "( *", s)' ;;
        -awk|-icn|-icon)    format='# %%-%ds' ; subst=' ' ;;
	-lisp|-scm)	    format=';;; %%-%ds' ; subst=' ' ;;
        -matlab) format='%%%% %%-%ds' ;         subst=' ' ;;
        -c++)    format='// %%-%ds' ;           subst=' ' ;;
        -c)      format='/* %%-%ds */'          subst='gsub("\\*/", "* /", s)' ;;
	-pascal) format='{ %%-%ds }' ;          subst='gsub("[{}]", "-", s)' ;;
        -f77)    format='C %%-%ds' ;            subst=' ' ;;
        -f90)    format='! %%-%ds' ;            subst=' ' ;;
        -tex)    format='%%%% %%-%ds' ;         subst=' ' ;;
        -L*) ;; # deliberately ignore requests for #line
        -w[0-9][0-9]*)    width=`echo $1 | sed 's/^-w//'` ;;
	-filter) filters=" | $filters $2 " ; shift ;;
	-markup) markup="$2" ; shift ;;
	-)  arg="$arg '$1'" ;;
        -*) opt="$opt '$1'" ;;
        *)  arg="$arg '$1'" ;;
        esac
	shift
done

PATH="$PATH:$LIB" eval "$markup $arg $filters" |
awk 'BEGIN { line = 0; capture = 0; format=sprintf("'"$format"'",'"$width"') }

function comment(s) {
    '"$subst"'
    return sprintf(format,s)
}

function grab(s) {
  if (capture==0) print
  else holding[line] = holding[line] s
}
   
/^@end doc/ { capture = 0; holding[++line] = "" ; next }
/^@begin doc/ { capture = 1; next }

/^@text /     { grab(substr($0,7)); next}
/^@quote$/    { grab("[[") ; next}
/^@endquote$/ { grab("]]") ; next}

/^@nl$/ { if (capture !=0 ) {
            holding[++line] = ""
          } else if (defn_pending != 0) {
	    print "@nl"
            for (i=0; i<=line && holding[i] ~ /^ *$/; i++) i=i
            for (; i<=line; i++) printf "@text %s\n@nl\n", comment(holding[i])
            line = 0; holding[0] = ""
            defn_pending = 0
          } else print
          next  
        }

/^@defn / { holding[line] = holding[line] "<"substr($0,7)">=" # (line should be blank)
            print ; defn_pending = 1 ; next }
{ print }' |
eval "$LIB/nt $opt"
