#include "gui.h"

#define LINES       1024        /* max length of html file */
#define MAXLEN      256         /* max length of a string */
#define MAXFILES    256         /* max number of files */

#define RESULTHTML  "_results.html"

/**********************/
/* global definitions */
/**********************/
int debug = 0;              /* global debug flag */

char *filesptr[MAXFILES];   /* list of filenames */

struct Remember *RememberFilesPtr = NULL,
                *RememberLinesPtr = NULL;   /* memory pointers */

/************************/
/* function prototypes  */
/************************/

/* report.c */
void start_report(), add_url(), finish_report();

/* dirlist.c */
int dirlist();

/* get_title.c */
int findstring(), findtitle(), readlines();

/************************/
/* main function        */
/************************/

int main(int argc,char *argv[])
{

    int gui_command = GO_RUNNING;   /* gui control flag */
    ULONG sigs = 0;                 /* input signals */
    char *search_text = "";         /* search string from MUI App */
    int flag_whole,                 /* whole word flag */
        flag_case,                  /* match case flag */
        flag_code;                  /* match HTML code */

    struct ObjApp *App;             /* MUI Application */

    FILE *html_fp, *report_fp, *fopen();

    char *lineptr[LINES],   /* storage for html file */
         target[MAXLEN];    /* target string */

    int i,              /* loop counter */
        nlines;         /* number of lines in a file */
    int nfiles = 0;     /* number of files found */
    int nfound = 0;     /* number of matching files found */

    /* check number of arguments */
    switch (argc)
        {
            case 1: /* string supplied by gui */

                /* Open the MUI interface */
                App = creategui();

                while (!gui_command)
                    {
                        /* Check for user inputs */
                        switch (DoMethod(App->app,MUIM_Application_NewInput,&sigs))
                            {
                                case MUIV_Application_ReturnID_Quit:
                                    gui_command = GO_QUIT;
                                    break;

                                case GO_SEARCH:
                                    get(App->string1,MUIA_String_Contents,&search_text);
                                    gui_command = GO_SEARCH;
                                    break;

                            }
                        if (!gui_command && sigs) Wait(sigs | SIGBREAKF_CTRL_C);
                        if (sigs & SIGBREAKF_CTRL_C) break;

                    }

                /* check what happened */
                if (gui_command == GO_SEARCH)
                    {
                        strcpy(target,search_text);
                        /*printf("search string = %s\n",target);*/

                        /* set flags from checkmark buttons */
                        get(App->checkmark1, MUIA_Selected, &flag_whole);
                        get(App->checkmark2, MUIA_Selected, &flag_case);
                        get(App->checkmark3, MUIA_Selected, &flag_code);
                    }

                /* Dispose of the interface */
                DisposeApp(App);
                break;

            case 2: /* string already supplied */
                strcpy(target, argv[1]);
                break;

            default:
                printf("Usage: get_title [STRING/S]\n");
                exit(1);
        }

    /* quit if required */
    if (gui_command == GO_QUIT) exit(1);

    /* read in current directory list */
    nfiles = dirlist();

    /* report how many files found */
    if ((report_fp = fopen(RESULTHTML,"w")) == NULL) {
        printf("Can't open report file for writing!\n");
        exit(1);
    } else start_report(report_fp, target, flag_case, flag_whole, flag_code, nfiles);

    /* open each file in dirlist() */
    for (i = 0; i < nfiles; i++) {

        /* attempt to open file */
        if ((html_fp = fopen(filesptr[i], "r")) == NULL) {
            printf("get_title: can't open %s\n", filesptr[i]);
            exit(1);
        } 

        /* read file into lineptr */
        if ((nlines = readlines(lineptr, LINES, html_fp)) <0) {
             printf("file too long to process!\n");
             exit(1);
        }
        
        /* close off the html file */
        fclose(html_fp);

        /* search for target */
        if (findstring(lineptr, nlines, target, flag_whole, flag_case, flag_code)) {

             nfound++;

             /* find title of matching file */
             if (!findtitle(filesptr[i], lineptr, nlines, report_fp))
                 printf("Error: No <TITLE> in document!\n");
        }

        /* release memory */
        if (RememberLinesPtr) {

            FreeRemember(&RememberLinesPtr, TRUE);
            if (debug) if (!RememberLinesPtr) printf("RememberLinesPtr memory released..\n");
                            else printf("RememberLinesPtr is still set to 0x%x\n",&RememberLinesPtr);
        } else printf("Error! RememberLinesPtr was NULL\n");
    }

    /* polish up report file */
    finish_report(report_fp, nfound);
    fclose(report_fp);

    /* cleanup memory */
    if (RememberFilesPtr) {
        FreeRemember(&RememberFilesPtr, TRUE);
        if (debug) if (!RememberFilesPtr) printf("RememberFilesPtr memory released..\n");
                            else printf("RememberFilesPtr is still set to 0x%x\n",&RememberFilesPtr);
    } else printf("Error! RememberFilesPtr was NULL\n");

}

