#!/bin/sh

# muttfolders
# Read data from subaddr and write mutthooks.rc
# set paths here:

SUBADDR="$HOME/.procmail/subaddr"
MUTTHOOKS="$HOME/mutthooks.rc"

# NAME and DOMAIN are used to make addresses for
# header lines in this form:
#    From: "$NAME" <[subaddress]@$DOMAIN>
# or From: "$NAME" <$USER[subaddress]@DOMAIN>
# where [subaddress] comes from the data file
# For the first form, set $USER=""
# For the second form, $USER must include the "+" as below:

NAME="John Doe"
USER=""
DOMAIN="username.users.somewhere.nnn"

#Alternative form:
#
#USER="username+"
#DOMAIN="somewhere.nnn"

# The format of the 'subaddr' file is:
#
# foldername whitelistname public-subaddr private-subaddr add-code
#
# In awk these become $1 through $5:
#
# $1="foldername" is the name of a mail folder
# $2="whitelistname" is the whitelist associated with foldername
# $3="public-subaddr" is the subaddress exposed to the public
# $4="private-subaddr" is the subaddress used but not publicised
# $5="add-code" is the subaddress used to add an entry to the whitelist
#
# More than one folder can use the same whitelist.
# public-subaddr and private-subaddr may be the same or different.
# add-code should be kept secret, since it automatically adds an
# address to the corresponding whitelist.
#
# mutthooks.rc is intended to be "sourced" by my .muttrc
# and contains a series of folder-hook commands for each
# folder listed in the subaddr file. Editing the subaddr file
# and running this script allows mutt to recognize all listed folders
# and add the appropriate return address to outgoing messages.
#
# My .muttrc contains folder-hook commands for the default case.
# This is necessary because folder-hooks are only executed when
# you enter a folder, they are not reversed when you leave one.
#
# Output is written to a temporary file which is then substituted
# for the old mutthooks.rc file. This is so the old one will be
# preserved in case the script crashes for some reason.

# Note the first echo is a "write", the rest are "append"

echo "# This file is generated from data in:" >$MUTTHOOKS.tmp
echo "# $SUBADDR" >>$MUTTHOOKS.tmp
echo "# If you want to make changes, edit that file" >>$MUTTHOOKS.tmp
echo "# and run the 'muttfolders' script" >>$MUTTHOOKS.tmp
echo "" >>$MUTTHOOKS.tmp

# This line defines the base folder as a mailbox
# ("INBOX" in IMAP or maildir folders)
# This is defined by "set folder=" in your .muttrc

echo "mailboxes =" >>$MUTTHOOKS.tmp

# To use the private subaddress for both From: and Reply-To:,
# change $3 to $4 in the "From:" line below:

awk -v N="$NAME" -v U="$USER" -v D="$DOMAIN" ' \
 {printf("mailboxes =%s\n",$1);\
  printf("folder-hook %s my_hdr From: \"%s\" <%s%s@%s>\n",$1,N,U,$3,D);\
  printf("folder-hook %s my_hdr Reply-To: \"%s\" <%s%s@%s>\n",$1,N,U,$4,D);\
  printf("folder-hook %s my_hdr Bcc: %s%s@%s\n\n",$1,U,$5,D)\
 } ' $SUBADDR >>$MUTTHOOKS.tmp

echo "# EOF" >>$MUTTHOOKS.tmp

if [ -e $MUTTHOOKS ]
then rm $MUTTHOOKS
fi

mv $MUTTHOOKS.tmp $MUTTHOOKS

# EOF
