#!/bin/sh

ONE=
if test "x$1" != "xTo:" -a "x$1" != "xCc:" ; then
  echo searching addressbook
else
  ONE=$1
  shift
fi

while test x"$1" != "x" ; do
  V=$(echo $1 | sed -e 's/,$//;')
  grep -i "$V" ~/.addressbook | while read line ; do
    S=$(echo "$line" | cut -d '	' -f 1)
    N=$(echo "$line" | cut -d '	' -f 2)
    A=$(echo "$line" | cut -d '	' -f 3)
    
    if test x$ONE = xtrue ; then
      echo -n " $A,"
    else
      echo -e "$A\t$N\t$S"
    fi
  done
  shift
done | \
(if test ! -z "$ONE" ; then
  echo "$ONE" $(cat | sed -e 's/,$//;')
else
  cat
fi)
