/*************************************************************************
*_Title pdsvmsrc - copies a CD file with PDS labels to magnetic disk
*       creates an output file with fixed-length record attributes.
*       To be used only on VAX/VMS systems.
*
*_Descr This program used of VAX/VMS systems. This file creates
*       a fixed length record file for PDS labeled files that do
*       not have extended attribute records (XARS) on CDROM media
*       files. 
*
*       pdsvmsrc will copy a file from a CDROM to magnetic disk. The
*       program will look at the RECORD_BYTES keyword in the PDS labels
*       to figure out the record-length of the fixed-length record file.
*
*       How to run the program:
*
*       $pdsvmsrc input output
*
*       Where:
*       input  = path name of input CDROM file 
*       output = path name of output file with proper record attributes
*
* Before running on a VAX/VMS system, create a command with the
* statement PDSVMSRC :== $dev:[dir]PDSVMSRC
* where: dev= CDROM disk device,
*        dir= directory location of software
*
*_HIST August 31, 1993 Eric Eliason, PDS Imaging Node, USGS, Flagstaff Az.
*************************************************************************/
#include <stdio.h>
#include <string.h>
main(narg, argstr)
   int  narg;
   char *argstr[];
{

   static char ifile[81];     /* name of input CDROM file */
   static char ofile[81];     /* name of output file with record attributes */
   static char buf[327680];   /* transfer buffer */
   static char number[12];
   static char mrs[32];
   int i,j,k,recl;            /* work variables */
   int nrecs = 10;	      /* number of records to read per I/O */
   char *ptr;                 /* pointer for buf area */
   char *strfnd();

   FILE *fpi;
   FILE *fpo;
/***********************************************************************
* Better have 3 arguments
***********************************************************************/
   if (narg!=3)
   {
       printf("Create file with record attributes for VAX/VMS systems\n");
       printf("Command line must specify two arguments\n");
       printf("1st argument: Name of input CDROM file\n");
       printf("2nd argument: Name ot output file with record attributes\n\n");
       printf("Set-up command line: pdsvmsrc :== $dev:[dir]PDSVMSRC.EXE\n");
       printf("Usage: pdsvmsrc cdfile outfile\n");
       return;
   }
/***********************************************************************
* extract the input and output file names
***********************************************************************/
   strcpy(ifile,argstr[1]);
   strcpy(ofile,argstr[2]);
   printf("Create file with record attributes for VAX/VMS systems\n");
   printf("Input CDROM file: %s\n",ifile);
   printf("Output file:      %s\n",ofile);

/***********************************************************************
* Open the input
***********************************************************************/
   fpi = NULL;
   if ((fpi = fopen(ifile,"rb")) == NULL)
   {
      printf("*** ERROR *** Can't open file %s\n",ifile);
      return;
   }

/***********************************************************************
* Read the first 512 bytes from the input file, from this we will obtain
* the RECORD_BYTES keyword that will tell us how big to make the 
* record length in the output file
***********************************************************************/
   if (fread(buf,512,1,fpi) <= 0)
   {
      printf("*** ERROR *** Can't read from file");
      return;
   }
   buf[512] = 0;

/************************************************************************
* Find the RECORD_BYTES keyword
************************************************************************/
   if ((ptr = strfnd(buf,"RECORD_BYTES"))==NULL) 
   {
      printf("*** ERROR *** RECORD_BYTES keyword not found in label\n");
      printf("area of the input file\n");
      return;
   }

/***********************************************************************
* Find the '= ' string after the RECORD_BYTES keyword
***********************************************************************/
   if ((ptr = strfnd(ptr,"= "))==NULL) 
   {
      printf("*** ERROR *** Improperly constructed PDS label.\n");
      return;
   }

/***********************************************************************
* extract the value of the RECORD_BYTES keyword value
***********************************************************************/
   while (*ptr < '0' || *ptr > '9') 
    {
      if (*ptr=='\0') 
      {
         printf("*** ERROR *** Improperly constructed PDS label.\n");
         return;
      }
      ptr++;
   }
/***********************************************************************
* 'ptr' now points to the start of the value associated with RECORD_BYTES
* obtain the length of the numeric field
***********************************************************************/
     i = 1;
     while (ptr[i] >= '0' && ptr[i] <= '9') i++;
     strncpy(number,ptr,i);
     recl = atoi(number);

/***********************************************************************
* Open the output file
***********************************************************************/
   mrs[0] = 0;
   strcat(mrs,"mrs=");
   strcat(mrs,number);

   fpo = NULL;
   if ((fpo = fopen(ofile,"wb","rfm=fix","fop=cbt",mrs))== NULL)
   {
      printf("*** ERROR *** Can't open output file %s\n",ifile);
      return;
   }

/***********************************************************************
* Position the input file to the start of the file
***********************************************************************/
   if (fseek(fpi, (long)0, SEEK_SET)!=0)
   {
      printf("*** ERROR *** fseek function failed, can't read input");
      return;
   }

/**************************************************************************
* read/write data until we're all done
**************************************************************************/
   while ((i= fread(buf,recl,nrecs,fpi)) > 0) fwrite(buf,recl,i,fpo);
}
char *strfnd(buf,template)
/***************************************************************************
* Find the character string stored in 'template' in the character string
* stored in 'buf'. A null pointer is returned if the character string
* is not found.
***************************************************************************/
    char *buf;
    char *template;
{
    int       buflen;
    int       temlen;
    int       i;

    buflen = strlen(buf);
    temlen = strlen(template);

    if (temlen > buflen) return NULL;

    for (i=0;i<(buflen-temlen+1);i++,buf++) 
       if (strncmp(buf,template,temlen)==0) return buf;
    return NULL;
}     
