#!/usr/bin/perl

#  STDIN: 
#
# 1186813364	OH2SIX-12>APRS,OH2AUE,I:"TLEM: Akku 11.76V Akku  0.26A Temp1 +29C Temp2 +28C TRANS 0101 ATV   0016 ALC   0000 AGC   0.85V 
# 1186813405	OH2SIX-12>APRS,OH2AUE,I:"STAT: Arm   OFF Fire  OFF ATV   OFF Cam 2 ATT   OFF HF    ON  SSPA  OFF PassB OFF 
# 1186813498	OH2SIX-12>APRS,OH2AUE,I:!6120.98N/02502.45EO/A=000341 062456h
# 1186813498	OH2SIX-11>APRS,WIDE3-3*,OH2AUE,I:!6120.98N/02502.45EO062456h/A=00104m
# 1186826989	OH2SIX-12>OH2KKU:!6134.49N/02516.02EO 111011z 
# 1186829460	OH2SIX-11>SYNTHETIC:!6139.56N/02518.53EO1050006h/A=000000 LANDING!
#

# Output:
#
# 1)  UNIX time_t
# 2)  latitude (northing)
# 3)  longitude (easting)
# 4)  altitude (meters)
#
# 5)  hspeed m/s
# 6)  vspeed m/s
#
# 7)  telem akku volt
# 8)  telem akku ampers
# 9)  telem temp1 degrees C
# 10)  telem temp2 degreec C
# 11)  telem TRANS code
# 12) telem ATV code
# 13) telem ALC code
# 14) telem AGV volt
#
# 15) stat ARM 1=ON
# 16) stat Fire 1=ON
# 17) stat ATV 1=ON
# 18) stat CAM 1/2
# 19) stat ATT 1=ON
# 20) stat HF 1=ON
# 21) stat SSPA 1=ON
# 22) stat PassB 1=ON
#

$t0 = 1186815663;

use POSIX;

$t1 = undef;
$t2 = undef;
$h1 = undef;
$h2 = undef;

while (<>) {

    if (m/^(\d*)\tOH2SIX-12>APRS[^:]*:"TLEM: Akku\s+([^V]+)V\s+Akku\s+([^A]+)A\s+Temp1\s+([^C]+)C\s+Temp2\s+([^C]+)C\s+TRANS\s+(\d+)\s+ATV\s+(\d+)\s+ALC\s+(\d+)\s+AGC\s+([^V]+)V/o) {
	printf("%s,,,,,,".
	       "%s,%s,%s,%s,%s,%s,%s,%s,".
	       ",,,,,,,".
	       "\n",
	       $1 - $t0,
	       $2, $3, $4, $5, $6, $7, $8, $9);
	next;
    }
    if (m/^(\d*)\tOH2SIX-12>APRS[^:]*:"STAT: Arm\s+(\S+)\s+Fire\s+(\S+)\s+ATV\s+(\S+)\s+Cam\s+(\S+)\s+ATT\s+(\S+)\s+HF\s+(\S+)\s+SSPA\s+(\S+)\s+PassB\s+(\S+)\s*$/o) {
	printf("%s,,,,,,".
	       ",,,,,,,,".
	       "%s,%s,%s,%s,%s,%s,%s,%s".
	       "\n",
	       $1 - $t0,
	       oo($2), oo($3), oo($4), $5, oo($6), oo($7), oo($8), oo($9));
	next;
    }


    #
    # OH2SIX-12>APRS,OH2AUE,I:!6120.98N/02502.45EO/A=000341 062456h
    #

    if (m/^(\d*)\tOH2SIX-12>APRS[^:]*:\!(\d\d)(\d\d\.\d\d)N\/(\d\d\d)(\d\d\.\d\d)EO\/A=(\d+) /o) {
	$h2 = 0.3048 * $6;
	$t2 = $1 - $t0;
	$dh = 0;
	$dv = 0;
	if (defined $t1) {
	    $dt = $t2 - $t1;
	    $dt = 1 if ($dt < 1);
	    $dh = ($h2 - $h1) / $dt;
	}
	$t1 = $t2;
	$h1 = $h2;
	printf("%s,%.5f,%.5f,%d,%.2f,%.2f,".
	       ",,,,,,,,".
	       ",,,,,,,".
	       "\n",
	       $t2,
	       (POSIX::strtol($2,10)) + (0+$3)/60.0,
	       (POSIX::strtol($4,10)) + (0+$5)/60.0,
	       $h2, $dh, $dv);

	next;
    }

    #
    # OH2SIX-11>APRS,WIDE3-3*,OH2AUE,I:!6120.98N/02502.45EO062456h/A=00104m
    #

    if (m{^(\d*)\tOH2SIX-11>APRS[^:]*:\!(\d\d)(\d\d\.\d\d)N/(\d\d\d)(\d\d\.\d\d)EO[^/]*/A=([^mM,]+)m}o) {
	$h2 = $6;
	$t2 = $1 - $t0;
	$dh = 0;
	$dv = 0;
	if (defined $t1) {
	    $dt = $t2 - $t1;
	    $dt = 1 if ($dt < 1);
	    $dh = ($h2 - $h1) / $dt;
	}
	$t1 = $t2;
	$h1 = $h2;
	printf("%s,%.5f,%.5f,%d,%.2f,%.2f,".
	       ",,,,,,,,".
	       ",,,,,,,".
	       "\n",
	       $t2,
	       (POSIX::strtol($2,10)) + (0+$3)/60.0,
	       (POSIX::strtol($4,10)) + (0+$5)/60.0,
	       $h2, $dh, $dv);

	next;
    }

    #
    # OH2SIX-12>OH2KKU:!6134.49N/02516.02EO 111011z 
    # OH2SIX-12>OH2KKU:!6136.47N/02518.01EO 111023z 5247m
    #
    
    #
    # OH2SIX-11>SYNTHETIC:!6139.56N/02518.53EO1050006h/A=00000m LANDING!
    #
    if (m{^(\d*)\tOH2SIX-11>SYNTH[^:]*:\!(\d\d)(\d\d\.\d\d)N/(\d\d\d)(\d\d\.\d\d)EO[^/]*/A=(\d+)m }o) {
	$h2 = $6;
	$t2 = $1 - $t0;
	$dh = 0;
	$dv = 0;
	if (defined $t1) {
	    $dt = $t2 - $t1;
	    $dt = 1 if ($dt < 1);
	    $dh = ($h2 - $h1) / $dt;
	}
	$t1 = $t2;
	$h1 = $h2;
	printf("%s,%.5f,%.5f,%d,%.2f,%.2f,".
	       ",,,,,,,,".
	       ",,,,,,,".
	       "\n",
	       $t2,
	       (POSIX::strtol($2,10)) + (0+$3)/60.0,
	       (POSIX::strtol($4,10)) + (0+$5)/60.0,
	       $h2, $dh, $dv);

	next;
    }



}


exit 0;

sub oo {
    local ($what) = @_;

    return ($what eq 'ON') ? 1 : 0;
}
