TString GetInfo(const char *fname, int &w, int &h)
{
    TString dt;

    ifstream fin(fname);
    while (1)
    {
        TString str;
        str.ReadLine(fin);
        if (!fin)
            break;

        if (str.BeginsWith("RelatedImageWidth"))
            w = atoi(str(str.First('|')+1, str.Length()).Data());
        if (str.BeginsWith("RelatedImageLength"))
            h = atoi(str(str.First('|')+1, str.Length()).Data());
        if (str.BeginsWith("DateTime"))
            dt = str(str.First('|')+1, 19);
    }

    return Form("%s.%s.%s&nbsp;&nbsp;%s",
                TString(dt(8,2)).Data(), TString(dt(5,2)).Data(),
                TString(dt(0,4)).Data(), TString(dt(11, 5)).Data());
}

void makehtml(const char *dir=".", int cols=4, int rows=3)
{
    TRegexp expr(".[JPG,AVI]$", kFALSE);

    TList list;

    void *ptr = gSystem->OpenDirectory(dir);
    char *name = 0;
    while (name=gSystem->GetDirEntry(ptr))
    {
        TString str(name);
        str = str(expr);

        if (!str.IsNull())
            list.Add(new TObjString(name));
    }
    gSystem->FreeDirectory(ptr);

    // ----------------------------------------

    TIter Next(&list);
    TObject *obj = 0;

    cout << "<center><table CELLPADDING=5>" << endl;

    int tot = 0;
    int i=0;
    while (obj=Next())
    {
        TString n = obj->GetName();

        Long_t id, size, flags, modtime;
        gSystem->GetPathInfo(Form("%s/%s", dir, n.Data()), &id, &size, &flags, &modtime);
        tot += size;

        TString t = n;
        t.ReplaceAll(".JPG", ".jpg");
        t.ReplaceAll(".AVI", ".jpg");

        TString txt = t;
        txt.ReplaceAll(".jpg", ".txt");

        int w, h;
        TString date = GetInfo(Form("%s/%s", dir, txt.Data()), w, h);

        if (i++%cols==0)
            cout << "  <tr>" << endl;

        cout << "    <td ALIGN=CENTER VALIGN=MIDDLE>" << endl;
        if (w>0)
            cout << "      <FONT SIZE=-1><B>" << date << "</B></FONT><BR>" << endl;
        cout << "      <A HREF=\"" << n << "\">" << endl;
        cout << "        <IMG SRC=\"thumb_" << t << "\" ALT=\"thumb_" << t << "\" BORDER=0></A>" << endl;
        cout << "      <BR>" << endl;
        cout << "      <FONT SIZE=-1>" << endl;
        if (w>0)
            cout << "        (" << w << "x" << h << ",&nbsp;" << TMath::Nint(size/1024.) << "kB)<BR>" << endl;
        cout << "        ";
        if (w>0)
            cout << "<A HREF=\"" << txt << "\">";
        cout << n;
        if (w>0)
            cout << "</A>";
        if (w==0)
            cout << ", " << TMath::Nint(size/1024.) << "kB";
        cout << endl;
        cout << "      </FONT>" << endl;
        cout << "    </td>" << endl;

        if (i%cols>0)
            continue;

        cout << "  </tr>" << endl;

        if ((i/cols)%rows>0)
            continue;

        cout << "</table>" << endl;
        /*
        cout << "<B>";
        if (i>rows*cols)
            cout << i-rows*cols*2+1 << " (-" << rows*cols << ") &lt;--- ";
            cout << i-rows*cols+1 << " ---&gt; " << i+1 << " (+" << rows*cols << ")</B>" << endl;
            */
        cout << endl;
        cout << endl;
        cout << "<table CELLPADDING=5>" << endl;
    }

    if (i%cols>0)
        cout << "  </tr>" << endl;
    /*
    i /= rows*cols;
    i++;
    i *= rows*cols;
    */
    cout << "</table>" << endl;
    /*
    cout << "<B>";
    if (i>rows*cols)
        cout << i-rows*cols*2+1 << " &lt;--- ";
    cout << i-rows*cols+1 << "</B>" << endl;
    cout << "</center>" << endl;
    */
    cerr << "Stats:  N=" << i << " " << ((i-1)/cols/rows)+1 << "x(" << cols << "x" << rows << ")  S=" << TMath::Nint(tot/(1024.*1024)) << "MB" << endl;
}

