--[[
=============================================
vcf.lua by Cancan

Functions to read and write a vcf type file

Email: cancangm (at) gmail (dot) com
=============================================
]]

function fromCSV(s, ttab, indice)
  s = s .. ';'        -- ending char
  local fieldstart = 1
  repeat
    local nexti = string.find(s, ';', fieldstart)
    ttab[indice] = string.sub(s, fieldstart, nexti-1)
    indice = indice + 1
    fieldstart = nexti + 1
  until fieldstart > string.len(s)
end

function saveVCFFile(filename, table)
  file = io.open(filename,"w")
  if file then
    file:write("BEGIN:VCARD\n")
    file:write("VERSION:2.1\n")
    file:write("N:" .. table[2] .. ";" .. table[3] .. ";")
    file:write(table[4] .. ";" .. table[5] .. "\n")
    file:write("FN:" .. table[1] .. "\n")
    file:write("NICKNAME:" .. table[6] .. "\n")
    file:write("TEL;HOME;VOICE:" .. table[7] .. "\n")
    file:write("TEL;HOME;FAX:" .. table[8] .. "\n")
    file:write("TEL;CELL;VOICE:" .. table[9] .. "\n")
    file:write("TEL;WORK;VOICE:" .. table[10] .. "\n")
    file:write("TEL;ADR;HOME:" .. table[12] .. ";" .. table[13] .. ";")
    file:write(table[11] .. ";" .. table[14] .. ";" .. table[15] .. ";")
    file:write(table[16] .. ";" .. table[17] .. "\n")
    file:write("EMAIL;INTERNET:" .. table[18] .. "\n")
    file:write("END:VCARD\n")
    file:close()
  end
end

function loadVCFFile(filename)
  file = io.open(filename, "r")
  local ttab = {}
  local index = 0
  local indey = 0
  local prefix = 0
  local pref = 0
  local line = ""
  local key = ""
  local value = ""
  local swap = ""
  for index=1, fieldmax do
    table.insert(ttab, "")
  end

  if file then
    for line in file:lines() do
      index = string.find(line, "\r")
      if index then
         line = string.sub(line, 1, index - 1)
      end
      index = string.find(line, ":")
      if index then
        key = string.sub(line, 1, index - 1)
        value = string.sub(line, index + 1)
        if key == "N" then
          fromCSV(value, ttab, 2)
        else
          pref = string.find(key, "PREF")
          if key == "FN" then ttab[1] = value 
          else
            if key == "NICKNAME" then ttab[6] = value
            else
              prefix = string.find(key, "ADR")
              if prefix then 
                prefix = string.find(key, "HOME")
                if prefix or pref or ttab[11] == "" then
                  fromCSV(value, ttab, 11)
                  swap = ttab[11]
                  ttab[11] = ttab[13]  
                  ttab[13] = ttab[12]
                  ttab[12] = swap
                end
              else
                prefix = string.find(key, "EMAIL")
                if prefix then 
                  prefix = string.find(key, "HOME")
                  if prefix or pref or ttab[18] == "" then
                    ttab[18] = value
                  end
                else
                  prefix = string.find(key, "TEL")
                  if prefix then
                    indey = 0
                    prefix = string.find(key, "HOME")
                    if prefix then indey = 7 end
                    prefix = string.find(key, "WORK")
                    if prefix then indey = 10 end
                    prefix = string.find(key, "CELL")
                    if prefix and (indey == 0 or indey == 7) then indey = 9 end
                    prefix = string.find(key, "FAX")
                    if prefix and (indey == 0 or indey == 7) then indey = 8 end
                    if idx ~= 0 then
                      ttab[indey] = value
                    end
                  end
                end
              end
            end
          end
        end
      end
    end
    if ttab[1] == "" then
      ttab[1] = ttab[3] .. " " .. ttab[2]
    end
    file:close()
    return(ttab)
  end
end
