#!/bin/sh

# addresses
#
# Read data from subaddr and create a file containing all
# possible email addresses, with subaddresses. Additional
# addresses can be hard-coded here if necessary.
#
# Set paths here:

SUBADDR="$HOME/.procmail/subaddr"
MYEMAIL="$HOME/.myemail"

# USER is usually your login name
# add "+" to the end (or "-" if that's what your ISP uses)

USER="username+"
DOMAIN1="somewhere.nnn"
DOMAIN2="username.users.somewhere.nnn"

# Addresses are written to a temporary file which is then
# substituted for the original, so the original is preserved
# if the script crashes for some reason.

if [ -e $MYEMAIL.tmp ]
then rm $MYEMAIL.tmp
fi

# This line adds the "base" address (no subaddress)
# Additional addresses can be added here by duplicating
# this line with other addresses in it.

echo "username@somewhere.nnn" >>$MYEMAIL.tmp

# This adds addresses in the form:
# $USER+subaddress@$DOMAIN1
# (The "+" is stored in $USER, see above)
# This form may show up even if you
# normally use the other form.

awk -v U="$USER" -v D="$DOMAIN1" '
 {printf("%s%s@%s\n",U,$3,D);\
  printf("%s%s@%s\n",U,$4,D);\
  printf("%s%s@%s\n",U,$5,D)\
 }' $SUBADDR >>$MYEMAIL.tmp

# This add addresses in the form:
# subaddress@$DOMAIN2
# you can comment this out if you don't use that form

awk -v D="$DOMAIN2" '
 {printf("%s@%s\n",$3,D);\
  printf("%s@%s\n",$4,D);\
  printf("%s@%s\n",$5,D)\
 }' $SUBADDR >>$MYEMAIL.tmp

# This removes any duplicates-- for example if
# the public and private subaddresses are the same.

sort -u $MYEMAIL.tmp >$MYEMAIL

rm $MYEMAIL.tmp

# EOF
