/**************************************************************
 *
 *      MakeHeaderIndex.c - Makes an index from header (.h) files.
 *
 *      Copyright (c) 1989, Peter Cherna
 *
 *      Created:  March 21, 1989
 *      Modified: August 28, 1989       Release 1.2:  August 29, 1989
 *
 *      Auto: cc -q -o RAM:<file>.o <path><file>
 *      Auto: ln RAM:<file>.o -lc -o <path><file>
 *
 **************************************************************/

#include <functions.h>
#include <stdio.h>
#include <ctype.h>

#ifndef EXEC_MEMORY_H
#include <exec/memory.h>
#endif

extern char *scdir();

#define DIRCOUNT 8      /*  Number of directories that have include files */

char *includedir[] =
    {
    "ch:devices/*",
    "ch:exec/*",
    "ch:graphics/*",
    "ch:hardware/*",
    "ch:intuition/*",
    "ch:libraries/*",
    "ch:resources/*",
    "ch:workbench/*",
    };

#define BUFFERSIZE 200
#define FORMFEED 12

struct Library *DosBase;
main(argc,argv)

    int argc;
    char *argv[];

    {
    FILE *source, *dest;
    char *buffer, *start, *end, *pat, *docfile, *destname;
    char subindex, ch;
    LONG offset;
    int p;

    DosBase = OpenLibrary("dos.library",0L);
    dest = fopen("ch:TempIndex","w");
    buffer = AllocMem((LONG) BUFFERSIZE,MEMF_CLEAR);
    destname = AllocMem((LONG) BUFFERSIZE,MEMF_CLEAR);
    if (!dest)
        {
        printf("Could not open ch:TempIndex for writing.\n");
        }
    else if (!buffer || !destname)
        {
        printf("Could not allocate buffer.\n");
        }
    else
        {
        for (p = 0; p < DIRCOUNT; p++)
            {
            pat = includedir[p];

            while (docfile = scdir(pat))
                {
                printf("Scanning file %s ...\n",docfile);
                source = fopen(docfile,"r");
                offset = 0;
                while (!feof(source))
                    {
                    offset++;
                    if (!fgets(buffer,BUFFERSIZE,source))
                        {
                        if (ferror(source))
                            printf("Error %d\n",ferror(source));
                        }
                    else if (!strncmp(buffer,"#define",7))
                        {
                        start = buffer+7;
                        /* skip any blanks after the '#define': */
                        while ((*start == ' ') || (*start == '\t'))
                            start++;
                        end = start;
                        /*  skip until the first blank after the label: */
                        while ((*end != ' ') && (*end != '\n') && (*end != '\t'))
                            end++;
                        *end = '\0';
                        fprintf(dest,"%s; %s; %ld\n",start,docfile,offset);
                        }
                    else if (!strncmp(buffer,"struct",6))
                        {
                        start = buffer+7;
                        /* skip any blanks after the 'struct ': */
                        while ((*start == ' ') || (*start == '\t'))
                            start++;
                        end = start;
                        while ((*end != ' ') && (*end != '\n') && (*end != '\t'))
                            end++;
                        *end = '\0';
                        fprintf(dest,"%s; %s; %ld\n",start,docfile,offset);
                        }
                    }
                if (source)
                    fclose(source);
                }
            }
        fclose(dest);

        subindex = ' ';
        dest = NULL;

        printf("Sorting index...\n");
        if (!Execute("sort >nil: from ch:TempIndex to ch:SortedIndex",
            NULL,NULL))
            printf("Sort failed.  Make sure the 'sort' command is available.\n");
        else
            {
            source = fopen("ch:SortedIndex","r");
            if (!source)
                printf("Sort failed - could not find resulting file.\n");
            else
                {
                printf("Sorted.\n");
                DeleteFile("ch:TempIndex");

                /*  Make an Index directory.  Ignore return code (probably
                    directory already exists)  Error will be caught when
                    we try to write the first index file. */
                Execute("makedir ch:Index", NULL, NULL);
                while (!feof(source))
                    {
                    if (!fgets(buffer,BUFFERSIZE,source))
                        {
                        if (ferror(source))
                            printf("Error %d\n",ferror(source));
                        else if (!feof(source))
                            printf("Unknown problem.\n");
                        }
                    else
                        {
                        ch = toupper(*buffer);
                        if (ch != subindex)
                            {
                            if (dest)
                                fclose(dest);
                            sprintf(destname,"ch:Index/Index%c",ch);
                            dest = fopen(destname,"w");
                            subindex = ch;
                            printf("Building subindex %c ...\n",ch);
                            }
                        fprintf(dest,"%s",buffer);
                        }
                    }
                fclose(dest);
                fclose(source);
                DeleteFile("ch:SortedIndex");
                }
            }
        }
    if (destname)
        FreeMem(destname,(LONG) BUFFERSIZE);
    if (buffer)
        FreeMem(buffer,(LONG) BUFFERSIZE);
    printf("Done.\n");
    if (DosBase)
        CloseLibrary(DosBase);
    }
