#!/usr/bin/env ruby

require 'RubyLibtrace'

# 1925, Sun 6 Apr 08 (PDT)
# copy-first-n.rb: Copies first n records from one trace to another
# Copyright (C) 2008, Nevil Brownlee, U Auckland | CAIDA | Wand

# ./copy-first-n.rb pcapfile:anon-v4.bpf pcapfile:first-n.bpf 30


in_uri = ARGV[0];  out_uri = ARGV[1]
n_records = ARGV[2]
if n_records.nil? || n_records.to_i <= 0
   exit
else
   n_records = n_records.to_i
   print "copying first #{n_records} records from #{in_uri} to #{out_uri} ...\n"
end

f = Trace.new(in_uri)
f.start

of = OutputTrace.new(out_uri)
of.start_output
print "returned from start_output\n"

n = 0
f.each_packet do |pkt|
   n += 1
   of.write_packet(pkt)

   break if n == n_records
end

of.close_output;  f.close

