#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

main(int argc, char *argv[])
{
  FILE *infile, *outfile;
  char *inbuf, *outbuf;
  size_t insize, outsize;
  int nbytes;

  //variables for building the output string
  unsigned int fixnum = 0;
  int month, day, year, hour, minute; float sec;
  double lat, lon, northings, eastings,dist;
  double lat_deg, lon_deg; float lat_min, lon_min;

  inbuf = malloc(200); insize = 200;
  outbuf = malloc(200); outsize = 200;
  infile = fopen(argv[1], "r");
  outfile = fopen(argv[2], "w");

  if (!infile) {
    perror("unable to open input file %s\n");
    exit(1);
  }

  if (!outfile) {
    perror("unable to open output file %s\n");
    exit(1);
  }
 
  do {
    memset(inbuf, 0, insize);
    memset(outbuf,0,200);
    nbytes = getline(&inbuf, &insize, infile);
    sscanf(inbuf,"%d,%lf,%lf,%lf,%lf,%lf",
	   &fixnum,&lat,&lon,&eastings,&northings,&dist);
    lat_min = 60.0*modf(lat, &lat_deg); lon_min = 60 * modf(lon, &lon_deg);
    if (nbytes > 0) {
      fprintf(outfile,
	      "NONE,Version=2,%2d-%2d-%2d %2d:%2d:00.0,%c%2d %2.4f,%c%3d %2.4f,"
	      "0.00,"
              "%.2f,%.2f,0.00,0.00,0.00,0.00,0.00,0.00,0.00,NONE,"
              "0.00,0.00,0.00,0.00,0.00,0.00,"
              "0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,,ROV_LOG\n",
	      5,23,18,13,18,
	      (lat>0)?'N':'S',(int)fabs(lat_deg),(float)fabs(lat_min),
	      (lon>0)?'E':'W',(int)fabs(lon_deg),(float)fabs(lon_min),northings,eastings);
    } else {
      break;
    }
  } while (1);

  fclose(infile); fclose(outfile);
  free(inbuf);
  free(outbuf);
}
