#!/bin/sh

# makesubrc
#
# This script generates an INCLUDERC file from the
# subaddress data file. Set paths here:

SUBFILE="$HOME/.procmail/subaddr"
RCFILE="$HOME/.procmail/subdata.rc"

# 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.

# Data is written to a temporary file, which is then substituted
# for the old file, 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:" >$RCFILE.tmp
echo "# $SUBFILE" >>$RCFILE.tmp
echo "# If you want to make changes, edit that file" >>$RCFILE.tmp
echo "# and run the 'makesubrc' script" >>$RCFILE.tmp
echo "" >>$RCFILE.tmp

# Note this adds a trailing slash to FOLDERNAME, as required
# for maildir folders. If you're using mbox files, you'll have
# to remove it.

awk '{printf(":0\n* SUBADDR ?? %s\n{\n",$3);\
      printf(" FOLDERNAME=\"%s/\"\n",$1);\
      printf(" WLNAME=\"%s\"\n}\n\n",$2);\
      printf(":0\n* SUBADDR ?? %s\n{\n",$4);\
      printf(" FOLDERNAME=\"%s/\"\n",$1);\
      printf(" WLNAME=\"%s\"\n}\n\n",$2);\
      printf(":0\n* SUBADDR ?? %s\n{\n",$5);\
      printf(" FOLDERNAME=\"%s/\"\n",$1);\
      printf(" WLNAME=\"%s\"\n",$2);\
      printf(" ADDCODE=\"%s\"\n}\n\n",$5);\
     }' $SUBFILE >>$RCFILE.tmp

echo "# EOF" >>$RCFILE.tmp

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

mv $RCFILE.tmp $RCFILE

# EOF
