/*
  restart.c
  6/2/92 wade@mit.edu

  This program is used to list the date and times
  the server was restarted.  It is run from the
  report.summary script.

  This program takes as input a file of dates the
  server was restarted, where each line of the file
  is in the form "Wed Apr  1 1992 12:31:18".

  The output of this program are all the times from
  the input file that are within the range of the
  starting and ending date arguments.

*/

#include <stdio.h>
#include <sys/types.h>
#include "date.h"
#include "network.h"
#include "network2.h"

struct inputrec {
  char transtype;
  char transdata[4000];
  struct daterec date;
};

int get_nextinputrec (struct inputrec *input, FILE *infile)
{
#define TIMESTAMP_LEN 26
#define DATESTAMP_LEN 15
  char astr[4000];
  char *ptr, *ptr2;

  do {
    if (fgets (astr, sizeof(astr), infile) == NULL)
      return EOF;

    /* remove trailing LF */
    ptr2 = astr;
    *(ptr2 + strlen(astr) - 1) = NULL;

    /* copy the line to the input data field */
    strcpy(input->transdata,ptr2);

    /* null out the trailing time stamp so that the date is 
       just "Mon DD Year" */
    ptr = astr;
    *(ptr + DATESTAMP_LEN) = NULL;

    ptr = astr + 4;  /* skip over the day, e.g. "Sun" */

    if (!parse_logdate(ptr, &(input->date), 1)) {
      continue;
    }
    else
      break;
  } while (1);

  return 1;
}

dousage(char *pgm)
{
  fprintf (stderr,
	   "Usage: %s [<increment>] [<starttime>] [<endtime>]\n\
Increment must be one of H,D,W,M,Y,O (default=O)\n\
   (Hour, Day, Week, Month, or Year, or\n\
   One interval from startdate to enddate)\n\
Date & time format -- `month day [hour:minute[:second]] [year]'\n\
   E.g. 'Aug 1 1991' (quotes are needed because of embedded spaces)\n\
   Default startdate is 1st of the month, default enddate is today\n", pgm);
  exit (-1);
}

stringtolower (char *str)
{
  char *ptr;

  for (ptr = str; *ptr ; ptr++)
    if (isupper(*ptr))
      *ptr = tolower(*ptr);
}

main (int argc, char *argv[])
{
  struct daterec startdate, enddate;
  struct daterec begof_intrvl, endof_intrvl, temp;
  struct inputrec input;
  char increment;
  FILE *datafile = stdin;
  int intervalnumber;
  int stat;
  
  if ((argv[1] != NULL && !strncmp ("-h", argv[1], 2)))
    dousage(argv[0]); /* never returns */
  
  if (argc < 2)
    increment = 'O';    /* Default interval size */
  else 
    increment = toupper ( *(argv[1]) );
  if (!index("HDWMYO", increment)) {
    fprintf (stderr, "Increment must be one of HDWMYO\n");
    exit (-21);
  }
  
  if (argc < 3) { 
    setdaterec (&startdate, time(0));
    startdate.hour = startdate.minute = startdate.sec = 0;
    startdate.day = 1;
  }
  else
    parse_logdate (argv[2], &startdate, 1);
  
  if (argc < 4)  /* enddate default =  now */
    setdaterec (&enddate, time(0));
  else
    parse_logdate (argv[3], &enddate, 1);

  if (!BEFORE(startdate, enddate)) {
    fprintf (stderr, "Startdate must be before enddate\n");
    exit (-1);
  }
  
  datecpy (&begof_intrvl, startdate); /* copy startdate into begof_intrvl */
  intervalnumber = 0;

  /* skip over data which is before the startdate */
  do {
    stat = get_nextinputrec(&input, datafile);
    /* if no date was specified, start with the first date found
       in the data file */
    if (argc < 3) {
      datecpy(&startdate,input.date);
      startdate.hour = startdate.minute = startdate.sec = 0;
      datecpy(&begof_intrvl, startdate);
    }

    /* fix enddate so that it does not include records from the
       date this process is running, since any records would be
       only a partial record for this day */
    if (argc < 4) {
      datedecr(&enddate,'D');
      enddate.hour = 23;
      enddate.minute = 59;
      enddate.sec = 59;
    }

  } while ((stat != EOF) && BEFORE(input.date, startdate));

  /* okay, now input contains our first real data, so let's do something
     with it! */
  do {

    /* try to figure out what timepoint the end of interval is */
    if (increment == 'O')                /* just one interval */
      datecpy (&endof_intrvl, enddate);

    else { 
      datecpy (&temp, begof_intrvl);   /* copy begof_intrvl into temp */
      dateincre (&temp, increment);    /* increase temp */

      if (BEFORE(temp, enddate))
	datecpy (&endof_intrvl, temp);     /* temp is the end of interval */
      else
	datecpy (&endof_intrvl, enddate);  /* enddate is the end of interval */
    }

    /* gather data for current interval */
    while (!AFTER(input.date, endof_intrvl) &&
	   !BEFORE(input.date, begof_intrvl) && (stat != EOF) ) {
      printf("%s\n",input.transdata);
      intervalnumber++;
      stat = get_nextinputrec(&input, datafile);
    }

    datecpy (&begof_intrvl, endof_intrvl); /* new begof_intrvl is endof intv */
  }
  while (BEFORE(begof_intrvl, enddate));

  if (intervalnumber == 0)
    printf("The server has not been restarted during this date range.\n");
}



