#!/bin/sh

EXECDIR=$0
EXECDIR=$(echo $EXECDIR | sed 's|/[^/]*$||g' )
if [ $EXECDIR == $0 ]; then 
   EXECDIR="."
fi

if [ $# -le 0 ]; then
cat 1>&2 <<EOF

  usage: $0 <input file> [ <output file> ]

  Script for generating htdig defaults.xml from 
   defaults.cc

  <input file> is the name of source file - this should
     be a htdig defaults.cc file 

  <output file> is the output file. If not specified, 
     it defaults to "defaults.xml"

EOF
exit 1
fi

WORKDIR=$EXECDIR
INTERIMCC="$WORKDIR/makexml.cc"
COMPILEDCC="$WORKDIR/makexml"
INTERIMXML="$WORKDIR/defaults.xml.tmp"
CC=gcc

INPUT=$1

if [ $# -gt 1 ]; then
   OUTPUT=$2
else
   OUTPUT="$WORKDIR/defaults.xml"
fi


##########################################################
# Step 1 - create a small C++ program called "makexml.cc"
# that creates the initial version of the xml file

echo Creating $INTERIMCC from $INPUT ... 1>&2 

{

cat << EOF
#include <stdio.h>
#include <string.h>

//#define BIN_DIR "___ BIN_DIR"
//#define COMMON_DIR "___ COMMON_DIR"
//#define CONFIG_DIR "___ CONFIG_DIR"
//#define DATABASE_DIR "___ DATABASE_DIR"
//#define IMAGE_URL_PREFIX "___ IMAGE_URL_PREFIX"
//#define VERSION "___ VERSION"
//#define DEFAULT_CONFIG_FILE "___ DEFAULT_CONFIG_FILE"

struct ConfigDefaults
{
  char  *name;          // Name of the attribute
  char  *value;         // Default value
  char  *type;          // Type of the value (string, integer, boolean)
  char  *programs;      // Whitespace separated list of programs/modules using this attribute
  char  *block;         // Configuration block this can be used in (can be blank)
  char  *version;       // Version that introduced the attribute
  char  *category;      // Attribute category (to split documentation)
  char  *example;       // Example usage of the attribute (HTML)
  char  *description;       // Long description of the attribute (HTML)
};

EOF

for x in `grep "^[\ \t]*{" $INPUT | grep -v "{0" | grep -v "^{$" | cut -f2 -d, | grep -v '"'`
do
   echo "#define $x \"___ $x\""
done

echo

grep  -v '^#' $INPUT   | grep -v "^HtConfig" | sed 's/\\$//'

#<!ATTLIST attribute name     CDATA #REQUIRED
#                    type     string|integer|boolean) "string"
#                    programs CDATA #REQUIRED
#                    block    CDATA #IMPLIED 
#                    version  CDATA #REQUIRED
#                    category CDATA #REQUIRED

cat << EOF

int main()
{
    printf( "<!DOCTYPE HtdigAttributes SYSTEM \"defaults.dtd\" >\n"
            "<HtdigAttributes>\n" );
    for (int i = 0; defaults[i].name; i++)
    {

        char type[50];
        strcpy( type, defaults[i].type );
        char* pchType = type; 
        while( *pchType )
        {
           if ( *pchType == ' ' ) *pchType = '_';
           *pchType++;
        }
        
        printf( "   <attribute name=\"%s\" \n"
                "              type=\"%s\" \n"
                "              programs=\"%s\" \n"
                "              version=\"%s\" \n"
                "              category=\"%s\" ",
                defaults[i].name,
                type,
                defaults[i].programs,
                defaults[i].version,
                defaults[i].category );

        char* block = defaults[i].block;
        if ( strlen( block ) > 0 )
            printf("\n              block=\"%s\" ", block );

        char* deft = defaults[i].value;
        char* attr = "";
        if ( strncmp( deft, "___ ", 4) == 0 )
        {
            attr = " configmacro=\"true\"";
            deft += 4;
        }

        printf ( ">\n"
                 "     <default%s>%s</default>\n", attr, deft );

        char* example = defaults[i].example;
        char* description = defaults[i].description;
        
        if ( strncmp( example, "**", 2 ) == 0 )
            printf( "     <nodocs/>\n" );
        else
        {
            printf( "     <example>%s</example>\n"
                    "     <description>%s     </description>\n"
                    , example
                    , description );
        }
        
        printf( "   </attribute>\n\n" );
    }
    printf( "</HtdigAttributes>\n" );
    return 0;
}

EOF

} > $INTERIMCC

##########################################################
# Step 2 - create a small C++ program called "makexml.cc"
# that creates the initial version of the xml file

echo Compiling $INTERIMCC to create $COMPILEDCC ... 1>&2 

$CC $INTERIMCC -o $COMPILECC makexml
if [ $? -ne 0 ]; then exit 1; fi

##########################################################
# Step 3 - create interim XML file from compile CC file

echo Running $COMPILEDCC to create $INTERIMXML ... 1>&2 

$COMPILEDCC > $INTERIMXML
if [ $? -ne 0 ]; then exit 1; fi

##########################################################
# Step 4 - create final XML file from interim

echo Using $INTERIMXML to create $OUTPUT ... 1>&2 

$EXECDIR/defaultsmunge.pl $EXECDIR < $INTERIMXML > $OUTPUT
