#!/bin/bash

version="0.00"
# See bzr for changelog


function print_name {
    # copyright
    cat <<- EOF >&2

Sarkozy.sh $version
    (C) 2007 Vincent Riquer <vincent@riquer.fr>

    Propaganda... that's what it's all about...

    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.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

EOF
}

function help {
	print_name
	cat <<-EOF >&2
		Usage:
		sarkozy.sh -f <address_list> -t <template> [-vp]

		    -f <address_file>: list of email addresses in the form
		        email:last name:first name:nickname
		        (nickname is optional)

		    -t <template>: email to be sent, with %email%, %fname% %lname% and
		        %nick% to be replaced with their corresponding
		        elements from <address_file>

		Debugging:
		    -v: more output (may be repeated to run set -x)
		    -p: only print what would be done

	EOF
	exit 0
}

verbose=0

while getopts "f:t:vph" opt
do
	case $opt in
		f) adresses="$OPTARG" ;;
		t) template="$OPTARG" ;;
		v) verbose=$(( $verbose + 1))
			if [ $verbose -gt 1 ]
			then
				set -x
			fi
		;;

		p) pretend=1 ;;
		h) help ;;
	esac
done

print_name

function execute {
# By using this function as a wrapper, we are able to work in normal, 
# verbose, or don't-do-anything modes
	if [ $pretend ]; then
		echo "[not executed] $@"
		read -p "[ press return ]"
	elif [ $verbose -eq 1 ];then
		echo " + $@"
		bash -c "$@" || error
	else
		bash -c "$@" || error
	fi
}


