#! /usr/bin/perl
#
#     palm_lsaddr - Palm address database helper utility for lbdb
#     Copyright (C) 2000 Dave Pearson <davep@davep.org>
#     Patch for windows-1251 <-> koi8-r recoding by Andrey Brindeew <abr@altlinux.ru>
#
#     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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.

use 5.8.0;
use Palm::PDB;
use Palm::Address;
use I18N::Langinfo qw(langinfo CODESET);

if ( $#ARGV > -1 )
{
    my $pdb = new Palm::PDB;
	my $pdb_charset = $ARGV[ 1 ] || 'LATIN1';
	my $usr_charset = langinfo(CODESET());

    if ( $pdb )
    {
        $pdb->Load( $ARGV[ 0 ] );

        my $record;

        for ( $i = 0, $record = $pdb->{records}[ $i ]; $record; $i++, $record = $pdb->{records}[ $i ] )
        {
            my $name = $record->{fields}{firstName} . " " . $record->{fields}{name};

            # Remove leading and trailing whitespace.
            $name =~ s/\s+$//;
            $name =~ s/^\s+//;

            # If the name is empty, use the company name instead.
            $name = $record->{fields}{company} unless ( length( $name ) > 0 );

            if ( length( $name ) > 0 )
            {
                my $phone;

                # Any of the phone fields could contain an email address.
                foreach $phone ( $record->{fields}{phone1},
                                 $record->{fields}{phone2},
                                 $record->{fields}{phone3},
                                 $record->{fields}{phone4},
                                 $record->{fields}{phone5} )
                {
                    # A phone field can also contain multiple lines.
                    foreach $contact ( split( /\n/, $phone ) )
                    {
                        # Does it look like an email address?
                        # (simply looking for an "@" is a bit lame but it's a
                        # start, feel free to offer a better method of doing
                        # the test)
                        if ( $contact =~ /\@/ )
                        {
                            print recode_data("$contact\t$name\t(Palm)\n",$pdb_charset,$usr_charset);
                        }
                    }
                }
            }
        }
    }
}

sub win2koi {
	my (@result) = (@_);
	foreach (@result) {
		if (ref) {
			if		(ref eq 'ARRAY') { $_ = [ win2koi(@{$_}) ] }
			elsif	((ref eq 'SCALAR') or (ref eq 'REF')) { $_ = \scalar(win2koi(${$_})) }
		}
		elsif (defined $_) {
			tr/\xFE\xE0\xE1\xF6\xE4\xE5\xF4\xE3\xF5\xE8-\xEF\xFF\xF0-\xF3\xE6\xE2\xFC\xFB\xE7\xF8\xFD\xF9\xF7\xFA\xDE\xC0\xC1\xD6\xC4\xC5\xD4\xC3\xD5\xC8-\xCF\xDF\xD0-\xD3\xC6\xC2\xDC\xDB\xC7\xD8\xDD\xD9\xD7\xDA/\xC0-\xFF/;
			tr/\xB8\xA8/\xA3\xB3/;
		}
	}
	return wantarray ? @result : $result[0];
}
sub koi2win {
	my (@result) = (@_);
	foreach (@result) {
		if (ref) {
			if		(ref eq 'ARRAY') { $_ = [ koi2win(@{$_}) ] }
			elsif	((ref eq 'SCALAR') or (ref eq 'REF')) { $_ = \scalar(koi2win(${$_})) }
		}
		elsif (defined $_) {
			tr/\xC0-\xFF/\xFE\xE0\xE1\xF6\xE4\xE5\xF4\xE3\xF5\xE8-\xEF\xFF\xF0-\xF3\xE6\xE2\xFC\xFB\xE7\xF8\xFD\xF9\xF7\xFA\xDE\xC0\xC1\xD6\xC4\xC5\xD4\xC3\xD5\xC8-\xCF\xDF\xD0-\xD3\xC6\xC2\xDC\xDB\xC7\xD8\xDD\xD9\xD7\xDA/;
			tr/\xA3\xB3/\xB8\xA8/;
		}
	}
	return wantarray ? @result : $result[0];
}

# Parameters:
#  - data for recoding
#  - .pdb charset
#  - user charset (from locale settings)
sub recode_data {
	my ($data, $pdb_ch, $user_ch) = @_;
	return $data if ($pdb_ch eq $user_ch);
	return $data if ($pdb_ch eq 'LATIN1'); # Need no conversion
	return $data if ($pdb_ch ne 'CP1251' and $pdb_ch ne 'KOI8-R'); # Sorry, only russian conversions available at this time
	return(win2koi($data)) if ($pdb_ch eq 'CP1251' and $user_ch eq 'KOI8-R');
	return(koi2win($data)) if ($pdb_ch eq 'KOI8-R' and $user_ch eq 'CP1251');
	return $data; # if all above fails
}
