/* XTime v. 1.10
   Copyright November 1991 by Kurt R. Krueger

   This program is freely distributable in an unmodified form with the
   proviso that I retain all rights to it and how it is used.  I could
   put a disclaimer in here, but since this is free, your rights to sue
   me for any major screw up in the program are nil.  For this reason, I
   ask for no money and do not release rights to anyone who sells it for
   more than or equal to $0.01 above the cost of distribution.  Actually,
   the real reason why I ask for no money is that I know that I wouldn't
   see $0.01 in shareware fees anyway, but if there is one person out there
   who actually would send money, send it to the charity of your choice,
   and I will be happy.  Finally, I will specifically allow Fred Fish to
   put this in his archive if he really thinks it is worth it. */


/* I don't claim it's efficient.  I just claim it usually works. */

#define XFER_RATE 232
#include <time.h>

extern long atol();
extern int atoi();
extern long FindFiles();
struct tm *tmp;
struct tm *localtime();
char *param, k[] = "-k", r[] = "-r", f[] = "-f", r_string[4];
int n = 1, i = 0, r_len = 0, hours = 0, mins, h, m, s, min_xfer = 5L;
int filemode = 0, x_rate = XFER_RATE;
long secs, byte = 0, time(), tp;

main (argc, argv)

int argc;
char *argv[];

{
  if (argc == 1)
    {
    help();
    exit(0);
    }
/* Check to see if a number or text was entered.  If text, go to file mode. */
  byte = atol(argv[argc-1]);
  if (!byte)
    filemode = 1;

/* Parse the command line */
  param = argv[n];
  while (param[0] == '-')
    {
/* Set kilo mode if -k and not in filemode */
    if (strcmp(argv[n], k) == 0 && filemode == 0)
      byte *= 1000;

/* Separate the value after -r */
    if (strncmp(argv[n], r, 2) == 0)
      {
      r_len = strlen(argv[n]) - 2;
      for (i = 0; i < r_len; i++)
        r_string[i] = argv[n][i+2];
      x_rate = atoi(r_string);
      if (x_rate == 0)
        x_rate = XFER_RATE;
      }

/* Parse out -f */
    if (!strcmp(argv[n], f))
      filemode = 1;

/* Complete loop and go on to parse out next parameter. */
    param = argv[n++];
    }

/* if true, go into file mode to get file size(s) */
  if (filemode)
    {
    byte = FindFiles(argv[argc-1]);
    if (!byte)
      {
      printf ("\nNo files matched.\n\n");
      exit(0);
      }
    /* PTH printf ("\nTotal size: %ld bytes\n", byte); */
    printf ("--------------------------------------\n");
    printf ("Total size:                   %8ld bytes\n", byte);
    }

  secs = byte / x_rate;
  mins = secs / 60;
  hours = mins / 60;
  secs -= (long)mins * 60;
  mins -= hours * 60;
  if (secs <= min_xfer)
    secs = min_xfer;
  printf ("\nAssumed transfer rate: %d CPS\n", x_rate);
  printf ("Transfer should take:  %dh %dm %lds\n", hours, mins, secs);

  /* Get clock time and estimate completion time. */
  
  time(&tp);
  tmp = localtime(&tp);
  h = tmp->tm_hour;
  m = tmp->tm_min;
  s = tmp->tm_sec;
  printf ("The current time is:   %02d:%02d:%02d\n", h, m, s);
  s += secs;
  if (s >= 60)
    {
    mins++;
    s -= 60;
    }
  m += mins;
  if (m >= 60)
    {
    hours++;
    m -= 60;
    }
  h += hours;
  while (h > 23)
    h -= 24;

  printf ("Est. completion time:  %02d:%02d:%02d\n\n", h, m, s);
}

help()
  {
  printf("\nXTime v. 1.10\n");
  printf("Copyright 1991 by Kurt R. Krueger\n\n");
  printf("XTime [-k] [-r<cps>] <[file size in bytes] or [filename]>\n\n");
  printf("-f: Force file mode\n");
  printf("-k: File size is in K.  Turned off in file mode.\n");
  printf("-r: Assumed transfer rate is r characters/sec\n\n");
  }
