#include <iostream.h>

#include <stdlib.h>

#include <stdio.h>

#include <string.h>

#include <Winsock2.h>


#ifndef GLOBAL_DECL

#include "global.h"     // Global data types and variables

#endif


#include "wsock.h"      // Interface to WSock

#include "striptags.h"  // Interface to StripTags



// Implementation

bool compile(pglist* pagelist)
{
    //FILE*             fp;                     // file pointer

    //char              line[200];              // line read from file

    //char              value[100];             // value extracted from line

    //char              version_number[200];    // version number of page list file

    //int               characters;             // used for splitting line

    data_item*          data;                   // pointer to structure containing data

    int                 loop, loop2;            // loop variable

    int                 counter;                // counter for duplicate filenames

    //char              address[200];           // Page to retrieve

    char                fname[200];             // Filename

    char                last_fname[200];        // Last filename used

    char                fname2[200];            // Filename to save original html to

    char                fname3[200];            // Filename to save converted html to

    char                title[100];             // title to put at top of converted page

    char                dud[10];                // only used for temporary storage in itoa function

    wsock               connection;             // Create instance of wsock object

    striptag            convert;                // Create instance of striptag object


    // Download the page file from the server

    if (! Download_Page_File() )
        return false;

    // Get the categories from the page file and put them into a linked list

    ParseCategories(pagelist);

    last_fname[0] = NULL;
    counter = 1;

    cout << "\nCommencing download..";

    pagelist -> move_first();           // move to start of list


    for ( loop=0; loop<pagelist -> items; loop++ )
    {

        data = pagelist -> get_item();      // get a page from the list


        cout << "\nGenerating filename";

        // Create filename by concatenating categories

        strcpy(fname, data -> category[0]);
        for ( loop2=1; loop2<5; loop2++ )
            strcat(fname, data -> category[loop2]);

        // Check if filename is the same as previous filename

        if ( !strcmp( last_fname, fname ) )
            counter++;          // Increment counter

        else
            counter = 1;        // Reset counter to 1


        // Set last_fname to allow checking next time

        strcpy(last_fname, fname);

        // Create final filenames

        strcpy(fname2, "Source Pages/");
        strcat(fname2, fname );
        strcat(fname2, itoa( counter, dud, 10 ) );
        strcat(fname2, ".htm");
    
        strcpy(fname3, "Converted Pages/");
        strcat(fname3, fname );
        strcat(fname3, itoa( counter, dud, 10 ) );
        strcat(fname3, ".htm");

        cout << "\nGenerating title..";

        // Generate title based on categories

        strcpy(title, "Web News Speak - Category");
        for ( loop2=0; loop2<5; loop2++ )
        {
            if ( strlen( data -> category[loop2] ) > 0 )
            {
                strcat(title, " : " );
                strcat(title, data -> category[loop2] );
            }
        }

        cout << "\nGetting '" << data -> url << "'";

        if ( !connection.get_page( data -> url, fname2 ) )
        {
            cout << "\n" << connection.error << "\n";
            exit(0);
        }

        cout << "\nConverting '" << data -> url << "'";

        if ( convert.strip_tags( fname2, fname3, data -> url, title, "+0" ) == false )
        {
            cout << "\nError converting.";
            exit(0);
        }

        cout << "\nDone.";

    }
    return true;
}

syntax highlighted by Code2HTML, v. 0.8.11