/******************************************************************************
*
    FILE:  PINGCOPY.C
    USAGE: PINGCOPY <source-path> <destination-path\ship-name>

           This program is used to copy PINGDATA.* files in the source
           directory to the destination directory, at the same time
           renaming them using the following coding scheme:

              first 2 characters   = code for ship name provided on
                                     command line, e.g., MW = Moana Wave
              3rd character        = last digit of the year, e.g.,
                                     8 for 1988
              4th character        = hexadecimal code for month
                                     (1 - 9 = January to September)
                                     (A - C = October to December)
              5th character        = code for day of month
                                     (1 - 9 = 1st to 9th)
                                     (A - V = 10th to 31st)
              6th character        = code for hour of day
                                     (0 - 9 = 00:00 to 09:00)
                                     (A - N = 10:00 to 23:00)
              7th & 8th characters = 2-digit code for minute (00 to 59)
              extension            = .png

           J. Ranada - 06/03/88 - University of Hawaii JIMAR

           Modified 07/23/90 to calculate 3rd character as year - 1980
           mod 10 instead of just year - 1980.

******************************************************************************/
#include <stdio.h>
#include <dir.h>   /* declaration of ffblk structure for DOS directory entry */
#include <string.h> /* str*() */
#include <ctype.h>  /* toupper */
#include <stdlib.h> /* system() */
#include "dbext.h"  /* YMDHMS_TIME_TYPE, USHORT, etc. */
#include "misc.h"   /* split_path() */

USHORT getbits(USHORT source, USHORT start, USHORT nbits);
void ushort_to_date(YMDHMS_TIME_TYPE *file_date, USHORT ushort_date,
   USHORT ushort_time);
char alphanum(USHORT num);

#if PROTOTYPE_ALLOWED
int main(int argc, char *argv[])
#else
int main(argc, argv)
int argc;
char *argv[];
#endif
{
   struct ffblk dir_entry;                            /* DOS directory entry */
   YMDHMS_TIME_TYPE file_date;
   FILE_NAME_TYPE source, dest, source_path, dest_path, filename;
   char response[2], command[80];
   int done, i, iarg, query_user = 1;

   iarg = 1;

   if (argc < 3)
   {
      printf("\nERROR: Invalid no. of arguments");
      printf("\nUSAGE: PINGCOPY [-b] <source-path> <destination-path\\ship-name>");
      printf("\n       where ship-name is a 2-character code");
      printf("\n       Option -b makes it non-interactive");
      printf("\n       for use in batch files.");
      return(-1);
   }

   if (strcmp(argv[iarg], "-b") == 0)
   {
      query_user = 0;
      iarg++;
   }

   strcpy(source_path,argv[iarg]);                             /* path name for source */
   for (i = 0; i < strlen(source_path); i++)
      source_path[i] = (char) toupper((int) source_path[i]);
   if (source_path[i-1] != '\\')
      strcat(source_path,"\\");                                /* must end with '\' */
   strcat(strcpy(source,source_path),"PINGDATA.*");
   iarg++;

   strcpy(dest,argv[iarg]);
   split_path(dest, dest_path, filename);    /* separate path name from ship name */
   filename[2] = '\0';               /* truncate ship name to first 2 chars. */
   strcpy(dest,strcat(dest_path,filename));     /* rejoin path name and ship name */
   for (i = 0; i < strlen(dest); i++)
      dest[i] = (char) toupper((int) dest[i]);

   printf("\nCOPY: %s to %symdhmm.png",source,dest);
   if (query_user)
   {
      printf("\nCONTINUE (y/n)? ");
      scanf("%1s",response);
      if ((response[0] != 'Y') && (response[0] != 'y')) return(-1);
   }

   done = findfirst(source,&dir_entry,0);       /* find first wildcard match */

   while (!done)
   {
      ushort_to_date(&file_date,(USHORT) dir_entry.ff_fdate,
         (USHORT) dir_entry.ff_ftime); /* convert from DOS date & time int's */
      sprintf(filename,"%1u%1x%c%c%02u.png",
         ( (file_date.year - 1980) % 10 ), file_date.month,
         alphanum( (USHORT) file_date.day ),
         alphanum( (USHORT) file_date.hour),
         file_date.minute);
      strcpy(command,"COPY ");
      strcat(strcat(strcat(strcat(strcat(command,source_path),dir_entry.ff_name)," "),
         dest),filename);
      printf("\n%s",command);                    /* display DOS COPY command */
      system(command);                           /* execute DOS COPY command */
      done = findnext(&dir_entry);               /* find next wildcard match */
   }
   return(0);
}

/*---------------------------------------------------------------------------
   FUNCTION: ushort_to_date
             converts DOS int date & time to ymdhms_time structure

             DOS int date:
                bits 15 -  9 = year - 1980;
                bits  8 -  5 = month;
                bits  4 -  0 = day;
             DOS int time:
                bits 15 - 11 = hour;
                bits 10 -  5 = minute;
                bits  4 -  0 = two-second increment;
  ---------------------------------------------------------------------------*/
#if PROTOTYPE_ALLOWED
void ushort_to_date(YMDHMS_TIME_TYPE *file_date,
		    USHORT ushort_date, USHORT ushort_time)
#else
void ushort_to_date(file_date, ushort_date, ushort_time)
YMDHMS_TIME_TYPE *file_date;
USHORT ushort_date, ushort_time;
#endif
{
   file_date->year   = getbits(ushort_date, 15, 7) + 1980;
   file_date->month  = getbits(ushort_date, 8, 4);
   file_date->day    = getbits(ushort_date, 4, 5);
   file_date->hour   = getbits(ushort_time, 15, 5);
   file_date->minute = getbits(ushort_time, 10, 6);
}

/*---------------------------------------------------------------------------
   FUNCTION: getbits
   RETURNS:  right-aligned nbits of source from start position
             (bit position 0 is at right end)
  ---------------------------------------------------------------------------*/
#if PROTOTYPE_ALLOWED
USHORT getbits(USHORT source, USHORT start, USHORT nbits)
#else
USHORT getbits(source, start, nbits)
USHORT source, start, nbits;
#endif
{
   return((source >> (start+1-nbits)) & ~(~0 << nbits));
}

/*---------------------------------------------------------------------------
   FUNCTION: alphanum
   RETURNS:  ASCII code for the one-char equivalent of its numeric argument
  ---------------------------------------------------------------------------*/
#if PROTOTYPE_ALLOWED
char alphanum(USHORT num)
#else
char alphanum(num)
USHORT num;
#endif
{
   if (num < 10) return(num + 48);                       /* ASCII '0' to '9' */
   else return(num + 55);                                /* ASCII 'A' to 'Z' */
}
