#! /usr/bin/env ruby
require 'pocolog'

ARGV.each do |path|
    file = Pocolog::Logfiles.open(path)
    current_pos = 0
    begin
        while !file.eof?
            current_pos = file.rio.tell
            if block_info  = file.read_one_block
                if block_info.type == Pocolog::DATA_BLOCK
                    file.validate_data_block
                end
            end
        end
    rescue Pocolog::InvalidFile => e
        puts "#{path}: broken at position #{current_pos}"
        puts "  copying the current file as #{path}.broken"
        FileUtils.cp path, "#{path}.broken"
        puts "  truncating the existing file"
        File.open(path, 'a') { |io| io.truncate(current_pos) }
    end
end
