/* -------------------------------------------------------------
 * This procedure is called to check parameters that are
 * expected to have a numeric value.
 * It will return the value the time needs to be adjusted
 * for as appropriate for the parameter passed.
 * ------------------------------------------------------------- */
signed long check_parameter( char *parm, char *value ) {
   long worknum;
   long one_day;
   worknum = strtol(value, (char **)NULL, 10);
   if (worknum == 0) {
      printf( "Invalid value for parameter %s : %s\n", parm, value );
      exit( 1 );
   }

   one_day = 60 * 60 * 24;    /* seconds * minutes * hours in a day */

   if (strncmp("-daysback",parm,9) == 0) {
      return( (60 * 60 * 24 * worknum) * -1 );
   }
   else if (strncmp("-daysforward",parm,12) == 0) {
      return( 60 * 60 * 24 * worknum );
   }
   else if (strncmp("-hoursback",parm,10) == 0) {
      return( (60 * 60 * worknum) * -1 );
   }
   else if (strncmp("-hoursforward",parm,13) == 0) {
      return( 60 * 60 * worknum );
   }
   else {
      printf( "Flag %s is not a valid -format option\n", parm );
	  show_syntax();
	  exit( 1 );
   }
} /* check_parameter */
/* -------------------------------------------------------------
 * MAIN:
 * Check all parameters passed on the command line, calculate
 * the date to be displayed, and display it.
 * ------------------------------------------------------------- */
int main( int argc, char **argv, char **envp ) {
   struct tm *time_var;
   time_t time_number;
   signed long time_offset;
   char *ptr;
   int i, date_override;
   char saved_command[MAX_PARM_LEN+1];
   char saved_format[MAX_MASK_LEN+1];
   char saved_startdate_override[13]; /* YYYYMMDDHHMM_ */

   /*
    * Check to see what command line parameters we have
    */
   if (argc < 2) {
      printf( "%s: (c)Mark Dickinson, 2001\n", argv[0] );
      show_syntax();
      exit( 1 );
   }

   time_offset = 0;     /* default, and start point for adjustments */
   date_override = 0;   /* use current system date and time */
   strcpy(saved_format,"YYYYMMDD"); /* default */
   i = 1;
   /* use a while loop instead of a for loop as we may
    * increment the counter ourselves */
   while ( i < argc ) {
      ptr = argv[i];
      i++;
	  if (i >= argc) {
		 printf( "Missing value for %s\n", ptr );
		 exit( 1 );
      }
      strncpy( saved_command, ptr, MAX_PARM_LEN ); 
      ptr = argv[i];
      if (strncmp("-format",saved_command,7) == 0) {
         validate_format( ptr, saved_format );
      }
	  else if (strncmp("-workingdate",saved_command,12) == 0) {
         date_override = 1;
		 strncpy( saved_startdate_override, ptr, 12 ); /* YYYYMMDDHHMM */
      }
      else {
         time_offset = time_offset + check_parameter( saved_command, ptr );
      }
      i++;
   }
    
   /*
    * Work out the new time and print the result.
    */
   if (date_override == 1) {
      /* have to get the dst flag setting for this */
      time_number = time(0);
      time_var = localtime( &time_number );
	  /* then workout the callers passed time */
      time_number = make_time( (char *)&saved_startdate_override, time_var->tm_isdst );
   }
   else {
      time_number = time(0);     /* time now in seconds from 00:00, Jan 1 1970 */
   }
   time_number = time_number + time_offset;
   if (strcmp("CTIME",saved_format) == 0) {
      printf( "%s", ctime( &time_number ) );
   }
   else {
     time_var = localtime( &time_number );   
     print_time( time_var, saved_format ); 
   }
   exit( 0 );
} /* end main */
/*-------------------------------------------------------*/
int main(int argc, char **argv) {
  XDIFile *xdifile;
  long  file_length, ilen, index, i, j, ret;
  long  ncol, nrows, nheader, nwords, ndict, nout;
  int   is_newline, fnlen, k;
  double *tdat;

  /* require 2 arguments! */
  if (argc < 2) {
    show_syntax();
    return 1;
  }

  /* read xdifile */
  xdifile = malloc(sizeof(XDIFile));
  ret = XDI_readfile(argv[1], xdifile);

  /* react to a terminal error */
  if (ret < 0) {
    printf("Error reading XDI file '%s':\n     %s\t(error code = %ld)\n",
	   argv[1], xdifile->error_message, ret);
    XDI_cleanup(xdifile, ret);
    free(xdifile);
    return 1;
  }

  /* react to a warning */
  if (ret > 0) {
    printf("Warning reading XDI file '%s':\n     %s\t(warning code = %ld)\n\n",
	   argv[1], xdifile->error_message, ret);
  }

  /* print some basic information about the file to the screen */
  printf("#------\n# XDI FILE Read %s VERSIONS: |%s|%s|\n" ,
	 xdifile->filename, xdifile->xdi_version, xdifile->extra_version);
  printf("# Elem/Edge: %s|%s|\n", xdifile->element, xdifile->edge);
  printf("# User Comments:\n%s\n", xdifile->comments);

  /* print all the metadata to the screen, validate each item */
  printf("# Metadata(%ld entries):\n", xdifile->nmetadata);
  printf(" --- \n");
  for (i=0; i < xdifile->nmetadata; i++) {
    printf(" %s / %s => %s\n",
	   xdifile->meta_families[i],
	   xdifile->meta_keywords[i],
	   xdifile->meta_values[i]);

    j = XDI_validate_item(xdifile, xdifile->meta_families[i], xdifile->meta_keywords[i], xdifile->meta_values[i]);
    if (j!=0) {
      printf("-- Warning for %s.%s: %s\t(warning code = %ld)\n\t%s\n",
	     xdifile->meta_families[i], xdifile->meta_keywords[i], xdifile->meta_values[i], j, xdifile->error_message);
    }
  }

  /* do the test for REQUIRED metadata */
  j = XDI_required_metadata(xdifile);
  printf("\n# check for required metadata -- (requirement code %ld):\n%s\n", j, xdifile->error_message);

  /* do the test for RECOMMENDED metadata */
  j = XDI_recommended_metadata(xdifile);
  printf("\n# check for recommended metadata -- (recommendation code %ld):\n%s\n", j, xdifile->error_message);

  /* print the data table to the screen */
  nout = min(4, xdifile->npts - 2);
  printf("# Arrays Index, Name, Values: (%ld points total): \n", xdifile->npts);
  tdat = (double *)calloc(xdifile->npts, sizeof(double));
  for (j = 0; j < xdifile->narrays; j++ ) {
    ret = XDI_get_array_name(xdifile,xdifile->array_labels[j], tdat);
    printf(" %ld %9s: ", j, xdifile->array_labels[j]);
    for (k=0; k < nout; k++) {  printf("%.8g, ", tdat[k]); }
    /* printf("\n"); */
    printf("..., %.8g, %.8g\n", tdat[xdifile->npts-2], tdat[xdifile->npts-1]);
  }

  if ((strlen(xdifile->outer_label) > 0)&& xdifile->nouter > 1) {
    printf("OUTER Array (2D data): %ld, %s\n", xdifile->nouter, xdifile->outer_label);
    for (j = 0; j < 5; j++) { /*xdifile->nouter;  j++) {*/
      printf(" %ld/%g,  " , xdifile->outer_breakpts[j], xdifile->outer_array[j]);
    }
    printf(" ..., ");
    nout = xdifile->nouter;
    for (j = nout-4; j < nout; j++) { 
      printf(" %ld/%g,  " , xdifile->outer_breakpts[j], xdifile->outer_array[j]);
    }
    printf("\n");
  }

  /* free memory before leaving */
  free(tdat);
  XDI_cleanup(xdifile, 0);
  free(xdifile);
  return 0;
}