#=======================================================================
# Newtonlink   - transfer data between a Apple Newton Message Pad and
#                Unix applications
#
# Copyright (C) 1996-1998    The Newtonlink Developers
#                            (newtonlink@newton.bawue.de)
# 
# 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.
#=======================================================================

#---------------------------------------------------------------------
# Get4KDEAddressbook
#---------------------------------------------------------------------

# $Log: Get4KDEAddressbook.pl,v $
# Revision 1.2  1999/11/17 16:00:46  sdteffen
# Added support for more than one Email address per person
#
# Revision 1.1  1999/04/18 16:26:07  reinhold
# Beta 5 : neue Option kab
#
#

sub Get4KDEAddressbook {

    # Get actual Newton cards
    &GetCards;

    # save old KDE Addressbook file
    if (-e "$KDEAddressbookFile") {
	rename ("$KDEAddressbookFile", "$KDEAddressbookFile.bak");
    }

    # copy template to new file
    #if (-e "$KDEAddressbookFile.template") {
    #	copy ("$KDEAddressbookFile.template", "$KDEAddressbookFile");
    #}
    

    # Open Newtonlink cards file
    open (NLCARDFILE, "$NLCardsFile")  || die "Can't open File : $!\n";

    # Open KDEAddressbook file for appending
    open (KDEAFILE, "> $KDEAddressbookFile")  || die "Can't open File : $!\n";

    # print to stdout
    printf "Writing KDE addressbook file $KDEAddressbookFile ...\n";

    # write Entry header
    printf KDEAFILE "# [File created by newtonlink 1.30 and the KDE 2.2.2 version of Get4KDEAddressbook.pl\n";
    printf KDEAFILE "[config]\n  # key-value-pairs:\n  LastCurrentKey=\"1\"\n";
    printf KDEAFILE "  LastViewMode=\"2\"\n  ShowEntryList=\"true\"\n";
    printf KDEAFILE "  user_1=\"(User field 1)\"\n  user_2=\"(User field 2)\"\n";
    printf KDEAFILE "  user_3=\"(User field 3)\"\n  user_4=\"(User field 4)\"\n";
    printf KDEAFILE "  user_headline=\"(User fields)\"\n[END config]\n";
    printf KDEAFILE "[entries]\n";
    printf KDEAFILE "  # subsections:\n";
    
    $KABEntry = 0;
    
    while (<NLCARDFILE>) 
    {

      ($Cardclass, $CardlastName, $CardfirstName, $Cardhonorific, $Cardaddress, 
       $Cardcountry, $Cardpostal_code, $Cardcity, $Cardbday, $CardhomePhone,
       $CardworkPhone, $CardfaxPhone, $Cardemail, 
       $Cardcompany) = split(/;/, $_, 20);    
        
       	# leave only trailing email addresses
       	s/([^;]*;){14}//;
       
       	# make entries for all the email addresses
       	while ($_ =~ /\@/)
       	{
	        # get next address
	        ($NextEmail) = split(/;/, $_, 2);
	        s/[^;]*;//;	            
	        # add to Email String	         
	        $Cardemail = $Cardemail."\\e".$NextEmail;
	}	

      @CardBDayFields = split(/\//,$Cardbday);
      $NewCardBDay = "19" . @CardBDayFields[2] . ", " . @CardBDayFields[0] . ", " . @CardBDayFields[1];

      if ( ($Cardclass eq "company") || ($Cardclass eq "person") || ($Cardclass eq "owner") ) 
      {
      
	# create new kab entry
	$KABEntry++;
	printf KDEAFILE "  [$KABEntry]\n";
	printf KDEAFILE "  # subsections\n";
	
	if ($Cardemail ne "")
	{
	  $EmailStr = $Cardemail."\\e";
	} else {
	  $EmailStr = "";
	}
	
	$FormattedName = "";
	$PhoneNumber = "";
	if ($Cardclass eq "company")
	{
	  $FormattedName = "Company: ".$CardlastName;
	  $PhoneNumber = "0\\e".$CardworkPhone."\\e";
	} else {
	  $PhoneNumber = "0\\e".$CardhomePhone."\\e";
          $FormattedName = "Person: ".$CardlastName;
	  if ($CardfirstName ne "")
	  {
	    $FormattedName .= ", ".$CardfirstName;
	  }
	}

	if ($CardfaxPhone ne "")
	  {
	    $PhoneNumber .= "2\\e".$CardfaxPhone."\\e"
	  }

	printf KDEAFILE "    [addresses]\n";
	printf KDEAFILE "      # subsections:\n";
	printf KDEAFILE "      [1]\n";
	printf KDEAFILE "        # key-value-pairs:\n";
	printf KDEAFILE "        address=\"$Cardaddress\"\n";
	printf KDEAFILE "        country=\"$Cardcountry\"\n";
	printf KDEAFILE "        deliverylabel=\"\"\n";
	printf KDEAFILE "        headline=\"\"\n";
	printf KDEAFILE "        org=\"$Cardcompany\"\n";
	printf KDEAFILE "        orgsubunit=\"\"\n";
	printf KDEAFILE "        orgunit=\"\"\n";
	printf KDEAFILE "        position=\"\"\n";
	printf KDEAFILE "        state=\"\"\n";
	printf KDEAFILE "        town=\"$Cardcity\"\n";
	printf KDEAFILE "        zip=\"$Cardpostal_code\"\n";
	printf KDEAFILE "      [END 1]\n";
	printf KDEAFILE "    [END addresses]\n";
	printf KDEAFILE "    # key-value-pairs:\n";
	printf KDEAFILE "    URLs=\"\"\n";
	# printf KDEAFILE "    birthday=\"$Cardbday\"\n";
	printf KDEAFILE "    birthday=\"$NewCardBDay\"\n";
	printf KDEAFILE "    comment=\"$Cardclass\"\n";
	printf KDEAFILE "    custom=\"\"\n";
	printf KDEAFILE "    emails=\"$EmailStr\"\n";
	printf KDEAFILE "    firstname=\"$CardfirstName\"\n";
	printf KDEAFILE "    fn=\"$FormattedName\"\n";
	printf KDEAFILE "    keywords=\"\"\n";
	printf KDEAFILE "    lastname=\"$CardlastName\"\n";
	printf KDEAFILE "    middlename=\"\"\n";
	printf KDEAFILE "    nameprefix=\"$Cardhonorific\"\n";
	printf KDEAFILE "    rank=\"\"\n";
	printf KDEAFILE "    talk=\"\"\n";
	printf KDEAFILE "    telephone=\"0\\e$PhoneNumber\\e\"\n";
	printf KDEAFILE "    title=\"\"\n";
	printf KDEAFILE "    user1=\"\"\n";
	printf KDEAFILE "    user2=\"\"\n";
	printf KDEAFILE "    user3=\"\"\n";
	printf KDEAFILE "    user4=\"\"\n";
	printf KDEAFILE "  [END $KABEntry]\n";
      }
      
    }

    # write addressbook footer
    printf KDEAFILE "[END entries]\n";

    # close files
    close (KDEAFILE);
    close (NLCARDFILE);
}

1;