#include <unistd.h>
#include <getopt.h>
#include "dump2sq.h"

void Error(){
  printf("usage: dump2sq [options] inputfile\n");
  printf("inputfile format\n");
  printf(" LAMMPS:  *.lammpstrj\n");
  printf(" FEMTECK: *.10\n");
  printf("\n");
  printf("options:\n");
  printf(" -h  --help          show this help message\n");
  printf("     --SQmin=%%f      Q minimum [0.2]\n");
  printf("     --SQmax=%%f      Q maximum [30]\n");
  printf("     --SQdel=%%d      Q delta [0.02]\n");
  printf("     --GRmin=%%f      r minimum [0.025]\n");
  printf(" -r  --GRmax=%%f      r maximum [L(minium cell length)*0.5]\n");
  printf(" -s  --GRdel=%%d      r delta [0.05]\n");
  printf(" -o  --output=%%s     Output basename [basename]\n");
  exit(1);
}

/* ####################################################################### */
/* ベース名を決める */
/* ####################################################################### */
void SetBaseName(COMMAND *com){
  int nLen;
  char *res = NULL;
  int i;
  char buf[1024];
  char infile[1024];

  strcpy(infile, com->infile);
  nLen = sizeof(infile);
  if ((nLen > 0) && (nLen <= 1030)) {
    res = strrchr(infile, '.');
    if(res != NULL) *res = '\0';
  }
  strcpy(com->outbase, infile);
  /* 拡張子 */
  for (i=strlen(com->infile); i>0; i-- ) {
    if (com->infile[i] == '.' ) break;
  }
  if (i == 0) strcpy(buf, "");
  else strncpy(buf, com->infile+i+1, strlen(com->infile)-i);
  /* ファイル形式の決定 */
  if (strcmp(buf, "lammpstrj") == 0) strcpy(com->filetype, "lammpstrj");
  else if (strcmp(buf, "xyz") == 0) strcpy(com->filetype, "xyz");
  else if (strcmp(buf, "10") == 0) strcpy(com->filetype, "xyz");
  else {
    printf("Input file extension: %s\n", buf);
    printf("The file format is unknown.\n");
    printf("Acceptable file formats are .lammpstrj, .xyz, .10\n");
  }
}


int SetArgment(int argc, char **argv, COMMAND *com){
  int opt, option_index;
  
  struct option long_options[] = {
    {"help",  no_argument,  NULL, 'h'},
    {"SQmin",  required_argument,  NULL, 'E'},
    {"SQmax",  required_argument,  NULL, 'F'},
    {"SQdel",  required_argument,  NULL, 'G'},
    {"GRmin",  required_argument,  NULL, 'J'},
    {"GRmax",  required_argument,  NULL, 'r'},
    {"GRdel",  required_argument,  NULL, 's'},
    {"output", required_argument,  NULL, 'o'},
    {0, 0, 0, 0}// 配列の最後はすべて0で埋める
  };
    
  //opterr = 0;/* エラーメッセージを非表示にする */
  if (argc == 1) Error();
  FILE* fp = fopen(argv[argc-1], "r");
  if (fp == NULL) Error();
    
  /* default値 */
  strcpy(com->infile, argv[argc-1]);
  com->SQmin = 0.2;
  com->SQmax = 30.0;
  com->SQdel = 0.02;
  com->GRmin = 0.025;
  com->GRmax = -1; /* default Lmin*1.0 */
  com->GRdel = 0.05;
  SetBaseName(com);
  
  while((opt = getopt_long(argc, argv, "hE:F:G:J:r:s:o:", 
			   long_options, &option_index)) != -1){
    switch(opt){
    case 'h': Error(); break;
    case 'E': com->SQmin = atof(optarg); break;
    case 'F': com->SQmax = atof(optarg); break;
    case 'G': com->SQdel = atof(optarg); break;
    case 'J': com->GRmin = atof(optarg); break;
    case 'r': com->GRmax = atof(optarg); break;
    case 's': com->GRdel = atof(optarg); break;
    case 'o': strcpy(com->outbase, optarg); break;
      // 解析できないオプションが見つかった場合は「?」を返す
      // オプション引数が不足している場合も「?」を返す
    case '?': 
      Error();
    }
    continue;
  }
  if (com->GRmax == 0.0) Error();
  printf("basename: %s\n", com->outbase);
  sprintf(com->gr_file, "%s.gr", com->outbase);
  sprintf(com->cn_file, "%s.cn", com->outbase);
  
  return 0; 
}
