/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * Geophysical Computational Tools & Library (GCTL)
 *
 * Copyright (c) 2023  Yi Zhang (yizhang-geo@zju.edu.cn)
 *
 * GCTL is distributed under a dual licensing scheme. You can redistribute 
 * it and/or modify it under the terms of the GNU Lesser General Public 
 * License as published by the Free Software Foundation, either version 2 
 * of the License, or (at your option) any later version. You should have 
 * received a copy of the GNU Lesser General Public License along with this 
 * program. If not, see <http://www.gnu.org/licenses/>.
 * 
 * If the terms and conditions of the LGPL v.2. would prevent you from using 
 * the GCTL, please consider the option to obtain a commercial license for a 
 * fee. These licenses are offered by the GCTL's original author. As a rule, 
 * licenses are provided "as-is", unlimited in time for a one time fee. Please 
 * send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget 
 * to include some description of your company and the realm of its activities. 
 * Also add information on how to contact you by electronic and paper mail.
 ******************************************************/

#include "gridmanager.h"
// We use the editline library to handle all inputs
#include "editline.h"

cmd_pair curr_cmd; // Executing command
gctl::regular_grid rg; // Grid object

extern "C" {

/*
 * Strip whitespace from the start and end of STRING.  Return a pointer
 * into STRING.
 */
char *stripwhite(char *string)
{
    char *s, *t;

    for (s = string; isspace(*s); s++) ;

    if (*s == 0)
	return s;

    t = s + strlen(s) - 1;
    while (t > s && isspace(*t))
	t--;
    *++t = '\0';

    return s;
}

/* Generator function for command completion.  STATE lets us
   know whether to start from scratch; without any state
   (i.e. STATE == 0), then we start at the top of the list. */
char *command_generator(const char *text, int state)
{
    static int list_index, len;

    /* If this is a new word to complete, initialize now.  This
       includes saving the length of TEXT for efficiency, and
       initializing the index variable to 0. */
    if (!state)
    {
        list_index = 0;
        len = strlen(text);
    }

    /* Return the next name which partially matches from the command list. */
    while (list_index < CMD_NUM)
    {
        if (std::string(text) == commands[list_index].name.substr(0, len))
        {
            //const char* name = commands[list_index].name.data();
            return strdup(commands[list_index].name.data());
        }
        else list_index++;
    }

    /* If no names matched, then return NULL. */
    return nullptr;
}

/*
 * Attempt to complete on the contents of TEXT.  START and END
 * bound the region of rl_line_buffer that contains the word to
 * complete.  TEXT is the word to complete.  We can use the entire
 * contents of rl_line_buffer in case we want to do some simple
 * parsing. Return the array of matches, or NULL if there aren't any.
 */
char **gridmanager_completion(const char *text, int start, int end)
{
    char **matches = nullptr;

    /* If this word is at the start of the line, then it is a command
       to complete.  Otherwise it is the name of a file in the current
       directory. */
    if (start == 0) matches = rl_completion_matches(text, command_generator);

    return matches;
}

void initialize_readline(void)
{
    /* Allow conditional parsing of the ~/.inputrc file. */
    rl_readline_name = "gridmanager";

    /* Tell the completer that we want a crack first. */
    rl_attempted_completion_function = gridmanager_completion;
}

} // End C section

void display_cmds()
{
    std::clog << "Command:\n";
    for (size_t i = 0; i < CMD_NUM - 1; i++)
    {
        std::clog << std::setw(12) << commands[i].name << ":\t" << commands[i].brief << "\n";
    }

    std::clog << "\nEnter \"<command>?\" to see detailed instructions.\n\n";
    std::clog << "Keywords:\n";
    for (size_t i = 0; i < KEY_NUM; i++)
    {
        std::clog << std::setw(12) << keywords[i].name << ":\t" << keywords[i].brief << "\n";
    }
    return;
}

void display_help(std::string input_cmd)
{
    std::string install_dir = GCTL_MESH_INSTALL_PREFIX;
    std::ifstream helpin;
    open_infile(helpin, install_dir + "/sbin/share/readme_gridmanager", ".md");

    std::string tmp_l, tmp_help;
    std::vector<std::string> cmds;
    std::vector<std::string> helps;
    while (getline(helpin, tmp_l))
    {
        if (tmp_l.substr(0, 4) == "####")
        {
            cmds.push_back(tmp_l.substr(5));
            tmp_help = "";
            while (getline(helpin, tmp_l))
            {
                if (tmp_l == "") break;
                else tmp_help += tmp_l + "\n";
            }
            helps.push_back(tmp_help);
        }        
    }
    helpin.close();

    std::string cmd_str; 
    for (size_t j = 0; j < cmds.size(); j++)
    {
        parse_string_to_value(cmds[j], ' ', true, cmd_str);
        if (input_cmd == cmd_str)
        {
            replace_all(tmp_l, cmds[j], "\\", "");
            std::cout << GCTL_BOLDGREEN << tmp_l << GCTL_RESET << "\n";
            std::cout << helps[j] << "\n";
            break;
        }
    }
    return;
}

void exec_cmd(std::string cmd)
{
    std::string cmd_name;
    parse_string_to_value(cmd, ' ', true, cmd_name);

    // show instruction if there is a question mark at end of the command
    if (cmd_name.back() == '?')
    {
        cmd_name = cmd_name.substr(0, cmd_name.length() - 1);
        display_help(cmd_name);
        return;
    }

    // set default command to null
    curr_cmd = commands[CMD_NUM - 1];
    for (size_t i = 0; i < CMD_NUM - 1; i++)
    {
        if (cmd_name == commands[i].name)
        {
            curr_cmd = commands[i]; break;
        }
    }

    if (curr_cmd.func_p == nullptr) throw gctl::runtime_error("Invalid command: " + cmd_name);

    std::vector<std::string> cmd_units;
    parse_string_with_quotes(cmd, cmd_units);

    return curr_cmd.func_p(cmd_units);
}

// This function is defined to avoid potential breakdown while running in script mode.
void quit(const std::vector<std::string> &cmd_units)
{
    return;
}

void mesh_info(const std::vector<std::string> &cmd_units)
{
    // info [\<new-grid-name\>] [\<new-info\>]
    if (cmd_units.size() == 1)
    {
        rg.show_info();
        return;
    }

    gctl::array<std::string> copy_str(2, "null");
    for (size_t i = 1; i <= GCTL_MIN(cmd_units.size() - 1, 2); i++)
    {
        copy_str[i - 1] = cmd_units[i];
    }

    if (copy_str[0] != "null") rg.set_meshname(copy_str[0]);
    if (copy_str[1] != "null") rg.set_meshinfo(copy_str[1]);
    return;
}

#ifdef GCTL_MESH_EXPRTK

inline double expr2double(std::string s)
{
    exprtk::symbol_table<double> sym_tab;
    exprtk::expression<double> expr;
    exprtk::parser<double> p;

    sym_tab.add_constants();
    expr.register_symbol_table(sym_tab);
    p.compile(s, expr);

    return expr.value();
}

#endif // GCTL_MESH_EXPRTK

void new_grid(const std::vector<std::string> &cmd_units)
{
    // new \<new-file\> \<xnum\> \<ynum\> \<xmin\> \<ymin\> \<dx\> \<dy\> [\<info\>]
    if (cmd_units.size() < 8) throw std::runtime_error("new-grid: insufficient parameters.");

    gctl::array<std::string> copy_str(8, "null");
    for (size_t i = 1; i <= GCTL_MIN(cmd_units.size() - 1, 8); i++)
    {
        copy_str[i - 1] = cmd_units[i];
    }

    if (rg.initiated()) rg.clear_regular_grid();
    
    double xmin, ymin, dx, dy;
#ifdef GCTL_MESH_EXPRTK

    xmin = expr2double(copy_str[3]);
    ymin = expr2double(copy_str[4]);
    dx = expr2double(copy_str[5]);
    dy = expr2double(copy_str[6]);

#else

    xmin = atof(copy_str[3].c_str());
    ymin = atof(copy_str[4].c_str());
    dx = atof(copy_str[5].c_str());
    dy = atof(copy_str[6].c_str());

#endif // GCTL_MESH_EXPRTK

    rg.init(copy_str[0], copy_str[7], atoi(copy_str[1].c_str()), 
        atoi(copy_str[2].c_str()), xmin, ymin, dx, dy);
    return;
}

void open_grid(const std::vector<std::string> &cmd_units)
{
    // open binary <file>
    // open netcdf <file> [<x-name> <y-name>] [<data-name>] [node|cell]
    // open surfer <file> <new-data-name> [surfer6-text|surfer6-binary|surfer7] [node|cell]
    if (cmd_units.size() < 3) throw std::runtime_error("open: insufficient parameters.");

    if (cmd_units[1] == "binary") open_binary(cmd_units);
    else if (cmd_units[1] == "netcdf") open_netcdf(cmd_units);
    else if (cmd_units[1] == "surfer") open_surfer(cmd_units);
    else throw std::runtime_error("open: unknown file format.");
    return;
}

void save_grid(const std::vector<std::string> &cmd_units)
{
    // save binary <file>
    // save netcdf <file> [<data-name>|node|cell]
    // save surfer <file> <data-name> [surfer6-text|surfer6-binary|surfer7]
    // save gmsh <file>
    // save text <file> <data-name> [<data-name>]...
    if (cmd_units.size() < 3) throw std::runtime_error("save: insufficient parameters.");

    if (cmd_units[1] == "binary") save_binary(cmd_units);
    else if (cmd_units[1] == "netcdf") save_netcdf(cmd_units);
    else if (cmd_units[1] == "surfer") save_surfer(cmd_units);
    else if (cmd_units[1] == "gmsh") save_gmsh(cmd_units);
    else if (cmd_units[1] == "text") save_text(cmd_units);
    else throw std::runtime_error("save: unknown file format.");
    return;
}

void open_binary(const std::vector<std::string> &cmd_units)
{
    // open binary <file>
    rg.load_binary(cmd_units[2]);
    return;
}

void save_binary(const std::vector<std::string> &cmd_units)
{
    // save binary <file>
    rg.save_binary(cmd_units[2]);
    return;
}

void open_netcdf(const std::vector<std::string> &cmd_units)
{
    // open netcdf <file> [<x-name> <y-name>] [<data-name>] [node|cell]
    if (cmd_units.size() < 3) throw std::runtime_error("open: insufficient parameters.");

    gctl::array<std::string> copy_str(5, "null");
    for (size_t i = 0; i < GCTL_MIN(cmd_units.size() - 2, 5); i++)
    {
        copy_str[i] = cmd_units[i + 2];
    }
    
    mesh_data_type_e d_type = NodeData;
    if (copy_str[4] == "cell") d_type = ElemData;
    else if (copy_str[4] == "node") d_type = NodeData;

    if (copy_str[1] == "null") copy_str[1] = "x";
    if (copy_str[2] == "null") copy_str[2] = "y";
    if (copy_str[3] == "null") rg.load_netcdf_grid(copy_str[0], d_type, copy_str[1], copy_str[2]);
    else rg.load_netcdf_grid(copy_str[0], d_type, copy_str[1], copy_str[2], copy_str[3]);
    return;
}

void save_netcdf(const std::vector<std::string> &cmd_units)
{
    // save netcdf <file> [<data-name>|node|cell] [xname] [yname]
    if (cmd_units.size() < 3) throw std::runtime_error("save: insufficient parameters.");

    gctl::array<std::string> copy_str(4, "null");
    for (size_t i = 0; i < GCTL_MIN(cmd_units.size() - 2, 4); i++)
    {
        copy_str[i] = cmd_units[i + 2];
    }

    std::string xname = "x", yname = "y";
    if (copy_str[2] != "null") xname = copy_str[2];
    if (copy_str[3] != "null") yname = copy_str[3];

    if (copy_str[1] == "null") rg.save_netcdf_grid(copy_str[0], NodeData, xname, yname);
    else if (copy_str[1] == "node") rg.save_netcdf_grid(copy_str[0], NodeData, xname, yname);
    else if (copy_str[1] == "cell") rg.save_netcdf_grid(copy_str[0], ElemData, xname, yname);
    else rg.save_netcdf_grid(copy_str[0], copy_str[1], xname, yname);
    return;
}

void save_gmsh(const std::vector<std::string> &cmd_units)
{
    // save gmsh <file>
    if (cmd_units.size() < 3) throw std::runtime_error("save: insufficient parameters.");

    rg.save_gmsh_withdata(cmd_units[2], OverWrite, NotPacked);
    return;
}

void open_surfer(const std::vector<std::string> &cmd_units)
{
    // open surfer <file> [<new-data-name>] [surfer6-text|surfer6-binary|surfer7] [node|cell]
    if (cmd_units.size() < 4) throw std::runtime_error("open: insufficient parameters.");

    gctl::array<std::string> copy_str(4, "null");
    for (size_t i = 0; i < GCTL_MIN(cmd_units.size() - 2, 4); i++)
    {
        copy_str[i] = cmd_units[i + 2];
    }

    surfer_file_type_e st = Surfer6Text;
    if (copy_str[2] == "surfer6-text") st = Surfer6Text;
    else if (copy_str[2] == "surfer6-binary") st = Surfer6Binary;
    else if (copy_str[2] == "surfer7") st = Surfer7Grid;

    mesh_data_type_e d_type = NodeData;
    if (copy_str[3] == "node") d_type = NodeData;
    else if (copy_str[3] == "cell") d_type = ElemData;

    if (copy_str[1] == "null") rg.load_surfer_grid(copy_str[0], "Untitled", d_type, st);
    else rg.load_surfer_grid(copy_str[0], copy_str[3], d_type, st);
    return;
}

void save_surfer(const std::vector<std::string> &cmd_units)
{
    // save surfer <file> <data-name> [surfer6-text|surfer6-binary|surfer7]
    if (cmd_units.size() < 4) throw std::runtime_error("save: insufficient parameters.");

    gctl::array<std::string> copy_str(3, "null");
    for (size_t i = 0; i < GCTL_MIN(cmd_units.size() - 2, 3); i++)
    {
        copy_str[i] = cmd_units[i + 2];
    }

    surfer_file_type_e st = Surfer6Text;
    if (copy_str[2] == "surfer6-text") st = Surfer6Text;
    else if (copy_str[2] == "surfer6-binary") st = Surfer6Binary;
    else if (copy_str[2] == "surfer7") st = Surfer7Grid;

    rg.save_surfer_grid(copy_str[0], copy_str[1] ,st);
    return;
}

void save_text(const std::vector<std::string> &cmd_units)
{
    // save text <file> <data-name>
    if (cmd_units.size() < 4) throw std::runtime_error("save: insufficient parameters.");

    gctl::array<std::string> copy_str(cmd_units.size() - 3, "null");
    for (size_t i = 0; i < cmd_units.size() - 3; i++)
    {
        copy_str[i] = cmd_units[i + 3];
    }

    rg.save_text(cmd_units[2], copy_str);
    return;
}

void data_cloud(const std::vector<std::string> &cmd_units)
{
    // data-cloud \<new-data-name\> node|cell \<data-could-file\> \<search-x\> \<search-y\> \<search-deg\> [\<text_descriptor\>]
    if (cmd_units.size() < 7) throw std::runtime_error("data-cloud: insufficient parameters.");

    gctl::array<std::string> copy_str(7, "null");
    for (size_t i = 1; i <= GCTL_MIN(cmd_units.size() - 1, 7); i++)
    {
        copy_str[i - 1] = cmd_units[i];
    }

    mesh_data_type_e d_type = NodeData;
    if (copy_str[1] == "node") d_type = NodeData;
    else if (copy_str[1] == "cell") d_type = ElemData;
    else throw std::runtime_error("open-surfer: invalid grid data type.");
    
    text_descriptor desc;
    desc.file_name_ = copy_str[2];
    desc.file_ext_ = ".txt";
    desc.col_str_ = "0,1,2";
    desc.delimiter_ = ' ';
    desc.att_sym_ = '#';
    desc.head_num_ = 0;
    if (copy_str[6] != "null")
    {
        parse_string_to_value(copy_str[6], '/', true, desc.col_str_, 
            desc.delimiter_, desc.att_sym_, desc.head_num_);
    }

    std::vector<double> posix_vec, posiy_vec, data_vec;
    read_text2vectors(desc, posix_vec, posiy_vec, data_vec);

    array<point2dc> posi_arr(posix_vec.size());
    array<double> posi_val;

    posi_val.input(data_vec);
    for (size_t i = 0; i < posi_arr.size(); i++)
    {
        posi_arr[i].x = posix_vec[i];
        posi_arr[i].y = posiy_vec[i];
    }

    rg.load_data_cloud(posi_arr, posi_val, atof(copy_str[3].c_str()), atof(copy_str[4].c_str()), atof(copy_str[5].c_str()), copy_str[0], d_type);
    return;
}

void gradient(const std::vector<std::string> &cmd_units)
{
    // gradient <data-name> <new-data-name> dx|dy <order>
    if (cmd_units.size() < 4) throw std::runtime_error("gradient: insufficient parameters.");

    gctl::array<std::string> copy_str(4, "null");
    for (size_t i = 1; i <= GCTL_MIN(cmd_units.size() - 1, 4); i++)
    {
        copy_str[i - 1] = cmd_units[i];
    }

    gradient_type_e g_type;
    if (copy_str[2] == "dx") g_type = Dx;
    else if (copy_str[2] == "dy") g_type = Dy;
    else throw std::runtime_error("gradient: invalid gradient type.");

    int order = 1;
    if (copy_str[3] != "null") order = atoi(copy_str[3].c_str());

    rg.gradient(copy_str[0], copy_str[1], g_type, order);
    return;
}

void wavelet(const std::vector<std::string> &cmd_units)
{
    // wavelet \<data-name\> \<wavelet-name\> \<order\> [\<show-summary\>]
    if (cmd_units.size() < 4) throw std::runtime_error("wavelet: insufficient parameters.");

    gctl::array<std::string> copy_str(4, "null");
    for (size_t i = 1; i <= GCTL_MIN(cmd_units.size() - 1, 4); i++)
    {
        copy_str[i - 1] = cmd_units[i];
    }

    int order = order = atoi(copy_str[2].c_str());

    if (copy_str[3] == "yes") rg.wavelet(copy_str[0], copy_str[1], order, true);
    else rg.wavelet(copy_str[0], copy_str[1], order, false);
    return;
}

void sum_data(const std::vector<std::string> &cmd_units)
{
    // sum-data \<new-data-name\> \<data-name1\> \<data-name2\>
    if (cmd_units.size() < 4) throw std::runtime_error("sum: insufficient parameters.");

    gctl::array<std::string> copy_str(3, "null");
    for (size_t i = 1; i <= GCTL_MIN(cmd_units.size() - 1, 3); i++)
    {
        copy_str[i - 1] = cmd_units[i];
    }

    rg.sum(copy_str[0], copy_str[1], copy_str[2]);
    return;
}

void diff_data(const std::vector<std::string> &cmd_units)
{
    // sum-data \<new-data-name\> \<data-name1\> \<data-name2\>
    if (cmd_units.size() < 4) throw std::runtime_error("diff: insufficient parameters.");

    gctl::array<std::string> copy_str(3, "null");
    for (size_t i = 1; i <= GCTL_MIN(cmd_units.size() - 1, 3); i++)
    {
        copy_str[i - 1] = cmd_units[i];
    }

    rg.diff(copy_str[0], copy_str[1], copy_str[2]);
    return;
}

void bool_data(const std::vector<std::string> &cmd_units)
{
    // bool \<new-data-name\> \<data-name1> \<bool-data\> [reverse]
    if (cmd_units.size() < 4) throw std::runtime_error("bool: insufficient parameters.");

    gctl::array<std::string> copy_str(4, "null");
    for (size_t i = 1; i <= GCTL_MIN(cmd_units.size() - 1, 4); i++)
    {
        copy_str[i - 1] = cmd_units[i];
    }

    if (copy_str[3] == "reverse") rg.boolean(copy_str[0], copy_str[1], copy_str[2], true);
    else rg.boolean(copy_str[0], copy_str[1], copy_str[2]);
    return;
}

void func_data(const std::vector<std::string> &cmd_units)
{
    // function \<expression\> \<new-data-name\>
    if (cmd_units.size() < 3) throw std::runtime_error("function: insufficient parameters.");

    gctl::array<std::string> copy_str(3, "null");
    for (size_t i = 0; i <= GCTL_MIN(cmd_units.size() - 1, 3); i++)
    {
        copy_str[i] = cmd_units[i + 1];
    }

    rg.function(copy_str[0], copy_str[1]);
    return;
}

void calculator(const std::vector<std::string> &cmd_units)
{
    // calculator \<expression\> \<var1,data-name1\> \<var2,data-name2\>...
    if (cmd_units.size() < 4) throw std::runtime_error("calculator: insufficient parameters.");

    gctl::array<std::string> copy_str(cmd_units.size() - 1, "null");
    for (size_t i = 1; i <= cmd_units.size() - 1; i++)
    {
        copy_str[i - 1] = cmd_units[i];
    }

    array<std::string> var_list(copy_str.size() - 1), data_list(copy_str.size() - 1);
    for (size_t i = 0; i < var_list.size(); i++)
    {
        parse_string_to_value(copy_str[i + 1], ',', true, var_list[i], data_list[i]);
    }

    rg.calculator(copy_str[0], var_list, data_list);
    return;
}

void data_output(const std::vector<std::string> &cmd_units)
{
    // data-output enable|disable \<data-name1\> \<data-name2\>...
    if (cmd_units.size() < 3) throw std::runtime_error("data-output: insufficient parameters.");

    gctl::array<std::string> copy_str(cmd_units.size() - 1, "null");
    for (size_t i = 1; i <= cmd_units.size() - 1; i++)
    {
        copy_str[i - 1] = cmd_units[i];
    }

    meshdata *data_ptr;
    if (copy_str[0] == "enable")
    {
        for (size_t i = 1; i < copy_str.size(); i++)
        {
            data_ptr = rg.get_data_ptr(copy_str[i]);
            data_ptr->output_ok_ = true;
        }
    }
    else if (copy_str[0] == "disable")
    {
        for (size_t i = 1; i < copy_str.size(); i++)
        {
            data_ptr = rg.get_data_ptr(copy_str[i]);
            data_ptr->output_ok_ = false;
        }
    }
    else throw std::runtime_error("data-output: invalid operation type.");
    return;
}

void data_rename(const std::vector<std::string> &cmd_units)
{
    // data-rename [\<old-name\>] [\<new-name\>]
    if (cmd_units.size() < 3) throw std::runtime_error("rename: insufficient parameters.");

    gctl::array<std::string> copy_str(2, "null");
    for (size_t i = 1; i <= GCTL_MIN(cmd_units.size() - 1, 2); i++)
    {
        copy_str[i - 1] = cmd_units[i];
    }

    meshdata &curr_data = rg.get_data(copy_str[0]);
    curr_data.name_ = copy_str[1];
    return;
}

void data_view(const std::vector<std::string> &cmd_units)
{
    // plot [\<data-name\>]...
    if (cmd_units.size() < 2) throw std::runtime_error("view: insufficient parameters.");

    for (size_t i = 1; i < cmd_units.size(); i++)
    {
        rg.view(cmd_units[i]);
    }
    return;
}

void data_plot(const std::vector<std::string> &cmd_units)
{
    // plot [\<data-name\>]...
    if (cmd_units.size() < 2) throw std::runtime_error("plot: insufficient parameters.");

    for (size_t i = 1; i < cmd_units.size(); i++)
    {
        rg.plot(cmd_units[i]);
    }
    return;
}

void get_stats(const std::vector<std::string> &cmd_units)
{
    // statistic \<data-name1\> \<data-name2\>...
    if (cmd_units.size() < 2) throw std::runtime_error("statistic: insufficient parameters.");

    gctl::array<std::string> copy_str(cmd_units.size() - 1, "null");
    for (size_t i = 1; i <= cmd_units.size() - 1; i++)
    {
        copy_str[i - 1] = cmd_units[i];
    }

    meshdata *data_ptr;
    std::vector<double> stats;
    for (size_t i = 0; i < copy_str.size(); i++)
    {
        data_ptr = rg.get_data_ptr(copy_str[i]);
        data_ptr->show_info();
        data_ptr->show_stats();
    }
    return;
}

void get_profile(const std::vector<std::string> &cmd_units)
{
    // profile \<data-name\> \<out-file\> {\<x\>,\<y\> \<x\>,\<y\> \<num\>}|{\<xy-file\> [\<file-format\>]}
    if (cmd_units.size() < 4) throw std::runtime_error("profile: insufficient parameters.");

    gctl::array<std::string> copy_str(5, "null");
    for (size_t i = 1; i <= GCTL_MIN(cmd_units.size() - 1, 5); i++)
    {
        copy_str[i - 1] = cmd_units[i];
    }

    array<point2dc> xys;
    array<double> p_data;
    if (access(copy_str[2].c_str(), R_OK))
    {
        point2dc st_p, ed_p;
        gctl::parse_string_to_value(copy_str[2], ',', true, st_p.x, st_p.y);
        gctl::parse_string_to_value(copy_str[3], ',', true, ed_p.x, ed_p.y);
        rg.extract_profile(copy_str[0], st_p, ed_p, atoi(copy_str[4].c_str()), xys, p_data);
    }
    else // File exist
    {
        text_descriptor desc;
        desc.file_name_ = copy_str[2];
        desc.file_ext_ = ".txt";
        desc.att_sym_ = '#';
        desc.col_str_ = "0,1";
        desc.delimiter_ = ' ';
        desc.head_num_ = 0;

        if (copy_str[3] != "null")
        {
            gctl::parse_string_to_value(copy_str[3], '/', true, desc.col_str_, desc.delimiter_, 
                desc.att_sym_, desc.head_num_);
        }
        
        std::vector<double> xs, ys;
        gctl::read_text2vectors(desc, xs, ys);

        xys.resize(xs.size());
        for (size_t i = 0; i < xys.size(); i++)
        {
            xys[i].x = xs[i];
            xys[i].y = ys[i];
        }

        rg.extract_points(copy_str[0], xys, p_data);
    }
    
    std::ofstream ofile;
    open_outfile(ofile, copy_str[1], ".txt");
    ofile << "# x y d val\n";
    for (size_t i = 0; i < xys.size(); i++)
    {
        ofile << xys[i].x << " " << xys[i].y << " " << distance(xys[0], xys[i]) << " " << p_data[i] << "\n";
    }
    ofile.close();
    return;
}

int main(int argc, char *argv[])
{
    if (argc >= 2)
    {
        std::string tmp_l;
        std::ifstream cmdin;

        // Run commands from files. Each line is a command.
        // You can give more than one file
        for (size_t i = 1; i < argc; i++) try
        {
            open_infile(cmdin, argv[i]);
            while (getline(cmdin, tmp_l) && tmp_l != "" && tmp_l[0] != '#')
            {
                exec_cmd(tmp_l);
            }
            cmdin.close();
        }
        catch(std::exception& e)
        {
            GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, "run gridmanager in the interactive mode for instructions.", 0);
        }
    }
    else
    {
        setlocale(LC_CTYPE, "");
        initialize_readline();	/* Bind our completer. */

        display_logo();
        std::clog << "gridmanager - conversion, compuatation and manipulation of the grid data.\n";
        std::clog << "Enter '?' to see all available commands.\n";

        std::string cmd_str;
        char *c_line = (char *)NULL;
        char *c_line_s = (char *)NULL;
        bool quit = false;

        while (!quit) try
        {
            c_line = readline(">> ");
            if (!c_line) break;

            c_line_s = stripwhite(c_line);
            cmd_str = c_line_s;

            if (cmd_str == "quit") quit = true;
            else if (cmd_str == "?") display_cmds();
            else exec_cmd(cmd_str);

            free(c_line);
        }
        catch(std::exception& e)
        {
            GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, "Enter 'help' for instructions.", 0);
        }
    }
    return 0;
}