#!/usr/bin/ruby
#
# apn - analyze ping numbers
#
PING_POS = 3

# Gotta have a file name argument
#
exit unless (ARGV.length == 1)

# The file's gotta be there
#
f = File.open(ARGV[0])

# Skip all lines up to the start of the data
#
l = nil
while (l = f.gets)
   break if (l.index("idt.data"))
end

# No data? We're done here
#
exit unless (l)

# Get the first line to set the initial conditions
# Ping number is the 4th element
#
l = f.gets.split
prev_ping = l[PING_POS].to_i
start_time = l[0].to_i

# Process the rest of the file
#
while (l = f.gets)
  next if (l.index("];"))  # This indicates the end of the data

  l = l.split
  next if (l.length < PING_POS+1)   # Boundary condition at the end of the data

  # Compare the ping numbers
  #
  this_ping = l[PING_POS].to_i
  if (this_ping != prev_ping + 1)
    mission_time = l[0].to_i - start_time
    puts("Out of sequence ping at time #{mission_time}: #{prev_ping} to #{this_ping}")
  end
  prev_ping = this_ping
end

