void Wrap(TString &str)
{
    while (str.Length()>80)
    {
        TString ss = str(0, 80);
        Int_t l = ss.Last(' ');
        if (l<0)
            break;
        cout << str(0, l) << endl;
        str.Remove(0, l);
    }
    cout << str << endl;
}

void ProcessFile(TString fname, TString date)
{
    cout << fname << endl;

    ifstream fin(fname);

    TString line;

    int i=81;

    TRegexp r0("20[0-2][0-9]-[0-1][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9]");
    TRegexp r1(date+" [0-2][0-9]:[0-5][0-9]:[0-5][0-9]");

    int lcnt=0;

    int print = 0;

    while (!fin.eof())
    {
        char *txt=new char[i];
        fin.getline(txt, i-1);

        if (lcnt++<4)
            continue;

        if (!fin)
            txt[i-1]=0;

        line += txt;
        delete txt;

        if (line.Length()==0)
        {
            if (print)
                cout << endl;
            continue;
        }

        if (!fin)
        {
            i *= 2;
            fin.clear();
            continue;
        }

        if (!line(r0).IsNull())
            print = !line(r1).IsNull();

        if (print)
            Wrap(line);
        line = "";
        i=81;
    }
    cout << "DONE." << endl;
}

void runbook(const char *d="20[0-2][0-9]-[0-1][0-9]-[0-3][0-9]")
{
    TString regexp = "20[0-2][0-9]-[0-1][0-9]-[0-3][0-9]";

    if (regexp!=d)
    {
        TRegexp r(regexp);

        TString date=d;
        if (date(r).IsNull())
        {
            cout << "ERROR - Date has wrong format (2003-05-06)" << endl;
            return;
        }
    }

    MDirIter Next;
    Next.AddDirectory("/home/MAGIC/online_data/runbook", "CC_*.rbk");

    TString name;
    while (1)
    {
        name=Next();
        if (name.IsNull())
            break;

        ProcessFile(name, d);
    }
}
