#!/usr/bin/tclsh

#
# pui-tabtoaddress
#
# This is a sample script using tclpui to create an AddressDB.pui
# with data from a different format-- in this case a tab-delimited
# file I output from FilemakerPro on a Macintosh.
#
# If the file exists, it is opened (for writing) otherwise, a NEW
# database is created.
#
  global rec
  global puidb

  load tclpui.so

  if {[llength $argv] != 2} {
    puts "Usage: tabtoaddress tab-file AddressDB.pui"
    puts "    Inserts addresses from tab-file into AddressDB.pui."
    puts "    tab-file is a tab-delimited file containing certain records."
    puts "    Modify script to deal with your own database type."
    exit -1
  }

  # Mac file character conversion. Should be more extensive.
  proc zapem { string } {
    regsub -all \v $string \n x
    return $x
  }

  set filename [lindex $argv 1]
  if [file exists $filename] {
    set puidb [puiOpen $filename w]
  } {
    set puidb [puiNew $filename AddressDB]
  }

  # Open file of tab-separated data (from fmaker pro)
  set fp [open [lindex $argv 0] r]

  while {[gets $fp line] != -1} {
    # Split at tabs
    set list [split $line "	"]

    # Extract filemaker pro fields
    set lastname  [zapem [lindex $list 0]]
    set firstname [zapem [lindex $list 1]]
    set title     [zapem [lindex $list 2]]
    set company   [zapem [lindex $list 3]]
    set work      [zapem [lindex $list 4]]
    set home      [zapem [lindex $list 5]]
    set fax       [zapem [lindex $list 6]]
    set other1    [zapem [lindex $list 7]]
    set other2    [zapem [lindex $list 8]]
    set email     [zapem [lindex $list 9]]
    set address   [zapem [lindex $list 10]]
    set city      [zapem [lindex $list 11]]
    set state     [zapem [lindex $list 12]]
    set zip       [zapem [lindex $list 13]]
    set country   [zapem [lindex $list 14]]
    set birthday  [zapem [lindex $list 15]]
    set note      [zapem [lindex $list 16]]

    # Set pilot fields based on filemaker data
    set rec(lastname)  $lastname
    set rec(firstname) $firstname
    set rec(title)     $title
    set rec(company)   $company
    set rec(address)   $address
    set rec(city)      $city
    set rec(state)     $state
    set rec(zip)       $zip
    set rec(country)   $country
    set rec(custom1)   $birthday
    set rec(custom2)   ""
    set rec(custom3)   ""
    set rec(custom4)   ""
    set rec(note)      $note

    set rec(whichphone)  1
    set rec(phonelabel1) 0
    set rec(phonelabel2) 1
    set rec(phonelabel3) 2
    set rec(phonelabel4) 3
    set rec(phonelabel5) 4

    set rec(phone1) $work
    set rec(phone2) $home
    set rec(phone3) $fax
    set rec(phone4) $other1
    set rec(phone5) $email
    if {"$other2" != ""} {
      if {"$email" != ""} {
        puts "Trouble-- both email and other2 on $lastname, $firstname"
      } {
        set rec(phone5) $other2
        set rec(phonelabel5) 3
      }
    }

    set rec(category) 0
    set rec(attribute) 64

    # Write NEW record into database
    set newid [puiWrite $puidb 0 rec]
  }
  puiClose $puidb

