/* This file is part of the Neper software package. */
/* Copyright (C) 2003-2015, Romain Quey. */
/* See the COPYING file in the top-level directory. */

#include"net_input_.h"

void
net_input_treatargs (int fargc, char **fargv, int argc, char **argv,
		     struct IN *pIn)
{
  int i;

  net_input_options_default (pIn);

  /* Reading options */

  net_input_options_set (pIn, fargc, fargv);
  net_input_options_set (pIn, argc, argv);

  // Processing nstring
  int argqty, argqty2;
  char **args = NULL;
  char **args2 = NULL;

  if ((*pIn).nstring)
    (*pIn).levelqty =
      net_input_treatargs_multiscale ("-n", (*pIn).nstring, -1, "1",
				      &((*pIn).n));
  else
    (*pIn).levelqty = 0;

  // net_input_treatargs_multiscale ((*pIn).nstring, (*pIn).levelqty, "1", &((*pIn).n));

  if ((*pIn).levelqty > 0)
  {
    // Processing idstring
    net_input_treatargs_multiscale ("-id", (*pIn).idstring,
				    (*pIn).levelqty, "1", &((*pIn).id));

    // Processing morphostring
    net_input_treatargs_multiscale ("-morpho", (*pIn).morphostring,
				    (*pIn).levelqty, "equiaxed",
				    &((*pIn).morpho));

    for (i = 2; i <= (*pIn).levelqty; i++)
      if (!strcmp ((*pIn).morpho[i], "tocta"))
	ut_print_message (2, 0,
			  "`-morpho tocta' is only available at level 1.\n");

    // Processing weightstring
    net_input_treatargs_multiscale ("-weight", (*pIn).weightstring,
				    (*pIn).levelqty, "0", &((*pIn).weight));

    // Processing randomizestring
    net_input_treatargs_multiscale ("-randomize", (*pIn).randomizestring,
				    (*pIn).levelqty, "0:1",
				    &((*pIn).randomize));

    // Processing centroidstring
    net_input_treatargs_multiscale ("-centroid", (*pIn).centroidstring,
				    (*pIn).levelqty, "0", &((*pIn).centroid));
    net_input_treatargs_multiscale ("-centroidfact",
				    (*pIn).centroidfactstring,
				    (*pIn).levelqty, "0",
				    &((*pIn).centroidfact));
    net_input_treatargs_multiscale ("-centroidconv",
				    (*pIn).centroidconvstring,
				    (*pIn).levelqty, "0",
				    &((*pIn).centroidconv));
    net_input_treatargs_multiscale ("-centroiditermax",
				    (*pIn).centroiditermaxstring,
				    (*pIn).levelqty, "0",
				    &((*pIn).centroiditermax));

    // Processing hardcorestring
    net_input_treatargs_multiscale ("-hardcore", (*pIn).hardcorestring,
				    (*pIn).levelqty, "1", &((*pIn).hardcore));

    // Testing consistenct between morpho = lamella and n, which must
    // start by lamwidth=
    if ((*pIn).morphostring != NULL)
    {
      ut_string_separate ((*pIn).morphostring, NEUT_SEP_FRAC, &args, &argqty);
      ut_string_separate ((*pIn).nstring, NEUT_SEP_FRAC, &args2, &argqty2);
      for (i = 0; i < argqty; i++)
	if (!strcmp (args[i], "lamella"))
	  if (strncmp (args2[i], "w=", 2) != 0)
	    ut_print_message (2, 0,
			      "With `-morpho lamella', you must use `-n w='.\n");

      ut_free_2d_char (args, argqty);
      ut_free_2d_char (args2, argqty2);
      args = NULL;
      args2 = NULL;
    }
  }

  /* Testing options */
  if ((*pIn).input == NULL)
  {
    ut_print_message (2, 2,
		      "Missing input data (options `-n' and `-id', or `-load*'.");
    abort ();
  }

  if (!strcmp ((*pIn).input, "n") && (*pIn).idstring == NULL)
    ut_print_message (2, 2, "Missing identifier (option `-id').\n");

  if (!strncmp ((*pIn).morphostring, "square", 6) && (*pIn).dim != 2)
    ut_print_message (2, 2, "Options `-morpho square' requires `-dim 2'.");

  /* Writing file names */

  if ((*pIn).body == NULL)
  {
    (*pIn).body = ut_alloc_1d_char (1000);

    if (!strcmp ((*pIn).input, "n"))
    {
      if ((*pIn).idstring != NULL)
      {
	sprintf ((*pIn).body, "n%s-id", (*pIn).nstring);
	for (i = 1; i <= (*pIn).levelqty; i++)
	{
	  if (strcmp ((*pIn).id[i], "-1") != 0)
	    sprintf ((*pIn).body, "%s%s", (*pIn).body, (*pIn).id[i]);
	  else
	    (*pIn).body = strcat ((*pIn).body, "rand");

	  if (i < (*pIn).levelqty)
	    (*pIn).body = strcat ((*pIn).body, NEUT_SEP_FRAC);
	}
      }
      else
      {
	if (!strcmp ((*pIn).morpho[1], "poisson"))
	  sprintf ((*pIn).body, "n%s-rand", (*pIn).nstring);
	else if ((*pIn).morpho[1][0] != '@')
	  sprintf ((*pIn).body, "n%s-%s", (*pIn).nstring, (*pIn).morpho[1]);
	else
	  sprintf ((*pIn).body, "n%s-coo", (*pIn).nstring);
      }
    }
    else if (!strcmp ((*pIn).input, "tess") || !strcmp ((*pIn).input, "tesr"))
      ut_string_body ((*pIn).load, ".", &((*pIn).body));
    else if (!strcmp ((*pIn).input, "n_reg"))
      sprintf ((*pIn).body, "n%s-%s", (*pIn).nstring, (*pIn).morpho[1]);
    else
      abort ();
  }

  if (!strcmp ((*pIn).domain, "default"))
  {
    if ((*pIn).dim == 3)
    {
      (*pIn).domain
	= ut_realloc_1d_char ((*pIn).domain, strlen ("cube(1,1,1)") + 1);
      strcpy ((*pIn).domain, "cube(1,1,1)");
    }
    else if ((*pIn).dim == 2)
    {
      (*pIn).domain
	= ut_realloc_1d_char ((*pIn).domain, strlen ("square(1,1)") + 1);
      strcpy ((*pIn).domain, "square(1,1)");
    }
    else if ((*pIn).dim == 1)
      ut_string_string ("segment(1)", &((*pIn).domain));
    else
      ut_error_reportbug ();
  }

  (*pIn).tess = ut_string_addextension ((*pIn).body, ".tess");
  (*pIn).mtess = ut_string_addextension ((*pIn).body, ".mtess");
  (*pIn).geo = ut_string_addextension ((*pIn).body, ".geo");
  (*pIn).mgeo = ut_string_addextension ((*pIn).body, ".mgeo");
  (*pIn).tesr = ut_string_addextension ((*pIn).body, ".tesr");
  (*pIn).raw = ut_string_addextension ((*pIn).body, ".raw");
  (*pIn).ply = ut_string_addextension ((*pIn).body, ".ply");
  (*pIn).dec = ut_string_addextension ((*pIn).body, ".3dec");
  (*pIn).obj = ut_string_addextension ((*pIn).body, ".obj");
  (*pIn).ori = ut_string_addextension ((*pIn).body, ".ori");
  (*pIn).debug = ut_string_addextension ((*pIn).body, ".debug");

  return;
}
