/* This file is part of the Neper software package. */
/* Copyright (C) 2003-2014, 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 != NULL)
  {
    ut_string_separate ((*pIn).nstring, NEUT_SEP_FRAC, &args, &argqty);
    (*pIn).levelqty = argqty;
    (*pIn).n = ut_alloc_1d_pchar ((*pIn).levelqty + 1);
    (*pIn).n[0] = ut_alloc_1d_char (2);
    strcpy ((*pIn).n[0], "1");
    for (i = 1; i <= (*pIn).levelqty; i++)
    {
      (*pIn).n[i] = ut_alloc_1d_char (strlen (args[i - 1]) + 1);
      strcpy ((*pIn).n[i], args[i - 1]);
    }
    ut_free_2d_char (args, (*pIn).levelqty);
    args = NULL;
  }
  else
    (*pIn).levelqty = 0;

  if ((*pIn).levelqty > 0)
  {
    // Processing idstring
    (*pIn).id = ut_alloc_1d_int ((*pIn).levelqty + 1);
    ut_array_1d_int_set ((*pIn).id, (*pIn).levelqty + 1, -1);

    argqty = 0;
    if ((*pIn).idstring != NULL)
    {
      ut_string_separate ((*pIn).idstring, NEUT_SEP_FRAC, &args, &argqty);
      if (argqty > (*pIn).levelqty)
      {
	ut_print_message (2, 0,
			  "The number of levels in option `-id' exceeds the number of levels in option `-n'.");
	abort ();
      }

      for (i = 1; i <= argqty; i++)
	sscanf (args[i - 1], "%d", &((*pIn).id[i]));
      ut_free_2d_char (args, argqty);
    }

    if ((*pIn).levelqty > 1 && argqty != (*pIn).levelqty)
      ut_print_message (2, 0,
			"For multilevel tessellation, -id must be specified for all levels.\n");

    // Processing morphostring
    (*pIn).morpho = ut_alloc_1d_pchar ((*pIn).levelqty + 1);
    if ((*pIn).morphostring != NULL)
    {
      ut_string_separate ((*pIn).morphostring, NEUT_SEP_FRAC, &args, &argqty);
      if (argqty > (*pIn).levelqty)
      {
	ut_print_message (2, 0,
			  "The number of levels in option `-morpho' exceeds the number of levels in option `-n'.");
	abort ();
      }

      for (i = 1; i <= argqty; i++)
      {
	(*pIn).morpho[i] = ut_alloc_1d_char (strlen (args[i - 1]) + 1);
	strcpy ((*pIn).morpho[i], args[i - 1]);
	if (i > 1 && !strcmp ((*pIn).morpho[i], "tocta"))
	{
	  ut_print_message (2, 0,
			    "`-morpho tocta' is only available at level 1.\n");
	  abort ();
	}
      }
      ut_free_2d_char (args, argqty);
      args = NULL;

      for (i = argqty + 1; i <= (*pIn).levelqty; i++)
      {
	(*pIn).morpho[i] = ut_alloc_1d_char (8);
	strcpy ((*pIn).morpho[i], "poisson");
      }
    }

    // Processing weightstring
    (*pIn).weight = ut_alloc_1d_pchar ((*pIn).levelqty + 1);
    if ((*pIn).weightstring != NULL)
    {
      ut_string_separate ((*pIn).weightstring, NEUT_SEP_FRAC, &args, &argqty);
      if (argqty > (*pIn).levelqty)
      {
	ut_print_message (2, 0,
			  "The number of levels in option `-weight' exceeds the number of levels in option `-n'.");
	abort ();
      }

      for (i = 1; i <= argqty; i++)
      {
	(*pIn).weight[i] = ut_alloc_1d_char (strlen (args[i - 1]) + 1);
	strcpy ((*pIn).weight[i], args[i - 1]);
      }
      ut_free_2d_char (args, argqty);
      args = NULL;

      for (i = argqty + 1; i <= (*pIn).levelqty; i++)
      {
	(*pIn).weight[i] = ut_alloc_1d_char (8);
	strcpy ((*pIn).weight[i], "none");
      }
    }

    // Processing centroidstring
    (*pIn).centroid = ut_alloc_1d_int ((*pIn).levelqty + 1);

    if ((*pIn).centroidstring != NULL)
    {
      ut_string_separate ((*pIn).centroidstring, NEUT_SEP_FRAC, &args,
			  &argqty);
      for (i = 1; i <= argqty; i++)
	sscanf (args[i - 1], "%d", &((*pIn).centroid[i]));
      ut_free_2d_char (args, argqty);
    }

    // Processing hardcorestring
    (*pIn).hardcore = ut_alloc_1d_pchar ((*pIn).levelqty + 1);

    if ((*pIn).hardcorestring != NULL)
    {
      ut_string_separate ((*pIn).hardcorestring, NEUT_SEP_FRAC, &args,
			  &argqty);
      for (i = 1; i <= argqty; i++)
	ut_string_string (args[i - 1], &((*pIn).hardcore[i]));
      ut_free_2d_char (args, argqty);
    }

    // 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);
    }
  }

  /* 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 ((*pIn).id[i] > 0)
	    sprintf ((*pIn).body, "%s%d", (*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;
}
