#!/usr/bin/perl -w

# Dump Pilot MiniCalc database entries to stdout
#
# Dobrica Pavlinusic <dpavlin@rot13.org> 2000-08-01
#
# http://www.rot13.org/~dpavlin/
#
# PDA::Pilot comes with source of pilot-link, others are from CPAN

# please note that I will not try to develop this conduct anymore.
# it's property program, which I don't want to support!

use strict;
use PDA::Pilot;
use Data::Dumper;
use Getopt::Std;

my %opts;
$opts{d} = "test-pimc.pdb";

# type detection is broken
my @type = ( undef, 'general','decimal','date','time','currency','percent' );

if (! getopts('d:tx',\%opts)) {
	print "Usage: $0 -t -x [-d dbname]\n";
	print "Dump all records from your Pilot MiniCalc database\n";
	print "  -t	shows types\n";
	exit;
}

die "Can't open $opts{d}" if (! -f $opts{d} && -r $opts{d});
my $db = PDA::Pilot::File::open($opts{d});

my $max_x=0;
my $max_y=0;
my (@d,@t);

my $i=1;
while(defined(my $r = $db->getRecord($i++))) {
#	warn Dumper($r);
	if (length($r->{raw}) > 28) {
		my ($col,$row,$len,$type,$data) =
			unpack("nn x14 n n x5 Z*",$r->{raw});
		$data=substr($data,0,$len);
		$max_x=$row if ($row>$max_x);
		$max_y=$col if ($col>$max_y);
		$d[$row][$col]=$data;
		$t[$row][$col]=$type;
print "$data -- $len $type\n";
	}
}

for (my $y=1; $y<=$max_y; $y++) {
	for (my $x=1; $x<=$max_x; $x++) {
		print $type[$t[$x][$y]],"\t" if ($opts{t});
		print $d[$x][$y] if (defined($d[$x][$y]));
		print "\t";
	}
	print "\n";
}

