#!/bin/bash

###########################################################################
#
#	Shell program to Send bulk mails from an address file.
#
#	Copyright 2001, USM Bish <bish@nde.vsnl.net.in>.
#
#	This program is free software; you can redistribute it and/ or
#	modify it under the terms of the GNU General Public License as
#	published by the Free Software Foundation; either version 2 of
#	the License, or (at your option) any later version. 
#
#	This program is distributed in the hope that it will be useful
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY  or  FITNESS FOR A PARTICULAR PURPOSE. See the
#	GNU General Public License for more details.
#
#
#	Usage:
#
#		bulk-mail [ -h | --help ]
#               bulk-mail [ -a <addressfile>]
#
#	Options:
#
#		-h, --help	  Display this help message and exit.
#               -a <addressfile>  Addressfile for bulk mail  
#
#	Revisions:
#
#	Dec/29/2001	File created             ........ ver 0.1
#
###########################################################################


###########################################################################
#	Constants
###########################################################################

PROGNAME=$(basename $0)
VERSION="0.1"
TEMP_FILE=/tmp/${PROGNAME}.$$


###########################################################################
#	Functions
###########################################################################

function send_bulk
{
    CNT=0
    ALIGN="\033[10G"
    for ADDRESSEE in `cat $ADDRESSFILE`; do
       CNT=$((CNT+1))
       echo -en "o $CNT]$ALIGN$ADDRESSEE"
       echo "#!/bin/sh" > $TEMP_FILE
       echo "sendmail -t <<- -EndOfMail-" >> $TEMP_FILE
       echo "From: "$FROM >> $TEMP_FILE
       echo "To: "$ADDRESSEE >> $TEMP_FILE
       echo "Subject: "$SUBJECT >> $TEMP_FILE
       echo "" >> $TEMP_FILE
       cat $MESSAGE >> $TEMP_FILE
       echo "" >> $TEMP_FILE
       echo "-EndOfMail-" >> $TEMP_FILE
       echo "" >> $TEMP_FILE
       chmod +x $TEMP_FILE
       $TEMP_FILE
       rm -f $TEMP_FILE
    done 
    echo
    echo "Done ..."
    echo
}

function get_details
{
    echo
    echo $PROGNAME"                      version : "$VERSION
    echo
    echo -en "Enter From address .... "
    read FROM
    echo -en "Enter Subject line .... "
    read SUBJECT
    echo -en "Message file name ..... "
    read MESSAGE
    if [ -s $MESSAGE ]; then
        echo
        echo "Beginning Delivery ... please wait"
        echo
    else
        echo "ERROR: Message file [$MESSAGE] not found"
        term_exit
    fi        
}

function clean_up
{
    rm -f ${TEMP_FILE}
}


function graceful_exit
{
    clean_up
    exit
}


function error_exit 
{
    echo "${PROGNAME}: ${1:-"Unknown Error"}" >&2
    clean_up
    exit 1
}

function term_exit
{
    echo "${PROGNAME}: Terminated"
    clean_up
    exit
}

function int_exit
{
    echo "${PROGNAME}: Aborted by user"
    clean_up
    exit
}


function usage
{
    echo "Usage: ${PROGNAME} [-h | --help]"
}


function helptext
{
    clear
    local tab=$(echo -en "\t\t")
		
    cat <<- -EOF-

        ${PROGNAME}                                      ver. ${VERSION}	
 
        This is a program to Send bulk mails from an address file.
	
        Options:
	
        -h, --help	    Display this help message and exit.
        -a <addressfile>    Addressfile for bulk mail
        
        An interactive program for bulk-mailing. All the addresses
        should be saved in a text file with no blank  lines within
        it. The program is invoked with the command:
        
        $PROGNAME -a addressfile         
        
        Your message should be saved in another text file.You will 
        be prompted for a From: address and Subject: line, and the 
        name of message file. 
        
        If you have the  sendmail  command working on your box the 
        rest is automaic.
         
		
-EOF-
}	


###########################################################################
#	Program starts here
###########################################################################

# Trap TERM, HUP, and INT signals and properly exit

trap term_exit TERM HUP
trap int_exit INT

# Process command line arguments
if ! [ "$1" ]; then
    usage
    graceful_exit
fi

if [ "$1" = "--help" ]; then
    helptext
    graceful_exit
fi

# Process arguments - edit to taste

while getopts ":ha:" opt; do

    case $opt in
    
    a )	if [ -s $2 ]; then
            ADDRESSFILE=$2
            get_details
            send_bulk
        else
            echo "ERROR: Addressfile not found ..." $2
            term_exit
        fi        
        ;;
		
    h )	helptext
	graceful_exit 
	;;
	
    * )	usage
	exit 1
	
    esac
done

graceful_exit


###############################################################
