#include "dump2analysis.h"

int* GetIDS(char* str){
  int i, len;
  int p = 0;
  char *tok=NULL;
  int *d;
  
  for (i=0; str[i] != '\0'; i++) if (str[i] == ',') p++;
  if (i == 0) {
    d = malloc(sizeof(int) * 1);
    d[0] = 0;
  }
  else{
    d = malloc(sizeof(int) * (p+2));
    tok = strtok(str, ",");
    d[0] = p + 2;
    d[1] =  atoi(tok);
    for (i=2; i<d[0]; i++){
      tok = strtok( NULL, ",");  /* 2回目以降 */
      d[i] =  atoi(tok);
    }
  }
  return d;
}


int SetArgment(int argc, char *argv[], PARAM *arg){
  int opt, option_index;
  char Aid[102400], Bid[102400], Cid[102400];

  Aid[0] =  '\0';
  Bid[0] =  '\0';
  Cid[0] =  '\0';
  struct option long_options[] = {
    {"help",  no_argument,  NULL, 'h'},
    {"mode",  required_argument,  NULL, 'm'},
    {"Aid",  required_argument,  NULL, 'a'},
    {"Bid",  required_argument,  NULL, 'b'},
    {"Cid",  required_argument,  NULL, 'c'},

    {"Aelem",  required_argument,  NULL, 'x'},
    {"Belem",  required_argument,  NULL, 'y'},
    {"Celem",  required_argument,  NULL, 'z'},

    {"Atype",  required_argument,  NULL, 's'},
    {"Btype",  required_argument,  NULL, 't'},
    {"Ctype",  required_argument,  NULL, 'u'},
    
    {"out",  required_argument,  NULL, 'o'},
    {"in",  required_argument,  NULL, 'i'},

    {"rmin",  required_argument,  NULL, '0'},
    {"rmax",  required_argument,  NULL, '0'},
    {"dr",  required_argument,  NULL, '0'},
    {"rcut",  required_argument,  NULL, '0'},
    {"rcut_ab",  required_argument,  NULL, '0'},
    {"rcut_bc",  required_argument,  NULL, '0'},
    {"dt",  required_argument,  NULL, '0'},
    {"shift",  required_argument,  NULL, '0'},
    {"tau",  required_argument,  NULL, '0'},
    {"nx",  required_argument,  NULL, '0'},
    {"ny",  required_argument,  NULL, '0'},
    {"nz",  required_argument,  NULL, '0'},
    {"cube_type",  required_argument,  NULL, '0'},
    {"radius",  required_argument,  NULL, '0'},
    {0, 0, 0, 0}// 配列の最後はすべて0で埋める
  };
  if (argc == 1) Error(); /* 引数なし */
  
  /* b,c,d.oは引数が必須 */
  while((opt = getopt_long(argc, argv, "hm:a:b:c:s:t:u:x:y:z:o:i:",
                           long_options, &option_index)) != -1){
    if (opt == '0'){
      if (strcmp(long_options[option_index].name, "rmin")==0) RMIN = atof(optarg);
      if (strcmp(long_options[option_index].name, "rmax")==0) RMAX = atof(optarg);
      if (strcmp(long_options[option_index].name, "dr")==0) DR = atof(optarg);
      if (strcmp(long_options[option_index].name, "rcut")==0) RCUT = atof(optarg);
      if (strcmp(long_options[option_index].name, "rcut_ab")==0) RCUT_AB = atof(optarg);
      if (strcmp(long_options[option_index].name, "rcut_bc")==0) RCUT_BC = atof(optarg);
      if (strcmp(long_options[option_index].name, "dt")==0) DT = atof(optarg);
      if (strcmp(long_options[option_index].name, "shift")==0) TAU_SHIFT = atoi(optarg);
      if (strcmp(long_options[option_index].name, "tau")==0) TAU = atoi(optarg);
      if (strcmp(long_options[option_index].name, "nx")==0) NX = atoi(optarg);
      if (strcmp(long_options[option_index].name, "ny")==0) NY = atoi(optarg);
      if (strcmp(long_options[option_index].name, "nz")==0) NZ = atoi(optarg);
      if (strcmp(long_options[option_index].name, "cube_type")==0) CUBE_TYPE = atoi(optarg);
      if (strcmp(long_options[option_index].name, "radius")==0) RADIUS = atof(optarg);
    }

    switch(opt){
    case 'h': Error(); break;
    case 'm': strcpy(arg->mode, optarg); break;
      
    case 'a': strcpy(Aid, optarg); break;
    case 'b': strcpy(Bid, optarg); break;
    case 'c': strcpy(Cid, optarg); break;

    case 's': arg->Atype = atoi(optarg); break;
    case 't': arg->Btype = atoi(optarg); break;
    case 'u': arg->Ctype = atoi(optarg); break;

    case 'x': strcpy(arg->Aelem, optarg); break;
    case 'y': strcpy(arg->Belem, optarg); break;
    case 'z': strcpy(arg->Celem, optarg); break;
      
    case 'o': strcpy(arg->outfile, optarg); break;
    case 'i': strcpy(arg->infile, optarg); break;
    case '?':
      Error();
    }
  }
  arg->Aid = GetIDS(Aid);
  arg->Bid = GetIDS(Bid);
  arg->Cid = GetIDS(Cid);
  ArgCheck(arg);
  return 1;
}
