#! /usr/bin/awk -f
#
# $Id: shtml2html,v 1.8 2005/10/12 18:11:21 dan Exp $
#
# Copyright (c) 2001, 2002, 2005 Dan McMahill
# All rights reserved.
#
# This code is derived from software written by Dan McMahill
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. All advertising materials mentioning features or use of this software
#    must display the following acknowledgement:
#        This product includes software developed by Dan McMahill
#  4. The name of the author may not be used to endorse or promote products
#     derived from this software without specific prior written permission.
# 
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
#  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
#  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
#  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
#  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
#  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
#  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
#  AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
#  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
#  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
#  SUCH DAMAGE.
#
# purpose:  parse html, find lines like:
#  <!--#include virtual="/user_footer.incl"-->
# and do the include.  should help in generating static web pages
#
# Usage:
#   awk -f shtml2html in.shtml > out.html
#


BEGIN {
    start = 1;
    sf_eat = 0;
# make sure this is open for when we close it in END
    printf("") > "/dev/stderr";
} 

# hack becasue command line variable assignments don't show up until
# _after_ BEGIN
start==1 {
    if (include == "") {
	include = "." ;
    }
    else {
	include=(".:" include);
    }
    npaths=split(include,includepath,":");
    start=0;
}

{
  
  line=$0;

  if (line ~ /<\!--\#include.*-->/){
# print the part of the line before the #include
    pre = gensub(/(.*)<\!--\#include.*-->/,"\\1",1,line);
    pre = sf(pre);
    printf("%s", pre);

# pick out just the <!--#include ...... --> part
    cmd = gensub(/(.*)(<\!--\#include.*-->)(.*)/,"\\2",1,line);
    #printf("processing directive \"%s\"\n",cmd) > "/dev/stderr";

#figure out the included file name
    file = gensub(/(.*)virtual=\"([^\"]*)\"(.*)/,"\\2",1,cmd);

    if ( file != cmd) {
# make sure we can read the file
	fileok=0;
	for (i=1; i<= npaths; i=i+1) {
	    if (includepath[i] == "") {
# this is the case where include=foo::bar (ie a blank entry)
		filepath =  file;
	    }
	    else {
		filepath = includepath[i] "/" file;
	    }
	    if ( (getline < filepath) < 0 ) {
#		printf("unable to process include of \"%s\": %s\n", \
#		       filepath,ERRNO) > "/dev/stderr";
		close(filepath);
	    }
	    else {
		close(filepath);
		fileok=1;
		break;
	    }
	}
	if (fileok) {
#printf("[%s]",file) > "/dev/stderr";
	    while ( getline < filepath ){ print sf($0); }
	    close(filepath);
	}
	else {
	    printf("unable to process include of \"%s\"\n", \
		   file) > "/dev/stderr";
	    exit 1;
	}
    }
    else {
      printf("ERROR:  could not find the file name in the directive\n") \
	> "/dev/stderr";
      printf("        \"%s\"\n\n",cmd)  > "/dev/stderr";
      exit 1;
    }

    #printf("\n") > "/dev/stderr";

    post = gensub(/(.*)(<\!--\#include.*-->)(.*)/,"\\3",1,line);
    post = sf(post);
    printf("%s\n",post);

  }
  else{
    print sf($0);
  }
}


END {
  close("/dev/stderr");
}

function sf(line) {

# process the <SOURCEFORGE> tags
  if( sourceforge == 1 ) {
    gsub(/<SOURCEFORGE>/, "", line);
    gsub(/<\/SOURCEFORGE>/, "", line);
  } else {
    if(line ~ /<SOURCEFORGE>/ ) { sf_eat = 1; }
    if(line ~ /<\/SOURCEFORGE>/ ) { sf_eat = 2; }
    gsub(/<SOURCEFORGE>/, "<!--", line);
    gsub(/<\/SOURCEFORGE>/, "-->", line);
    if( sf_eat == 1) { return( "" ); }
    else if( sf_eat == 2) { sf_eat = 0 ; return( "" ); }
  }

  return(line);
}

