/* 
** Advanced RSS Manager, V2.0, By Alex Derbeev, Belarus.
** Copyright(C)2007-2008, All Rights Reserved.
** Sun Public License applied.
** You can contact me via ICQ, UIN 162344461.
** Please Note, "Opera" browser required so far.
*/
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include "sky_rocket.h"

using namespace std;

#define NUM_PROV 2

char const *db[NUM_PROV][9] =
{
 {
  "All_feeds.txt",
  "links1.txt", 
  "bbc.co.uk",
  "eu_",
  "E-mail this to a friend", "E-mail this to a friend",
  "baked_eu_",
  "full_eu_raw.txt",
  "_uk_london"
 },
 {
  "All_feeds.txt",
  "links2.txt", 
  "yahoo",
  "ya_",
  "Yahoo! News Only", "Email Story",
  "baked_ya_", 
  "full_yahoo_raw.txt",
  "_united_states"
 }
};

class rocket
{
public:
 rocket() {}
 void Run();
protected:
 int generate_links(char const *, char const *, char const *);
 void download(char const *, int, char const *);
 void convert_to_txt(int, char const *);
 void get_messages_bodies(int, char const *, char const *, char const *);
 void merge_considering_size(int, char const *, char const *);
 void gen_suitable_htmls(int, char const *);
 void launch_chm_cooking(int, char const *, char const *);
};

void rocket::Run()
{
 int total[NUM_PROV];
#define loop for (int k=0; k < NUM_PROV; k++)
 loop total[k] = generate_links(FEEDS_FILE(k), FILE_WITH_LINKS(k), PROVIDER_LINK(k));
 loop download(FILE_WITH_LINKS(k), total[k], PROVIDER_PREFIX(k));
 printf("Converting...");
 loop convert_to_txt(total[k], PROVIDER_PREFIX(k));
 printf(" done.\n");
 loop get_messages_bodies(total[k], PROVIDER_PREFIX(k), BODY_BEGIN(k), BODY_END(k));
 loop merge_considering_size(total[k], BAKED(k), FULL_TEXT(k));
 loop gen_suitable_htmls(total[k], PROVIDER_PREFIX(k));
 loop launch_chm_cooking(total[k], PROVIDER_PREFIX(k), POSTFIX(k));
 printf("All tasks completed.\n");
}

int rocket::generate_links(char const *file_with_feeds,
 char const *file_with_links,
 char const *provider
)
{
 ifstream in(file_with_feeds, ios::in);
 ofstream out(file_with_links);
 char buf[4096];
 int total_links = 0;
 while (in.getline(buf, sizeof buf))
 {
  if (strstr(buf, "* http://"))
  {
   if (strstr(buf, provider))
   {
    out << buf+2 << "\n";
    total_links++;
   }
  }
 }
 return total_links;
}

void rocket::download(char const *file_with_links, int total, 
 char const *prefix
)
{
    ifstream in(file_with_links, ios::in);
    string command;
    int current_link = 0;
    char link[512];
    while (in.getline(link, sizeof link))
    {
        current_link++;
        char current_html[512];
        sprintf(current_html, "%d", current_link);
        command = "";
        command += "URL2File.exe ";
        command += link;
        command += " ";
        command += prefix;
        command += current_html;
        command += ".html";
        system(command.c_str());
        printf("\r %d of %d ok, be patient...", current_link, total);
    }
    printf("\n");
    printf("done\n");
}

void rocket::convert_to_txt(int total, char const *prefix)
{
    for (int s=1; s <= total; s++)
    {
        string command = "";
        char current_html[512];
        char current_txt[512];
        sprintf(current_html, "%d", s);
        sprintf(current_txt, "%d", s);
        command += "HTMLtoTXT.exe ";
        command += prefix;
        command += current_html;
        command += ".html ";
        command += prefix;
        command += current_txt;
        command += ".txt";
        system(command.c_str());
    }
}

void rocket::get_messages_bodies(int total, char const *prefix, 
 char const *border_begin_of_the_body,
 char const *border_end_of_the_body
)
{
    for (int s=1; s <= total; s++)
    {
        char current_text_file[512];
        sprintf(current_text_file, "%s%d.txt", prefix, s);
        ifstream in(current_text_file, ios::in);
        sprintf(current_text_file, "baked_%s%d.txt", prefix, s);
        ofstream out(current_text_file);
        char buf[16384];
        while (in.getline(buf, sizeof buf))
        {
            if (strstr(buf, border_begin_of_the_body))
            {
                while (in.getline(buf, sizeof buf))
                {
                    if (strstr(buf, border_end_of_the_body))
                        goto cycle_on_files;
                    out << buf << "\n";
                }
            }
        }
cycle_on_files:
        continue;
    }
}

void rocket::merge_considering_size(int total, char const *prefix, 
 char const *out_name
)
{
    string command = "";
    command += "copy /b ";
    command += prefix;
    command += "*.txt ";
    command += out_name;
    system(command.c_str());
}

void rocket::gen_suitable_htmls(int total, char const *prefix)
{
    for (int s=1; s <= total; s++)
    {
        char current_text_file[512];
        sprintf(current_text_file, "baked_%s%d.txt", prefix, s);
        ifstream in(current_text_file, ios::in);
        sprintf(current_text_file, "suitable_%s%d.html", prefix, s);
        ofstream out(current_text_file);
        char buf[16384];
        out << "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">" << endl;
        out << "<html>" << endl;
        out << "<head>" << endl;
        out << "<title>" << endl;
        out << "</title>" << endl;
        out << "<meta name=\"GENERATOR\" content=\"sky_rocket\">" << endl;
        out << "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Windows-1252\">" << endl;
        out << "</head>" << endl;
        out << "<BODY>" << endl;
        out << "<P><FONT face=Georgia>";
        while (in.getline(buf, sizeof buf))
        {
            out << buf << "<BR>" << endl;
        }
        out << "</FONT></P></BODY>" << endl;
        out << "</html>";
    }
}

void rocket::launch_chm_cooking(int total, char const *prefix, char const *postfix)
{
    char Name_buf[256];
    sprintf(Name_buf, "daily%s.wcp", postfix);
    ofstream new_project(Name_buf);

    char Compiled[256];
    sprintf(Compiled, "daily%s.chm", postfix);

    new_project << "[GENERAL]" << endl;
    new_project << "Title=World News" << endl;
    new_project << "RootDir=" << endl;
    new_project << "DefaultTopic=" << endl;
    new_project << "CompiledFile=" << Compiled << endl;
    new_project << "CustomTemplate=" << endl;
    new_project << "DefaultTemplate=1" << endl;
    new_project << "Encoding=Windows-1251" << endl;
    new_project << "Language=0x0409" << endl;
    new_project << "DeleteProject=0" << endl;
    new_project << "ViewCompiledFile=1" << endl;
    new_project << "HasChild=0" << endl;
    new_project << "NoChild=10" << endl;
    new_project << "HtmlHelpTemplate=" << endl;
    new_project << "HtmlHelpTitle=World News" << endl;
    new_project << "HtmlHelpTitleSame=1" << endl;
    new_project << "WebHelpDefault=" << endl;
    new_project << "WebHelpOutputFolder=c:\\WebHelp" << endl;
    new_project << "WebHelpTemplate=" << endl;
    new_project << "WebHelpTitle=World News" << endl;
    new_project << "WebHelpDefaultSame=1" << endl;
    new_project << "WebHelpTemplateSame=1" << endl;
    new_project << "WebHelpTilteSame=1" << endl;
    new_project << "StartFromRoot=1" << endl;
    new_project << "AutoCollapse=0" << endl;
    new_project << "DrawLines=1" << endl;
    new_project << "SingleHtmlFilename=index.htm" << endl;
    new_project << "SingleHtmlOutputFolder=c:\\SingleHTML" << endl;
    new_project << "SingleHtmlTitle=New HTML Help" << endl;
    new_project << "SingleHtmlHasToc=0" << endl;
    new_project << "SingleHtmlSame=1" << endl;
    new_project << "HeadProperties=1" << endl;
    new_project << "PageProperties=1" << endl;
    new_project << "RealColorIcon=0" << endl;
    new_project << "ShowIndex=1" << endl;
    new_project << "NavWidth=250" << endl;
    new_project << "WebFontColor=#000000" << endl;
    new_project << "WebBackColor=#DCDBD6" << endl;
    new_project << "HHPFolder=" << endl;
    new_project << "[CHMSetting]" << endl;
    new_project << "Top=50" << endl;
    new_project << "Left=50" << endl;
    new_project << "Height=500" << endl;
    new_project << "Width=700" << endl;
    new_project << "PaneWidth=250" << endl;
    new_project << "DefaultTab=0" << endl;
    new_project << "ShowMSDNMenu=0" << endl;
    new_project << "ShowPanesToolbar=1" << endl;
    new_project << "ShowPane=1" << endl;
    new_project << "HideToolbar=0" << endl;
    new_project << "HideToolbarText=0" << endl;
    new_project << "StayOnTop=0" << endl;
    new_project << "Maximize=0" << endl;
    new_project << "Hide=1" << endl;
    new_project << "Locate=1" << endl;
    new_project << "Back=1" << endl;
    new_project << "bForward=1" << endl;
    new_project << "Stop=1" << endl;
    new_project << "Refresh=1" << endl;
    new_project << "Home=1" << endl;
    new_project << "Print=1" << endl;
    new_project << "Option=1" << endl;
    new_project << "Jump1=0" << endl;
    new_project << "Jump2=0" << endl;
    new_project << "AutoShowHide=0" << endl;
    new_project << "AutoSync=1" << endl;
    new_project << "Content=1" << endl;
    new_project << "Index=1" << endl;
    new_project << "Search=1" << endl;
    new_project << "Favorite=1" << endl;
    new_project << "UseFolder=0" << endl;
    new_project << "AutoTrack=0" << endl;
    new_project << "SelectRow=0" << endl;
    new_project << "PlusMinus=1" << endl;
    new_project << "ShowSelection=1" << endl;
    new_project << "ShowRoot=1" << endl;
    new_project << "DrawLines=1" << endl;
    new_project << "AutoExpand=0" << endl;
    new_project << "RightToLeft=0" << endl;
    new_project << "LeftScroll=0" << endl;
    new_project << "Border=0" << endl;
    new_project << "DialogFrame=0" << endl;
    new_project << "RaisedEdge=0" << endl;
    new_project << "SunkenEdge=0" << endl;
    new_project << "SavePosition=0" << endl;
    new_project << "ContentsFont=,8,0" << endl;
    new_project << "IndexFont=,8,0" << endl;
    new_project << "Title=World News" << endl;
    new_project << "Language=0x0409" << endl;
    new_project << "Font=" << endl;
    new_project << "DefaultTopic=" << endl;
    new_project << "[TOPICS]" << endl;
    new_project << "TitleList=" << total << endl;

    for (int s=1; s <= total; s++)
    {
        char current_text_file[512];
        sprintf(current_text_file, "baked_%s%d.txt", prefix, s);
        ifstream in(current_text_file, ios::in);
        char buf[16384];
        while (in.getline(buf, sizeof buf))
        {
            int L = strlen(buf);
            bool empty = true;
            for (int j=0; j < L; j++)
            {
                if (buf[j] != ' ')
                {
                    empty = false;
                    break;
                }
            }
            if (!empty) break;
        }
        new_project << "TitleList.Title." << s-1 << "=" << buf << endl;
        new_project << "TitleList.Level." << s-1 << "=0" << endl;
        char my[512];
        sprintf(my, "suitable_%s%d.html", prefix, s);
        new_project << "TitleList.Url." << s-1 << "=" << my << endl;
        new_project << "TitleList.Icon." << s-1 << "=0" << endl;
        new_project << "TitleList.Status." << s-1 << "=0" << endl;
        new_project << "TitleList.Keywords." << s-1 << "=" << endl;
        new_project << "TitleList.ContextNumber." << s-1 << "=" << endl;
        new_project << "TitleList.ApplyTemp." << s-1 << "=0" << endl;
        new_project << "TitleList.Expanded." << s-1 << "=0" << endl;
        new_project << "TitleList.Kind." << s-1 << "=0" << endl;
    }
    new_project.close();
    char command[512];
    sprintf(command, "WinCHM\\winchm.exe %s /h", Name_buf);
    system(command);
}

int main()
{
    rocket download_engine;
    download_engine.Run();
    return 666;
}
