/* important to put all the doubles first
since the C compiler by default will align these at 8 byte boundaries */
extern struct cmdLineArgs {
  double betaBQ1,errorBQ1,KnLBQ1;
  double betaAQ1,errorAQ1,KnLAQ1;
  char plane[4];
  long verbose;
} cmdlineargs_;

#include "mdb.h"
#include "scan.h"
#include "match_string.h"

#define PLANE 0
#define VERBOSE 1
#define BQ1 2
#define AQ1 3
#define N_OPTIONS 4
char *option[N_OPTIONS] = {
  "plane",
  "verbose",
  "bq1",
  "aq1",
  };

#define XPLANE 0
#define YPLANE 1
#define PLANE_TYPES 2
char *planes[PLANE_TYPES] = {
  "x", "y", };

static char *USAGE = "betaAtQ1 -plane={x|y} \n\
   -BQ1=beta=<value>[,error=<value>][,KnL=<value>] \n\
   -AQ1=beta=<value>[,error=<value>][,KnL=<value>]";

static char *BQ1Usage = "-BQ1=beta=<value>[,error=<value>][,KnL=<value>]";
static char *AQ1Usage = "-AQ1=beta=<value>[,error=<value>][,KnL=<value>]";

char *plane = NULL;
long verbose = 0;
double betaBQ1 = 0.0;
double errorBQ1 = 0.0;
double KnLBQ1 = 0.0;
double betaAQ1 = 0.0;
double errorAQ1 = 0.0;
double KnLAQ1 = 0.0;

/* since the function is called from fortran, all small letters
   is required */

void readcmdlineargs_(long *argc, char *argv, long length);
void init_stats_();
void report_stats_();

void readcmdlineargs_(long *argc, char *argv, long length) {
  SCANNED_ARG *scanned;
  long i, type_index, iterations;
  long unsigned dummyFlag;

  *argc  =  scanargs(&scanned, *argc, &argv);
  if (!*argc) {
    fprintf(stderr,"%s\n",USAGE);
    exit(1);
  }
  /* note that one one argument at a time is passed by the
     main fortran program. */
  for (i = 0; i<*argc; i++) {
    if (scanned[i].arg_type == OPTION) {
      delete_chars(scanned[i].list[0], "_");
      delete_chars(scanned[i].list[0], " ");
      switch(match_string(scanned[i].list[0], option, N_OPTIONS, UNIQUE_MATCH)) {
      case VERBOSE:
        verbose = 1;
        break;
      case PLANE:
        if (scanned[i].n_items > 1) {
          if (0>(type_index = match_string(scanned[i].list[1], 
                                            planes, PLANE_TYPES, UNIQUE_MATCH))) {
            printf("Plane %s isn't valid.\n", scanned[i].list[1]);
            exit(1);
          }
          plane = (char *) malloc(sizeof(char)*4);
          strcpy(plane, strtok(planes[type_index], " ") );
          strcpy(cmdlineargs_.plane, plane);
        } 
        else {
          bomb("plane not specified", USAGE);
        }
        break;
      case BQ1:
        if((scanned[i].n_items-=1)<0 ||
           !scanItemList(&dummyFlag, scanned[i].list+1, &scanned[i].n_items, 0,
                         "beta", SDDS_DOUBLE, &betaBQ1, 1, 0,
                         "error", SDDS_DOUBLE, &errorBQ1, 1, 0,
                         "KnL", SDDS_DOUBLE, &KnLBQ1, 1, 0,
                         NULL) || betaBQ1==0 ) {
          fprintf( stderr,"BQ1: beta %lf error %lf KnL %lf\n", betaBQ1, errorBQ1, KnLBQ1);
          bomb("bad -BQ1 syntax", BQ1Usage);
        }
        break;
      case AQ1:
        if((scanned[i].n_items-=1)<0 ||
           !scanItemList(&dummyFlag, scanned[i].list+1, &scanned[i].n_items, 0,
                         "beta", SDDS_DOUBLE, &betaAQ1, 1, 0,
                         "error", SDDS_DOUBLE, &errorAQ1, 1, 0,
                         "KnL", SDDS_DOUBLE, &KnLAQ1, 1, 0,
                         NULL) || betaAQ1==0 ) {
          fprintf( stderr,"AQ1: beta %lf error %lf KnL %lf\n", betaAQ1, errorAQ1, KnLAQ1);
          bomb("bad -AQ1 syntax", AQ1Usage);
        }
        break;
      default:
        bomb("unknown option given.", USAGE);  
      }
    }
    else {
      bomb("bad argument", USAGE);
    }
  }
  cmdlineargs_.verbose = verbose;
  cmdlineargs_.betaBQ1 = betaBQ1;
  cmdlineargs_.errorBQ1 = errorBQ1;
  cmdlineargs_.KnLBQ1 = KnLBQ1;
  cmdlineargs_.betaAQ1 = betaAQ1;
  cmdlineargs_.errorAQ1 = errorAQ1;
  cmdlineargs_.KnLAQ1 = KnLAQ1;
  return;
}

void init_stats_() {
  init_stats();
}

void report_stats_() {
  report_stats(stdout, "statistics: ");
}
