#!/bin/sh
#
# Takes the aliases from Elm and outputs them in Mutt format.
# This output can be included in your ~/.muttrc in two ways:
#
# 1.  for a one time conversion concatenate them to your .muttrc:
#     > elm2mutt >> ~/.muttrc
#
# 2.  when you wnat to use both Elm and Mutt, but do not want to maintain two
#     lists of email addresses include a line in your .muttrc that says:
#       source ~/.mutt.aliases
#     now everytime you have new aliases in your .elm/aliases.text:
#     > elm2mutt > ~/.mutt.aliases

# This script is licensed under GPL, see the COPYING file for more details.
# Last Revision: 03-Apr-2000 10:26
# Version: 1.3

# AUTHOR
# J.I. van Hemert <jvhemert@cs.leidenuniv.nl>

# PHYSICAL MAIL
#
# LIACS - Leiden University
# Niels Bohrweg 1
# 2333 CA Leiden
# The Netherlands

# CHANGELOG
#
# 2000/05/01 (1.3)
# - fixed: aliases with a dash now work (thanks to Christopher Drexler)
# - changed: reversed CHANGELOG
# - BUG: more than two address for one person
#
# 2000/04/03 (1.2)
# - fixed: groups by scanning for a ',' inside email addresses
# - fixed: two addresses for one person
# - BUG: more than two address for one person
#
# 1999/02/22 (1.1)
# - added: newalias
# - fixed: groups by scanning for a ',' inside a record
#
# 1999/08/13 (1.0)
# - changed: now outputs to stdout
#
# 1999/08/17 (pre)
# - fixed: first line of output (Updating your personal...) removed
#

# Make sure we use the latest update
newalias > /dev/null

# Get people with one email address
elmalias -a -u -f 'alias %a %n <%v>' | grep -v '<.*,.*>' 

# Get groups
elmalias -a -u -f 'alias %a %n <%v>' | grep '<.*,.*>' | grep -v '<.*@.*>' | sed -e 's/alias \([a-zA-Z0-9-]*\) .* <\(.*\)>/alias \1 \2/g'

# Get people with two email addresses
elmalias -a -u -f 'alias %a %n <%v>' | grep '<.*,.*>' | grep '<.*@.*>' | sed -e 's/alias \([a-zA-Z0-9-]*\) \(.*\) <\([^,]*\),\([^,]*\).*>/alias \1 \1_1,\1_2\
alias \1_1 \2 <\3> \
alias \1_2 \2 <\4>/g'
