#! /bin/csh 
#
# Create an indexed web of source code listings.  
# 
# Usage: src2www [ -o webdir ] [-lines 8] [-f ] srcfile srcfile ...
#
#
#
# Examples:  
#   src2www foo.adb     ## creates foo.adb.html and INDEX.html in default dir
#   src2www -o mywebdir *ads *.adb  *.c *.h  ## now INDEX.html is useful ...
#   src2www -o srcweb -f *.a *.c *.h         ## needed if directory is huge 
#   src2www -o srcweb -lines 5 *.ads *.adb   ## number every 5th line
#
#    This script guesses 
#    the source language based on the file name. Currently it guesses
#      *.c, *.C, *.h, *.H, *.cc  =>  Ansii C or restricted C++ 
#      *.a, *.ads, *.adb, *.ada => Ada83 or Ada95
#    It should be fairly obvious how to change the guess system
#
#  Bug reports and other correspondence to: src2www@cs.purdue.edu
#	
#
## set echo  ## for debugging only
#BEGIN CONFIGURE
echo "Script  has not been configured"
echo 'Edit file "Configuration.src", save as "Configuration"'
echo '  and then type "make wwwbin" '
exit(1)
#END CONFIGURE
#(but you may need to augment the file-suffix matching patterns below)
# 
## Self-destruct if not configured 
   if (! -d ${bindir}) then 
	echo "*** Configuration problem ***"
	echo "*** $bindir is not an accessible directory ***"
	echo "*** Edit file 'Configuration' or 'Configuration.src' ***"
	exit 1
   else if (! -e ${bindir}/Configuration) then
	echo "*** Didn't find file 'Configuration' in $bindir ***"
	echo "*** Edit file Configuration.src and save as Configuration ***"
	exit 1
   else if (! -x ${bindir}/ada2html) then
	echo "*** I didn't find an expected executable (ada2html)"
	echo "*** Is $0 configured correctly?"
	echo "*** Be sure to edit Configuration.src, save it as "
	echo "***    Configuration and then type 'make wwwbin'"
	exit 1
   else 
	echo "Configuration in $bindir looks ok ..."
   endif

##
##  Argument parsing loop
##
##  Note: For huge directories, csh blows up if you try to build a 
##        variable listing all files.  I can't build a web of gnat's 
##	  gcc directory.  Workaround: -f means everything else is 
##	  file names, in which case we pick it up directly from $*
##
set input_files = 
set filter_args = 
getarg:

  switch ($1:q)
  	
      case -o: 
	 ## Output directory 
         if ($#argv < 2  ||  $2:q =~ -*) then
            echo  $1:q option must have argument
            exit 1
         else
	    set webdir = "$2:q"
            shift
            breaksw
         endif

     case -lines:
     case +lines: 
         if ($#argv < 2  ||  $2:q =~ -*) then
            echo  $1:q option must have argument
            exit 1
         else
	    set  filter_args = "$filter_args +lines $2:q "
            shift
            breaksw
         endif

      case -f: 
	 ## skip the rest, assume they are input files
         shift
	 goto skipargs
         
      default:
         set input_files = "$input_files $1:q"
   endsw

if ($#argv > 0) then
   shift
   goto getarg
endif

skipargs: 

## Configuration note: The patterns below are for common file naming
## standards I am aware of, but you may need to add patterns for your
## local environment. 

foreach f ($input_files $*) 

  ## Maintenance note: We should add a way to "force" the language
  ## here, e.g., if program name is ada2www then use ada2html regardless
  ## of source file name. 

  switch ($f:q)
      case *.ad[bs]: 
      case *.a: 
      case *.ada: 
	set filter = ${bindir}/ada2html
	breaksw;
      case *.[cChH]: 
      case *.hh:
      case *.cc++:
      case *.cc: 
      case *.c++: 
	set filter = ${bindir}/c2html
	breaksw;
      default: 
	echo "Unable to guess file type of $f ; skipping file"
	goto skipfile
  endsw

  set fbase = `basename $f`

  echo "Processing $f with $filter"
  ## Basic filtering to produce an html file
  expand $f | ${filter} ${filter_args}  >! ${webdir}/${fbase}${html_suffix}
  ## Extract markers for indexing
  ${awk} -v File=${fbase}${html_suffix}  -f ${awkpath}/html_markers.awk \
	${webdir}/${fbase}${html_suffix} >! ${tmpdir}/$$.index
  ## In addition to index within this file, save for global index
  cat ${tmpdir}/$$.index >>! ${tmpdir}/$$.gindex
  ## Process the list of anchors to create an html index
  ${awk} -f ${awkpath}/html_index.awk ${tmpdir}/$$.index \
	| cat ${webdir}/${fbase}${html_suffix} -  >! ${webdir}/tmp$$
  ## Wrap standard html elements around it and replace the original 
  ## .html file with the indexed version.  We also remove the TITLE
  ## attribute from anchors with html_clean.awk,
  ## to work around a bug in the LYNX browser. 
  ${rm} ${webdir}/${fbase}${html_suffix}
  cat ${webdir}/tmp$$ | ${awk} -f ${awkpath}/html_clean.awk |\
	${bindir}/html_wrap -title "${fbase}" >! \
	${webdir}/${fbase}${html_suffix}

skipfile: 
end

${rm} -f ${webdir}/tmp$$

echo "*... building global index" 
## The processing of each file above left a list of tags
## in $$.gindex.  We will arrange those tags alphabetically
## and build a global index to all the web pages built in 
## this run.  (Maintenance note:  There should be a way to 
## do this incrementally when files are added/modified.) 

## Alphabetize by declared entity name
sort -df +2  ${tmpdir}/$$.gindex >! ${tmpdir}/$$.index

## Create guide letters linked to positions within the index
## NOTE: guide letter links are tightly coupled to alphabetized 
##       index generation
${awk} -f ${awkpath}/html_guide_index.awk ${tmpdir}/$$.index  >! ${tmpdir}/$$.gindex

## Create the alphabetized index.  Each letter heading is an anchor
##       that can be reached from the guide letters. 
${awk} -f ${awkpath}/html_global_index.awk ${tmpdir}/$$.index >>! \
	 ${tmpdir}/$$.gindex

## Wrap standard html elements around the index file
cat ${tmpdir}/$$.gindex | ${bindir}/html_wrap >! ${webdir}/INDEX${html_suffix}

${rm} -f ${tmpdir}/$$.index
${rm} -f ${tmpdir}/$$.gindex






