#!/bin/sh
# alist: Creates a distribution list containing all contacts
# in the specified mutt alias file
# Version: 0.1
# Author: Gavin Costello (gavcos@gmail.com)
PROGNAME=`basename "$0"`
TMPDIR=/tmp
umask 077
if [ $# -lt 1 ]; then
echo 1>&2 "Usage: ${PROGNAME} aliases_file"
exit 1
fi
SRC=$1
DEST=$TMPDIR/list
if [ -f $SRC ]; then
echo 1>&2 "Backing up " ${SRC}
cp $SRC $SRC.bak
if [ -s $DEST ]; then
echo 1>&2 "Removing old list file"
rm -rf $DEST
fi
echo 1>&2 "Creating new alias list"
touch $DEST
echo -n 1>&2 "alias all " > $DEST
cat $SRC | awk 'BEGIN { OFS=","; ORS=" "} { print $2 }' >> $DEST
echo 1>&2 "Appending list to alias file"
cat $DEST >> $SRC
rm -rf "${DEST}" && echo 1>&2 "Deleted temporary list file (${DEST})."
exit 0
else
echo 1>&2 "Unable to source alias file, check arguments"
exit 1
fi