/**************************************************************
 *
 *      MakeAutoDocIndex.c - Makes an index from autodocs files.
 *
 *      Designed to work with 1.3 AutoDocs.
 *
 *      Copyright (c) 1989, Peter Cherna
 *
 *      Created:  March 6, 1989
 *      Modified: August 23, 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 6

char *autodocdir[] =
    {
    "AutoDocs:DevicesA-K/*.doc",
    "AutoDocs:DevicesL-Z/*.doc",
    "AutoDocs:LibrariesA-K/*.doc",
    "AutoDocs:LibrariesL-Z/*.doc",
    "AutoDocs:LinkerLibs/*.doc",
    "AutoDOcs:Resources/*.doc",
    };

#define BUFFERSIZE 200
#define FORMFEED 12

struct Library *DosBase;
main()

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

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

            while (docfile = scdir(pat))
                {
                printf("Scanning file %s ...\n",docfile);
                source = fopen(docfile,"r");
                    while (!feof(source))
                    {
                    if (!fgets(buffer,BUFFERSIZE,source))
                        {
                        if (ferror(source))
                            printf("Error %d\n",ferror(source));
                        else
                            printf("Unknown problem.\n");
                        }
                    else if (*buffer == FORMFEED)
                        {
                        offset = ftell(source);
                        fgets(buffer,BUFFERSIZE,source);
                        if (!feof(source))
                            {
                            start = buffer;
                            while (*start == ' ')
                                start++;
                            end = start;
                            while ((*end != ' ') && (*end != '\n') && (*end != '\t'))
                                end++;
                            *end = '\0';
                            funcname = end;
                            while ((*funcname != '/') && (funcname > start))
                                funcname--;
                            if (*funcname == '/')
                                fprintf(dest,"%s; ",funcname+1);
                            else
                                fprintf(dest,"%s; ",funcname);
                            *funcname = '\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 AutoDocs:TempIndex to AutoDocs:SortedIndex",
            NULL,NULL))
            printf("Sort failed.  Make sure the 'sort' command is available.\n");
        else
            {
            source = fopen("AutoDocs:SortedIndex","r");
            if (!source)
                printf("Sort failed - could not find resulting file.\n");
            else
                {
                printf("Sorted.\n");
                DeleteFile("AutoDocs: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 AutoDocs: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,"AutoDocs: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("AutoDocs:SortedIndex");
                }
            }
        }
    if (destname)
        FreeMem(destname,(LONG) BUFFERSIZE);
    if (buffer)
        FreeMem(buffer,(LONG) BUFFERSIZE);
    printf("Done.\n");
    if (DosBase)
        CloseLibrary(DosBase);
    }
