#!/bin/csh -f

set TOPFILE = include_top.html
set BOTFILE = include_bot.html

if ($#argv >= 1) then
  set FILES = $1
else
  set FILES = *.html
endif

echo Will process $FILES

foreach file ( $FILES )
  if ( ($file != "$TOPFILE") && ($file != "$BOTFILE") ) then
    echo Processing $file
    
    grep -v "<\!--I-->" $file >! tmp1
    
    set body = `grep -i "<body" tmp1 | wc -l`
    
    if ($body == 0) then
      grep -i "<TITLE>" tmp1 >! tmp
      echo "<BODY>" >> tmp
      awk '{ printf "<\!--I-->%s\n", $0 }' $TOPFILE >> tmp
      grep -v -i "<TITLE>" tmp1 >> tmp
      awk '{ printf "<\!--I-->%s\n", $0 }' $BOTFILE >> tmp
      echo "</BODY>" >> tmp
    else
      awk '{ if (($1=="<body>")||($1=="<BODY>")||\
	         ($1=="<body" )||($1=="<BODY" )) {\
	        printf "<BODY text=\"#243b4a\" BGCOLOR=\"#FFFFFF\">\n";\
		exit;\
	     } else\
   	       printf "%s\n", $0; }' tmp1 >! tmp 

      awk '{ printf "<\!--I-->%s\n", $0 }' $TOPFILE >> tmp

      awk '{ if (($1=="</body>")||($1=="</BODY>")) exit; \
      	     if (doit==1) printf "%s\n", $0; \
	     if (($1=="<body>")||($1=="<BODY>")||\
	     	 ($1=="<body" )||($1=="<BODY" )) doit=1; }' tmp1 >> tmp

      awk '{ printf "<\!--I-->%s\n", $0 }' $BOTFILE >> tmp

      awk '{ if (($1=="</body>")||($1=="</BODY>")) doit=1; \
      	     if (doit==1) printf "%s\n", $0; }' tmp1 >> tmp
    endif
    \rm tmp1
    if ( -f \#$file\# ) \rm \#$file\#
    mv $file \#$file\#
    mv tmp $file
  endif
end

#last line


