#!/bin/sh

local in_opts= recpt= cmd= server=

logger "smtpclientwrapper: $*"

error() {
	[ "$opt_L" = "yes" ] && logger -t $0[$$] -p user.warn "Error: $@"
	echo "Error: $@\n"
	exit 1
}

usage() {
	cat <<EOF
Usage: smtpclientwrapper [options] recipients
  -S	all: Tell smtpclient to try secondary if primary failed (default).
	primary: Use primary mail server.
	secondary: Use secondary mail server.
	*: Do not use mail server specific SMTP parameter values.
  -h  Display this page.

smtpclient Help:

$(smtpclient -h 2>&1)
EOF
}

# Options
#------------------------------------------------------------------------------
base_cmd() {
	cmd="smtpclient -L $in_opts" # Syslogging enabled with -L to catch errors
	[ "$opt_f" ] || {
		if [ "$root_SMTP_FromEmail" ]; then
			cmd="$cmd -f $root_SMTP_FromEmail"
		else
			error "Option '-f' argument empty!"
		fi
	}
}

add_primary() {
	[ "$root_SMTP_MailServer1" ] || error "Option '-S' argument empty!"
	cmd="$cmd -S $root_SMTP_MailServer1"
	[ "$root_SMTP_MailServerPort1" ] && cmd="$cmd -P $root_SMTP_MailServerPort1"
	if [ "$root_SMTP_Authentication_A1_Enabled" = yes ]; then
		[ "$opt_g" ] || {
			if [ "$root_SMTP_Authentication_A1_UserName" ]; then
				cmd="$cmd -g $root_SMTP_Authentication_A1_UserName"
			else
				error "Option '-g' argument empty!"
			fi
		}
		[ "$opt_G" ] || {
			if [ "$root_SMTP_Authentication_A1_Password" ]; then
				cmd="$cmd -G $root_SMTP_Authentication_A1_Password"
			else
				error "Option '-G' argument empty!"
			fi
		}
		[ "$opt_w" ] || {
			if [ "$root_SMTP_Authentication_A1_WeakestMethod" ]; then
				cmd="$cmd -w $root_SMTP_Authentication_A1_WeakestMethod"
			else
				error "Option '-w' argument empty!"
			fi
		}
		[ "$root_SMTP_Authentication_A1_UsePOP" = yes ] && {
			[ "$opt_j" ] || {
				if [ "$root_SMTP_Authentication_A1_POPServer" ]; then
					cmd="$cmd -j $root_SMTP_Authentication_A1_POPServer"
				else
					error "Option '-j' argument empty!"
				fi
			}
		}
	fi

	if [ "$root_SMTP_MailServerSSL1" = yes ]; then
		cmd="$cmd -n"

		[ "$opt_R" ] || [ "$opt_t" ] || {
			if [ "$root_SMTP_SSL_S1_RootCertificate" ]; then
				cmd="$cmd -t $root_SMTP_SSL_S1_RootCertificate"
			else
				cmd="$cmd -R /etc/ssl/certs"
			fi
		}
		[ "$opt_o" ] || [ "$opt_H" ] || [ "$opt_J" ] || {
		        [ "$root_SMTP_SSL_S1_ClientCertificate" ] && \
			[ "$root_SMTP_SSL_S1_ClientPrivateKey" ] && \
			[ "$root_SMTP_SSL_S1_ClientPrivateKeyPasswd" ] && {
				cmd="$cmd -o $root_SMTP_SSL_S1_ClientCertificate"
				cmd="$cmd -H $root_SMTP_SSL_S1_ClientPrivateKey"
				cmd="$cmd -J $root_SMTP_SSL_S1_ClientPrivateKeyPasswd"
			}
		}
	fi
}

add_secondary() {
	[ "$root_SMTP_MailServer2" ] || error "Option '-S' argument empty!"
	cmd="$cmd -S $root_SMTP_MailServer2"
	[ "$root_SMTP_MailServerPort2" ] && cmd="$cmd -P $root_SMTP_MailServerPort2"
	if [ "$root_SMTP_Authentication_A2_Enabled" = yes ]; then
		[ "$opt_g" ] || {
			if [ "$root_SMTP_Authentication_A2_UserName" ]; then
				cmd="$cmd -g $root_SMTP_Authentication_A2_UserName"
			else
				error "Option '-g' argument empty!"
			fi
		}
		[ "$opt_G" ] || {
			if [ "$root_SMTP_Authentication_A2_Password" ]; then
				cmd="$cmd -G $root_SMTP_Authentication_A2_Password"
			else
				error "Option '-G' argument empty!"
			fi
		}
		[ "$opt_w" ] || {
			if [ "$root_SMTP_Authentication_A2_WeakestMethod" ]; then
				cmd="$cmd -w $root_SMTP_Authentication_A2_WeakestMethod"
			else
				error "Option '-w' argument empty!"
			fi
		}
		if [ "$root_SMTP_Authentication_A2_UsePOP" = yes ]; then
			[ "$opt_j" ] || {
				if [ "$root_SMTP_Authentication_A2_POPServer" ]; then
					cmd="$cmd -j $root_SMTP_Authentication_A2_POPServer"
				else
					error "Option '-j' argument empty!"
				fi
			}
		fi
	fi
	if [ "$root_SMTP_MailServerSSL2" = yes ]; then
		cmd="$cmd -n"

		[ "$opt_R" ] || [ "$opt_t" ] || {
			if [ "$root_SMTP_SSL_S2_RootCertificate" ]; then
				cmd="$cmd -t $root_SMTP_SSL_S2_RootCertificate"
			else
				cmd="$cmd -R /etc/ssl/certs"
			fi
		}
		[ "$opt_o" ] || [ "$opt_H" ] || [ "$opt_J" ] || {
		        [ "$root_SMTP_SSL_S2_ClientCertificate" ] && \
			[ "$root_SMTP_SSL_S2_ClientPrivateKey" ] && \
			[ "$root_SMTP_SSL_S2_ClientPrivateKeyPasswd" ] && {
				cmd="$cmd -o $root_SMTP_SSL_S2_ClientCertificate"
				cmd="$cmd -H $root_SMTP_SSL_S2_ClientPrivateKey"
				cmd="$cmd -J $root_SMTP_SSL_S2_ClientPrivateKeyPasswd"
			}
		}
	fi
}

add_backup() {
	[ "$opt_B" ] || {
		[ "$root_SMTP_MailServer2" ] && {
			cmd="$cmd -B $root_SMTP_MailServer2"
			[ "$root_SMTP_MailServerPort2" ] && cmd="$cmd -Q $root_SMTP_MailServerPort2"
			if [ "$root_SMTP_Authentication_A2_Enabled" = yes ]; then
				[ "$opt_k" ] || {
					if [ "$root_SMTP_Authentication_A2_UserName" ]; then
						cmd="$cmd -k $root_SMTP_Authentication_A2_UserName"
					else
						error "Option '-k' argument empty!"
					fi
				}
				[ "$opt_K" ] || {
					if [ "$root_SMTP_Authentication_A2_Password" ]; then
						cmd="$cmd -K $root_SMTP_Authentication_A2_Password"
					else
						error "Option '-K' argument empty!"
					fi
				}
				[ "$opt_x" ] || {
					if [ "$root_SMTP_Authentication_A2_WeakestMethod" ]; then
						cmd="$cmd -x $root_SMTP_Authentication_A2_WeakestMethod"
					else
						error "Option '-x' argument empty!"
					fi
				}
				if [ "$root_SMTP_Authentication_A2_UsePOP" = yes ]; then
					[ "$opt_l" ] || {
						if [ "$root_SMTP_Authentication_A2_POPServer" ]; then
							cmd="$cmd -l $root_SMTP_Authentication_A2_POPServer"
						else
							error "Option '-l' argument empty!"
						fi
					}
				fi
			fi
			if [ "$root_SMTP_MailServerSSL2" = yes ]; then
			    [ "$opt_R" ] || [ "$opt_t" ] || {
				if [ "$root_SMTP_SSL_S2_RootCertificate" ]; then
				    cmd="$cmd -t $root_SMTP_SSL_S2_RootCertificate"
				else
				    cmd="$cmd -R /etc/ssl/certs"
				fi
			    }
			    [ "$opt_E" ] || [ "$opt_I" ] || [ "$opt_p" ] || {
				[ "$root_SMTP_SSL_S2_ClientCertificate" ] && \
				    [ "$root_SMTP_SSL_S2_ClientPrivateKey" ] && \
				    [ "$root_SMTP_SSL_S2_ClientPrivateKeyPasswd" ] && {
				    cmd="$cmd -E $root_SMTP_SSL_S2_ClientCertificate"
				    cmd="$cmd -I $root_SMTP_SSL_S2_ClientPrivateKey"
				    cmd="$cmd -p $root_SMTP_SSL_S2_ClientPrivateKeyPasswd"
				}
			    }
			fi
		}
	}
}

# Input parameters
#------------------------------------------------------------------------------
[ "$1" ] || error "Need parameters!"

while [ "$1" ]; do
        local default_opts="a:A:b:B:c:C:d:De:f:F:hij:LmM:P:q:Q:r:s:S:T:u:z:"
	local tls_opts="nNt:R:o:E:H:I:J:p:"
	local auth_pop_opts="g:G:k:K:"
	local auth_opts="W:x:"
	local pop_opts="j:l:"
	options=$default_opts$tls_opts$auth_pop_opts$auth_opts$pop_opts
	if getopts $options opt; then
		case "$opt" in
			a|A|b|B|c|C|d|e|E|f|F|g|G|H|I|j|J|k|K|l|M|n|N|o|p|P|q|Q|r|R|s|t|T|u|W|x|z)
				val="$OPTARG"
#[ "$val" ] || error "Option '$opt' argument missing!"
				[ "$opt" = s ] && val=$(echo "$val" | /bin/urldecode)
				eval "opt_$opt"="\"$val\""
				in_opts="$in_opts -$opt \"$val\""
				shift
				;;
			D|i|m)
				in_opts="$in_opts -$opt"
				;;
			S)
				server="$OPTARG"
				case "$server" in
					'')
						error "Option '-$opt' argument missing!"
						;;
					all|primary|secondary)
						;;
					*)
						in_opts="$in_opts -$opt $server"
						;;
				esac
				shift
				;;
			h)
				usage
				exit 0
				;;
			L)
				opt_L=yes
				;;
		esac
	else
		recpt="$recpt `echo $1 | tr [','';'] ' '`"
	fi
	shift
done

[ "$server" ] || server=all

# SMTP parameters
#------------------------------------------------------------------------------
file=/tmp/SMTP$$.txt
parhandclient --nocgi getgroup root.SMTP - SCRIPT > $file || {
	rm -f $file
	error "Failed to get SMTP parameters"
}
. $file
rm -f $file

# Send
#------------------------------------------------------------------------------
base_cmd

case $server in
	primary)
		add_primary
		;;
	secondary)
		add_secondary
		;;
	all)
		add_primary
		add_backup
		;;
	*)
		;;
esac

echo -e "Call:\n$cmd $recpt\n\n"
result=$(eval $cmd $recpt 2>&1)
retval=$?
echo -e $result
exit $retval
