## -*-Tcl-*-
 # ###################################################################
 # 
 #  FILE: "addressbook2tcl.tcl"
 #                                    created: 2007-01-11 16:32:54 
 #                                last update: 2007-01-12 13:48:20 
 #  Author: Frédéric Boulanger
 #  E-mail: Frederic.Boulanger@supelec.fr
 #    mail: Supélec - Département Informatique
 #          3 rue Joliot-Curie, 91192 Gif-sur-Yvette, France
 #     www: http://wwwsi.supelec.fr/fb/
 #  
 #  Description: 
 # 
 #  History
 # 
 #  modified   by  rev reason
 #  ---------- --- --- -----------
 #  2007-01-11 FBO 1.0 original
 # ###################################################################
 ##
# set suppressedFields [list \
#  ABDate \
#  ABPersonFlags \
#  ABRelatedNames \
#  AIMInstant \
#  Creation \
#  FirstPhonetic \
#  ICQInstant \
#  JabberInstant \
#  LastPhonetic \
#  MSNInstant \
#  MiddlePhonetic \
#  Modification \
#  UID \
#  YahooInstant \
# ]
proc exportAddressBook {} {
  set suppressedFields [list UID]

#   set expfilename [putfile "Save exported addresses to" "AddressBook-[ISOTime::ISODate].tcl"]
#   if {[string length $expfilename] == 0} {
#     error "cancel"
#   }

  set expfilename "AddressBook-[clock format [clock seconds] -format "%Y-%m-%d"].tcl"

  set expfile [open $expfilename "w"]
  fconfigure $expfile -encoding utf-8 -translation lf
  puts $expfile "package require addressbook"
  
  package require addressbook
  set idlist [addressbook persons -ids]
  set grplist [addressbook groups -ids]
  
  foreach id $grplist {
    set data [addressbook record $id]
    set name ""
    foreach field $data {
      if {[lindex $field 0] == "GroupName"} {
        set name "[lindex $field 1]"
        break
      }
    }
    puts $expfile "set gid \[addressbook create group \"$name\"\]"
    puts $expfile "set map($id) \$gid"
    foreach field $data {
      set fieldname [lindex $field 0]
      set fieldvalue [lindex $field 1]
      if {[lsearch -exact $suppressedFields $fieldname] >= 0} {
        continue
      }
      puts $expfile "addressbook set \$gid  $fieldname \{$fieldvalue\}"
    }
    puts $expfile ""
  }
  
  foreach id $idlist {
    set data [addressbook record $id]
    set name ""
    foreach field $data {
      if {[lindex $field 0] == "Last"} {
        set name "[lindex $field 1]"
        break
      }
    }
    puts $expfile "set uid \[addressbook create person \"$name\"\]"
    puts $expfile "set map($id) \$uid"
    foreach field $data {
      set fieldname [lindex $field 0]
      set fieldvalue [lindex $field 1]
      if {[lsearch -exact $suppressedFields $fieldname] >= 0} {
        continue
      }
      puts $expfile "addressbook set \$uid  $fieldname \{$fieldvalue\}"
    }
    puts $expfile ""
  }
  
  foreach id [concat $grplist $idlist] {
    set parents [addressbook parents -ids $id]
    foreach p $parents {
      if {[string length $p] == 0} continue
      puts $expfile "addressbook add \$map($p) \$map($id)"
    }
  }
  
  puts $expfile "addressbook save"
  close $expfile
}

exportAddressBook
