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

           This program is used to rename PINGDATA.* files in the source
           directory 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

           If the destination path is not the same as the source path,
           then the renaming also causes the PINGDATA.* files to be
           moved to the destination.  However, the drive specifier,
           if provided, must be the same for both source and destination.

           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... functions */
#include <ctype.h>                                       /* toupper function */
#include "dbext.h"                         /* YMDHMS_TIME_TYPE, USHORT, etc. */
#include "misc.h"                               /* CODAS split_path function */

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];
   int done,i,j, iarg = 1, query_user= 1;

   if (argc < 3)
   {
      puts("\nERROR: Invalid no. of arguments");
      puts("\nUSAGE: PING_REN <source-path> <destination-path\\ship-name>");
      puts("\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 (j = 0; j < strlen(source_path); j++)
      source_path[j] = (char) (toupper((int) source_path[j]));
   if (source_path[j-1] != '\\')
   {
      strcat(source_path,"\\");                                /* must end with '\' */
      j++;
   }
   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("\nRENAME: %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);
   }

   if ((done = findfirst(source,&dir_entry,0)) == 1)  /* find wildcard match */
      printf("\nERROR: Path or file %s not found",source);

   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(dest+i,filename);                              /* new file name */
      strcpy(source+j,dir_entry.ff_name);                   /* old file name */
      printf("\nRENAME %s %s",source,dest);        /* display RENAME command */
      if (rename(source,dest))
      {
         done = 1;                                /* error in RENAME command */
         printf("\nERROR: Renaming %s to %s",source,dest);
      }
      else
         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' */
}
