/* This converts the entire OS into HTML.
See ::/Demo/ToHtmlToTXTDemo/ToHtml.ZC.

Notice that an entry like $TX,"GOOGLE",HTML="http://www.google.com"$
will be converted to text in the html with an html link.

Terry cheated by hardcoding www.templeos.org as the website
for ZealOS Links.  Why don't you copy
::/Demo/ToHtmlToTXTDemo/ToHtml.ZC to your /Home directory
and modify it?  You are welcome to link to
http://www.templeos.org if you want file that come on the
ZealOS distribution.

You can pass html meta data as args to ToHtml().
*/

Cd(__DIR__);;
#include "::/Demo/ToHtmlToTXTDemo/ToHtml"

//ToHtml("DemoInPage.DD", "~/DemoOutPage");

CTask *index_task = User("#include \"::/Demo/ToHtmlToTXTDemo/HtmlDirList\";;DocMax;DocMax;\n");
DocClear(index_task->border_doc, TRUE);
WinVert(1, TEXT_ROWS - 2, index_task);
WinToTop;

U0 DirIndex(U8 *dest_path, U8 *full_name, U8 *opt_func="")
{
    U8      *index_path,
            *index_path_out,
            *index_exe;
    CDoc    *index_doc;

    index_path      = MStrPrint("%s/index.DD", dest_path);
    index_path_out  = MStrPrint("%s/index.html", dest_path);
    "%s\n", index_path;

    index_exe = MStrPrint("Cls;DirIndexList(\"%s\");%s;View;\n", full_name, opt_func);
    XTalk(index_task, index_exe);

    TaskWait(index_task);

    index_doc = DocNew(index_path);
    DocInsDoc(index_doc, index_task->put_doc);
    DocWrite(index_doc);
    DocDel(index_doc);

    TaskWait(index_task);
    MessagePostWait(index_task, MESSAGE_KEY_DOWN_UP, CH_SHIFT_ESC, 0);

    ToHtml(index_path, index_path_out);
    Del(index_path);

    Free(index_path);
    Free(index_path_out);
    Free(index_exe);

}

I64 Dir2Html(U8 *src_files_find_mask, U8 *dst_files_find_mask)
{
    CDirEntry   *tmpde1 = NULL, *tmpde2;
    I64          res = 0;
    U8          *dest_path,
                *dest_file,
                *dest_abs,
                *src_abs,
                 dest_ext[STR_LEN];

    tmpde1 = FilesFind(src_files_find_mask, FUF_RECURSE);
    dest_abs    = DirNameAbs(dst_files_find_mask);
    src_abs     = DirNameAbs(src_files_find_mask);

    if (tmpde1)
    {
        while (tmpde1)
        {
            tmpde2 = tmpde1->next;
            res++;

            dest_file = StrNew(tmpde1->name);
            FileExtRemove(dest_file, dest_ext);

            "%s\n", tmpde1->full_name;

            if (!(tmpde1->attr & RS_ATTR_DIR))
            {
                if (StrCompare(dest_ext, "ZXE")     &&
                    StrCompare(dest_ext, "BIN")     &&
                    StrCompare(dest_ext, "ISO")     &&
                    StrCompare(dest_ext, "ISO.C")   &&
                    StrCompare(dest_ext, "html")    &&
                    StrCompare(dest_ext, "DATA")    &&
                    StrCompare(dest_ext, "MAP"))
                {
                    dest_path = MStrPrint("%s/%s.%s.html", dest_abs, dest_file, dest_ext);

                    "%s\n\n", dest_path;
                    ToHtml(tmpde1->full_name, dest_path);
                    progress1++;

                    Free(dest_path);
                }
            }
            else
            {
                dest_path = MStrPrint("%s/%s", dest_abs, dest_file);

                DirIndex(dest_path, tmpde1->full_name);

                if (StrCompare(tmpde1->full_name, dest_abs))
                    Dir2Html(tmpde1->full_name, dest_path);

                Free(dest_path);


            }

            DirEntryDel(tmpde1);
            tmpde1 = tmpde2;

            Free(dest_file);
        }
    }

    Free(dest_abs);

    return res;

}

I64 HtmlGen()
{
    I64 res;

    DocMax;
    DocMax;
    DelTree("::/HTML/");

    progress1       = 0;
    progress1_max   = FileCount(FilesFind("::/", FUF_RECURSE));

    DirIndex("::/HTML/", "::/", "HashTableDump;");

    res = Dir2Html("::/", "::/HTML/");

    Kill(index_task);

    return res;
}