DECLARE SUB NoGood ()
DECLARE SUB FindComma (a$, kv!, abort!)
DECLARE SUB NMEAtoHST (a$)
DECLARE SUB ParseTime (a$, kv!, tme$)
DECLARE SUB ParseLL (a$, kv!, LL$, abort!)
COMMON SHARED Altitude, BADnmea, Nocomma, RMC, RMCV, RMCnul, VTG, VTGerr
COMMON SHARED BadTime, DA$
OP$ = "C:\APRS\HSTS\"
DO
Again:
   CLS
   PRINT "NMEA2HST.EXE"
   PRINT
   PRINT "This program converts a raw NMEA text file to an APRSdos.HST formatted"
   PRINT "file for replay within APRSdos using the FILE-REPLAY command.  This "
   PRINT "NMEA2HST conversion was built into APRSdos in version 827 and prior."
   PRINT "It was removed in 828 Jan 99 to make more room."
   PRINT
   PRINT "Only GGA, GLL RMC and VTG sentences are parsed.  These sentences do not"
   PRINT "include the APRS Day-of-the-month."
   PRINT
   INPUT "Enter a two digit DAY for this event"; a$
          a = INT(VAL(a$)): IF a < 1 OR a > 31 THEN CALL NoGood: GOTO Again
          DA$ = LzN$(a, 2)
   INPUT "Enter path and file name for RAW NMEA text file"; FI$
          IF FI$ <> "" THEN CALL NoGood: GOTO Again
          OPEN IF$ FOR INPUT AS #1: IF ERR <> 0 THEN CALL NoGood: GOTO Again
   PRINT "Enter new OUTPUT PATH for the resulting .HST file if not " + OP$
   INPUT a$: IF a$ <> "" THEN OP$ = a$
   IF RIGHT$(OP$, 1) <> "\" THEN OP$ = OP$ + "\"
   
   INPUT "Enter OUTPUT.HST file name"; FO$
   IF INSTR(FO$, ".") = 0 THEN FO$ = FO$ + ".hst"
   IF LEN(FO$) < 5 THEN CALL NoGood: GOTO Again
   INPUT "Proceed (Y/n)"; a$
   IF UCASE$(a$) = "N" THEN CALL NoGood: GOTO Again
   
   OPEN OP$ + OF$ FOR OUTPUT AS #2
   IF ERR <> 0 THEN CALL NoGood: GOTO Again
   PRINT
   PRINT "Processing"
   DO UNTIL EOF(1)
      LINE INPUT #1, a$: CALL NMEAtoHST(a$)
      PRINT #2, a$
      PRINT ".";
   LOOP
   PRINT
   PRINT "Total processed:"; N
LOOP
END

SUB FindComma (a$, kv, abort)
      IF kv > 0 AND kv < 139 THEN
         i = INSTR(kv, a$, ","): IF i >= kv THEN kv = i + 1 ELSE abort = -1
         IF MID$(a$, kv, 1) = "," THEN abort = -1  ' Aborts on null field
      ELSE abort = -1: Nocomma = Nocomma + 1: kv = 1
      END IF

END SUB

SUB NMEAtoHST (a$)

 STATIC CSE$, SPD$
 abort = 0: kv = 7
 IF LEFT$(a$, 6) = "$GPGGA" THEN
    CALL ParseTime(a$, kv, tme$)
    CALL ParseLL(a$, kv, LL$, abort)
    kv = kv + 7
    GGA = GGA + 1: LOCATE 20, 16: PRINT "GGA="; GGA
    IF LEN(a$) > kv THEN
       CALL FindComma(a$, kv, Tbort) 'skip to comma after hdop
       a = kv: CALL FindComma(a$, kv, Tbort) 'Temporary abort symbol
       IF kv - 1 - a > 0 AND Tbort = 0 THEN
          alt = VAL(MID$(a$, a, kv - 1 - a))
          IF MID$(a$, kv, 1) = "M" THEN alt = alt * 3.2804
          'alt$ = "/A=" + LzN$(alt, 6)
          Altitude = Altitude + 1: LOCATE 21, 16: PRINT "ALT="; Altitude
       END IF
    END IF
 END IF
 IF LEFT$(a$, 6) = "$GPVTG" THEN
    CALL FindComma(a$, kv, abort): CSE$ = MID$(a$, kv, 3)
    CALL FindComma(a$, kv, abort) 'skip T
    CALL FindComma(a$, kv, abort) 'skip MagCse
    CALL FindComma(a$, kv, abort) 'skip M
    CALL FindComma(a$, kv, abort): CSpd$ = CSE$ + "/" + MID$(a$, kv, 3)
    IF abort THEN VTGerr = VTGerr + 1 ELSE VTG = VTG + 1
    LOCATE 20, 48: PRINT "VTG="; VTG
    LOCATE 21, 48: PRINT "err="; VTGerr
 END IF
 IF LEFT$(a$, 6) = "$GPRMC" THEN
    CALL ParseTime(a$, kv, tme$)
    CALL FindComma(a$, kv, abort)
    IF MID$(a$, kv, 1) = "V" THEN RMCV = RMCV + 1 ELSE RMC = RMC + 1
    LOCATE 20, 1: PRINT "RMC ="; RMC
    LOCATE 21, 1: PRINT "RMCv="; RMCV
    CALL ParseLL(a$, kv, LL$, abort)
    CALL FindComma(a$, kv, abt): SPD$ = MID$(a$, kv, 3)
    CALL FindComma(a$, kv, abt): CSE$ = MID$(a$, kv, 3)
    IF NOT abt THEN CSpd$ = CSE$ + "/" + SPD$ ELSE RMCnul = RMCnul + 1
    LOCATE 22, 1: PRINT "null="; RMCnul
 END IF
 IF abort = 0 THEN a$ = "          " + tme$ + LL$ + "/" + CSpd$

END SUB

SUB NoGood
 INPUT "Invalid Entry.  Try again.  Hit ENTER to continue"; a$
END SUB

SUB ParseLL (a$, kv, LL$, abort)
   
  CALL FindComma(a$, kv, abort): LL$ = MID$(a$, kv, 7)
  CALL FindComma(a$, kv, abort): LL$ = LL$ + MID$(a$, kv, 1) + "/"
  CALL FindComma(a$, kv, abort): LL$ = LL$ + MID$(a$, kv, 8)
  CALL FindComma(a$, kv, abort): LL$ = LL$ + MID$(a$, kv, 1)
  IF abort THEN BADnmea = BADnmea + 1: LOCATE 20, 64: PRINT "BAD="; BADnmea

END SUB

SUB ParseTime (a$, kv, tme$)
     abort = 0
     i = INSTR(kv, a$, ","): IF i >= kv THEN kv = i + 1 ELSE abort = -1
     'DA$ = LEFT$(ZTG$, 2)
     'IF Zulu = 0 AND val(mid$(a$, kv, 2)) + UTC < 0 THEN 'a new day
      '  DA$ = LzN$(VAL(DA$) + 1, 2)
       ' IF DA$ = "32" THEN DA$ = "01"
     'END IF
     tme$ = LEFT$("@" + DA$ + MID$(a$, kv, 4) + "z  ", 8)'Buoy?


     IF abort THEN
        tme$ = "*******/": BadTime = BadTime + 1
        LOCATE 21, 64: PRINT "BadT="; BadTime
     END IF
END SUB

