/*

    Minimum Profit 5.x
    A Programmer's Text Editor

    Directory edition.

    Copyright (C) 2012 Angel Ortega <angel@triptico.com>

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

    http://triptico.com

*/

/** editor actions **/

mp.actions['open_folder'] = sub {
    local f;

    if ((f = mp_drv.openfolder(L("Folder to open"))) != NULL) {
        local d = mp.add(new(mp_doc_dir, { name: f }));
        d->setup();
    }
};


/** action descriptions **/

mp.actdesc['open_folder'] = LL("Open folder...");

/** mp_doc_dir subclass **/

global mp_doc_dir = new(mp_doc);


/** code **/

sub mp_doc_dir.setup(doc)
{
    local n;

    doc.title = sprintf(L("<listing of %s>"), doc.name);

    /* get the directory content */
    doc.dir_content = glob(NULL, doc.name)->
        grep("@[^/]$@")->
        map(sub (e) { local l = split(e, '/'); e = l[-1]; });

    doc.txt.lines = [
        L("Directory editor"),
        L("Deleting a line deletes the file;"),
        L("Changing a line renames the file;"),
        L("Saving commits the changes."),
        L("(Keep the 'number:' on each line untouched)"),
        ''
    ]->map(sub(e) { '# ' ~ e;});

    for (n = 0; n < size(doc.dir_content); n++)
        push(doc.txt.lines, sprintf("%d: %s", n + 1, doc.dir_content[n]));

    doc.txt.mod = 0;

    if (doc.txt.y > size(doc.txt.lines))
        doc.txt.y = size(doc.txt.lines);

    doc.mtime = NULL;
}


sub mp_doc_dir.save(doc)
{
    local log = new(mp_doc, {
        name: sprintf(L("<changes to directory '%s'>"), doc.name)
        }
    );

    log.txt.lines = [];

    /* rename changed files */
    foreach (l, doc.txt.lines) {
        local p = l->sscanf("%d: %S");
        local n;

        if (size(p) == 2 && (n = p[0]) > 0 && p[1]) {
            local op = doc.dir_content[n - 1];
            local np = p[1];

            if (op ne np) {
                local m;

                if (rename(op, np) == -1)
                    m = sprintf(L("# error renaming '%s' to '%s' (%s)"),
                        op, np, ERRNO);
                else
                    m = sprintf(L("# renamed '%s' to '%s'"), op, np);

                push(log.txt.lines, m);
            }

            doc.dir_content[n - 1] = NULL;
        }
    }

    /* delete unreferenced files */
    foreach (l, doc.dir_content) {
        if (l != NULL) {
            local m;

            if (unlink(l) == -1)
                m = sprintf(L("# error deleting '%s' (%s)"), l, ERRNO);
            else
                m = sprintf(L("# deleted '%s'"), l);

            push(log.txt.lines, m);
        }
    }

    mp.add(log);
    log.read_only = 1;

    doc.txt.mod = 0;

    doc->setup();

    return 0;
}
