#!/usr/bin/ruby
#
require 'tins/xt'
require 'set'

$shortcuts = Set.new

def shortcut(fn, suffix = nil)
  shortcut = fn.scan(/([A-ZÄÖÜ])[a-zäöü]/).join.downcase
  shortcut.empty? and return fn.gsub(' ', ?_)
  shortcut = "#{shortcut}#{suffix}"
  while $shortcuts.member?(shortcut)
    shortcut.sub!(/(\d+)\z/) { $1.to_i + 1 }
    shortcut.sub!(/\D\z/, '\\&1')
  end
  $shortcuts << shortcut
  shortcut
end

vcf_path = ARGV.shift || '~/Desktop/Contacts.vcf'
vcf_path = File.expand_path vcf_path
alias_path = ARGV.shift || '~/.muttrc-aliases'
alias_path = File.expand_path alias_path
begin
  real_alias_path = File.readlink alias_path
  alias_path = real_alias_path
rescue Errno::ENOENT
end

File.secure_write(alias_path) do |aliases|
  File.open(vcf_path) do |vcf|
    until vcf.eof?
      card = vcf.readline('END:VCARD')
      fn = card[/^FN:(.*)/, 1] or next
      fn.chomp!
      hemail = card[/^EMAIL;.*?type=HOME.*?:(.*)/, 1]
      hemail and aliases.puts "alias #{shortcut(fn, ?h)} #{fn} <#{hemail.chomp}>"
      wemail = card[/^EMAIL;.*?type=WORK.*?:(.*)/, 1]
      wemail and aliases.puts "alias #{shortcut(fn, ?w)} #{fn} <#{wemail.chomp}>"
      email = card[/^EMAIL;.*?:(.*)/, 1]
      if email && ! [ hemail, wemail ].include?(email)
        aliases.puts "alias #{shortcut(fn)} #{fn} <#{email.chomp}>"
      end
    end
  end
end