#define MAIN
#include <stdlib.h>
#include <string.h>
#include <grass/glocale.h>
#include "list.h"
#include "local_proto.h"

int main (int argc, char *argv[])
{
    int i,n;
    char *mapset;
    struct GModule *module;
    struct Option **parm, *p;
    char *from, *to;
    char buf1[256], *location_path;
    int result = 0;

    init (argv[0]);

	module = G_define_module();
	module->keywords = _("general");
    module->description =
		_("Copies available data files in the user's current mapset "
		"search path and location to the appropriate element "
		"directories under the user's current mapset.");

    parm = (struct Option **) G_calloc (nlist, sizeof(struct Option *));

    for (n = 0; n < nlist; n++)
    {
        p = parm[n] = G_define_option();
        p->key = list[n].alias;
        p->key_desc="from,to";
        p->type = TYPE_STRING;
        p->required = NO;
        p->multiple = NO;
        p->gisprompt = G_malloc (64);
        sprintf (p->gisprompt, "old,%s,%s", list[n].mainelem, list[n].maindesc);
        p->description = G_malloc (64);
        sprintf (p->description, _("%s file(s) to be copied"), list[n].alias);
    }

    if (G_parser(argc, argv))
        exit(1);

    /* check for even number of names for each element */
    for (n = 0; n < nlist; n++)
    {
        i = 0;
        if (parm[n]->answers)
            while (parm[n]->answers[i])
                i++;
        if (i%2) /* must be even number of names */
        {
            G_usage();
            G_fatal_error(_("must be even number of names"));
        }
    }

    location_path = G__location_path();

    for (n = 0; n < nlist; n++)
    {
        if (parm[n]->answers == NULL)
            continue;
        i = 0;
        while (parm[n]->answers[i])
        {
            from = parm[n]->answers[i++];
            to   = parm[n]->answers[i++];
            mapset = find (n, from, "");
            if (!mapset)
            {
                fprintf (stderr, _("<%s> not found\n"), from);
                continue;
            }
	    if (find (n, to, G_mapset()) && !(module->overwrite))
	    {
		fprintf (stderr, _("ERROR: <%s> already exists\n"), to);
		continue;
	    }
            if (G_legal_filename (to) < 0)
            {
                fprintf (stderr, _("ERROR: <%s> illegal name\n"), to);
                continue;
            }
            if (strcmp (mapset, G_mapset()) == 0 && strcmp (from, to) == 0)
            {
                fprintf (stderr, _("%s=%s,%s: files are the same, no copy required\n"),
                    parm[n]->key,from,to);
                continue;
            }
            if ( do_copy (n, from, mapset, to) == 1 )
            {
                result = 1;
            }
	    sprintf (buf1, "%s/%s/cell_misc/%s/reclassed_to",
			    location_path, mapset, to);
	    remove(buf1);
        }
    }
    exit(result);
}
