#!/usr/local/bin/gawk -f

BEGIN {
    if (ARGC == 1) {
        print "Usage: extract master-files[...] output-file"
        exit 1
    }
    OutputFILE = ARGV[ARGC - 1]
    printHeader()
    for (i = 1; i < ARGC - 1; i++)
        extract(ARGV[i])
    printPrototypes();
    exit 0
}

function printHeader()
{
    print "/******************************************************************************" >OutputFILE
    print " *      (C)Copyright        AdIn Research, Inc.         1991-1993.            *" >OutputFILE
    print " ******************************************************************************" >OutputFILE
    print " *" >OutputFILE
    print " *  File name  : stdext.h" >OutputFILE
    print " *" >OutputFILE
    print " *  Note       : Beta-RNA Standard Function I/F" >OutputFILE
    print " *" >OutputFILE
    print " *****************************************************************************/" >OutputFILE
    print "#if !defined(_STDEXT_H_)" >OutputFILE
    print "#define _STDEXT_H_" >OutputFILE
    print "" >OutputFILE
    print "#if !defined(_RNA00USR_H_)" >OutputFILE
    print "" >OutputFILE
    print "#include <stdio.h>" >OutputFILE
    print "" >OutputFILE
    print "#if defined(__cplusplus)" >OutputFILE
    print "extern \"C\" {" >OutputFILE
    print "#endif /* __cplusplus */" >OutputFILE
    print "" >OutputFILE
    print "#if !defined(ULONG_MAX)" >OutputFILE
    print "# define ULONG_MAX        ((unsigned long) -1)" >OutputFILE
    print "#endif /* not ULONG_MAX */" >OutputFILE
    print "#if !defined(DBL_MIN)" >OutputFILE
    print "# define DBL_MIN          (2.2250738585072015e-308) /* Min IEEE double exactly */" >OutputFILE
    print "#endif /* not DBL_MIN */" >OutputFILE
    print "" >OutputFILE
}

function printPrototypes()
{
    print "" >OutputFILE
    print "#endif /* not _RNA00USR_H_ */" >OutputFILE
    print "" >OutputFILE
    print "/********** function prototypes **********/" >OutputFILE
    print "EXPORT Ferr PUBLIC PROTOTYPE(RS_Init, (int));" >OutputFILE
    print "EXPORT Ferr PUBLIC PROTOTYPE(RS_Halt, (void));" >OutputFILE
    print "EXPORT Ferr PUBLIC PROTOTYPE(RS_LoadData, (char *));" >OutputFILE
    print "EXPORT Ferr PUBLIC PROTOTYPE(RS_SaveData, (char *));" >OutputFILE
    print "EXPORT Ferr PUBLIC PROTOTYPE(RS_GetPGNum, (RNUM *));" >OutputFILE
    print "EXPORT Ferr PUBLIC PROTOTYPE(RS_GetPGList, (RID *, RNUM));" >OutputFILE
    print "EXPORT Ferr PUBLIC PROTOTYPE(RS_SetPGLabel, (RID, char *));" >OutputFILE
    print "EXPORT Ferr PUBLIC PROTOTYPE(RS_GetPGLabel, (RID, char *));" >OutputFILE
    print "EXPORT Ferr PUBLIC PROTOTYPE(RS_SetPTLabel, (RID, char *));" >OutputFILE
    print "EXPORT Ferr PUBLIC PROTOTYPE(RS_GetPTLabel, (RID, char *));" >OutputFILE
    print "EXPORT Ferr PUBLIC PROTOTYPE(RS_SetSSLabel, (RID, char *));" >OutputFILE
    print "EXPORT Ferr PUBLIC PROTOTYPE(RS_GetSSLabel, (RID, char *));" >OutputFILE
    print "EXPORT Ferr PUBLIC PROTOTYPE(RS_SetASLabel, (RID, RID, char *));" >OutputFILE
    print "EXPORT Ferr PUBLIC PROTOTYPE(RS_GetASLabel, (RID, RID, char *));" >OutputFILE
    print "" >OutputFILE
    print "#if defined(__cplusplus)" >OutputFILE
    print "extern \"C\" {" >OutputFILE
    print "#endif /* __cplusplus */" >OutputFILE
    print "" >OutputFILE
    print "#endif /* not _STDEXT_H_ */" >OutputFILE
}

function extract(file)
{
    nl = 0
    while ((ret = getline line <file) > 0) {
        if (match(line, /\/\*\*STDEXT_BEGIN\*\*\//)) {
            if (inBlock != 0) {
                printf "nested block (%s, line=%d)\n", file, lc
                exit 1
            }
            inBlock = 1;
        } else if (match(line, /\/\*\*STDEXT_END\*\*\//)) {
            if (inBlock != 1) {
                printf "too-many ends (%s, line=%d)\n", file, lc
                exit 1
            }
            inBlock = 0;
        } else if (inBlock) {
            print line >OutputFILE
            nl++
        }
    }
    if (ret == -1)
        printf "cannot open %s\n", file
    else if (inBlock)
        printf "%s : warning : extraction block left open\n", file
    else if (nl > 0)
        printf "Extracted %d lines from %s\n", nl, file
}
