#!/usr/bin/perl -w
#
# pdb2zdb v 0.1
#
# Copyright (C) 2006  Ludovic Drolez 
#
#   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 for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.       
#
#
#

use Palm::PDB;
use Palm::Memo;
use Palm::Address;
use Palm::Datebook;
use Data::Dumper;
use Unicode::String;

my $num = 1;

sub escapecsv(*)
{
    $_[0] =~ s/\"/\"\"/g;
    return $_[0];
}  

sub toutf8(*)
{
    return Unicode::String::latin1($_[0]);
}  

sub trim($)
{
    my $string = shift;
    $string =~ s/^\s+//;
    $string =~ s/\s+$//;
    return $string;
}

my $ts = `date +%Y%m%dT%H%M%S`;

if (-f "MemoDB.pdb") {
    $pdb = new Palm::Memo;
    $pdb->Load("MemoDB.pdb");
    
    #print Dumper($pdb);

    mkdir "memo";
    open(OUT, ">memo.csv");
    print OUT "CARDID,CLAS,MEM1,DATE\n";
    
    foreach my $rec (@{$pdb->{records}}) {
	my $data = toutf8($rec->{data});
	my $datae = escapecsv($data);
        my $cat = "";
	$cat = $pdb->{appinfo}{categories}->[$rec->{'category'}]{'name'} if ($rec->{'category'} != 0);
	my ($tit, $tr) = split(/\n/, $data, 2);
	my $date = $ts;
	print OUT "0,\"$cat:$tit\",\"$datae\",$date\n";	
	
	# text file output
	$cat = toutf8($pdb->{appinfo}{categories}->[$rec->{'category'}]{'name'});
	mkdir("memo/$cat");
	open(FO, ">memo/$cat/$tit.txt");
	print FO $data;
	close(FO);

	$num++;
    }
    
    close(OUT);
    $num = 1;
}


if (-f "AddressDB.pdb") {
    $pdb = new Palm::Address;
    $pdb->Load("AddressDB.pdb");
    
    #print Dumper($pdb);

    #CARDID,CATEGORY,FULL,TITL,LNME,FNME,MNME,SUFX,FLAS,LNPR,FNPR,NAPR,CPNY,
    #CPPR,SCTN,PSTN,TEL2,FAX2,CPS2,BSTA,BCTY,BSTR,BZIP,BCTR,BWEB,OFCE,PRFS,
    #ASST,MNGR,BPGR,CPS1,TEL1,FAX1,HSTA,HCTY,HSTR,HZIP,HCTR,HWEB,DMAL,MAL1,
    #SPUS,GNDR,BRTH,ANIV,NCNM,CLDR,MEM1,GRPS
    open(OUT, ">address.csv");
    print OUT "CARDID,CATEGORY,FULL,LNME,FNME,CPNY,TEL2,FAX2,".
	"CPS2,BGPR,CPS1,TEL1,DMAL,BSTR,BCTY,BZIP,".
	"BCTR,TITL,MEM1\n";
    
    foreach my $rec (@{$pdb->{records}}) {
        my $cat = "";
	$cat = $pdb->{appinfo}{categories}->[$rec->{'category'}]{'name'} if ($rec->{'category'} != 0);
	my $date = $ts;

	my $lnme = toutf8($rec->{fields}{name});
	my $fnme = toutf8($rec->{fields}{firstName});
	my $cpny = toutf8($rec->{fields}{company});

	my $bstr = toutf8($rec->{fields}{address});
	my $bcty = toutf8($rec->{fields}{city});
	my $bsta = toutf8($rec->{fields}{state});
	my $bzip = toutf8($rec->{fields}{zipcode});
	my $bctr = toutf8($rec->{fields}{country});
	my $titl = toutf8($rec->{fields}{title});
	my $mem1 = toutf8($rec->{fields}{note});
	my $custom = trim(toutf8($rec->{fields}{custom1})." ".toutf8($rec->{fields}{custom2})." ".toutf8($rec->{fields}{custom3})." ".toutf8($rec->{fields}{custom4}));

	if ($custom ne "") { $mem1 .= "custom: $custom"; }
	
	my @phones;
	for(my $i=1; $i <= 8; $i++) {
	    my $p = $rec->{fields}{phone."$i"};
	    if ($p ne "") {
		# phone labels:
		# "Work", "Home", "Fax", "Other", "E-mail", "Main", "Pager" and "Mobile"
		my $pl = $rec->{phoneLabel}{phone."$i"};
		
		if (!defined ($phones[$pl])) { $phones[$pl] = $p; }
		else { $phones[$pl] .= " ".$p; }
	    }
	}

	print OUT "0,$cat,\"$lnme $fnme\",\"$lnme\",\"$fnme\",\"$cpny\",\"$phones[0]\",\"$phones[2]\",".
	    "\"$phones[7]\",\"$phones[6]\",\"$phones[3]\",\"$phones[1]\",\"$phones[4]\",\"$bstr\",\"$bcty\",\"$bzip\",".
	    "\"$bctr\",\"$titl\",\"$mem1\"\n";
	#print Dumper($rec);
	$num++;
    }
    
    close(OUT);
    $num = 1;
}


if (-f "DatebookDB.pdb") {
    $pdb = new Palm::Datebook;
    $pdb->Load("DatebookDB.pdb");

    open(OUT, ">datebook.csv");

    foreach my $rec (@{$pdb->{records}}) {
        my $cat = "";
	$cat = $pdb->{appinfo}{categories}->[$rec->{'category'}]{'name'} if ($rec->{'category'} != 0);
	my $date = $ts;

	print Dumper($rec);

	my $lnme = toutf8($rec->{fields}{name});
	my $fnme = toutf8($rec->{fields}{firstName});
	my $cpny = toutf8($rec->{fields}{company});

    }    
    close(OUT);
    
}