#!/usr/bin/perl

# convert-dumps version 0.1

# Copyright (C) 2008, ashley willis <barry@venamous.net>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.
#
# See the GNU General Public License in the COPYING file at the
# root directory of this project for more details.

# converts "btool -n -d <DBNAME>" output, or .dump files from "./dump-simple",
# into individual binary files.

# ($_ = pack "H*", "414243")

foreach $file (@ARGV) {
  open(IN, "$file");
  $record = 0;
  while (<IN>) {
    if (/^Raw record dump for record: /) {
      ($record) and close (OUT);
      $record++;
      open(OUT, ">$file-$record");
    }
    if (/^    0/) {
      s/^    0[\da-f]{7}: //;
      s/  .*\n//;
      s/ //g;
      $out = pack("H*", $_);
      print OUT $out;
    }
  }
  close(OUT);
  close(IN);
}
