#!/usr/bin/perl -w
use strict;
use DB_File;

if (!$ARGV[0] || $ARGV[0] eq "help") {
  print <<EOF;

  Program to fully unpack a .bdb file of chromatograms. Does not translate 
  identifiers. This version adapted from unpack-bdb-reverse-translate.pl
  which did translate names back to facility ids.

  Expects list of .bdb files on standard input

  Usage: <output directory>

EOF
exit(-1);
}

my ($output_dir) = @ARGV;

while(my $bdb_filename = <STDIN>) {
  chomp $bdb_filename;

  print STDERR "Processing file $bdb_filename....";
  my %cgrams = ();
  my $database = tie(%cgrams, 'DB_File', "$bdb_filename", O_RDONLY, 0666, 
		     $DB_HASH)
    or die "Failed to bind databsae file \"$bdb_filename\" ($!)";

  my ($sgn_id, $tigr_id);
  foreach $sgn_id ( keys %cgrams ) {

    open F, "| bzip2 -dc - | gzip -c - > $output_dir/${sgn_id}.gz";
    print F $cgrams{$sgn_id};
    close F
      or print STDERR "Error closing pipe: ($!) ($?)\n";

  }

  undef $database;
  untie %cgrams;

  print STDERR "done\n";
}


