#! /bin/bash

Syntax="Syntax:
$0 --help|help|-h
$0 --list|list|-l
$0 --status|status|-s
$0 --test|test|-t to_mail@address
$0 --version|version|-v
These options require root power:
$0 --install|install|-i
$0 --update|update|-u
$0 --rescue|rescue|-r
$0 --off|off	unset ssmtp configuration
$0 --on|on	repair ssmtp configuration
$0 --alias|alias user email_alias
$0 smtp.server:587 mail@address PaSsWoRd
SSMTP is GPL V3 licensed by SISalp, first version 2018
----------------------------------------"

case "$1" in
--version|version|-v)
	SSMTPVersion="version 1.0, 15 november 2018 rev 2"
	echo "<!--SSMTP version is $SSMTPVersion -->"
	exit 0
	;;
--help|help|-h)
	$0 --version
	echo "$Syntax"
	exit 0
	;;
--list|list|-l)
	if [ -f /etc/ssmtp/ssmtp.conf ] ; then
		cat /etc/ssmtp/ssmtp.conf
	fi
	exit 0
	;;
--status|status|-s)
	if [ ! -f /etc/ssmtp/ssmtp.conf ] ; then
		echo "ssmtp is not installed"
	else
		if cat /etc/ssmtp/ssmtp.conf | grep -q "unset by " ; then
			echo "ssmtp is off"
		else
			echo "ssmtp is on"
		fi
	fi
	exit 0
	;;
--test|test|-t)
	if [ -f /etc/ssmtp/ssmtp.conf ] ; then
		if cat /etc/ssmtp/ssmtp.conf | grep -q "unset by " ; then
			echo "ssmtp is off"
		fi
		case "$2" in
		*@*)
			;;
		*)
			echo "$Syntax"
			exit 1
			;;
		esac
		if cat /etc/ssmtp/ssmtp.conf | mail -s "SSMTP test mail sent on $HOSTNAME" $2 ; then
			echo "mail is sent to $2"
			exit 0
		else
			echo "FAILURE when mail is sent to $2"
			exit 1
		fi
	else
		echo "/etc/ssmtp/ssmtp.conf is not found"
		if echo "/etc/ssmtp/ssmtp.conf is not found" | mail -s "SSMTP test mail /etc/ssmtp/ssmtp.conf is not found on $HOSTNAME" $2 ; then
			echo "mail is sent to $2"
			exit 0
		else
			echo "FAILURE when mail is sent to $2"
			exit 1
		fi
	fi
	exit 0
	;;
esac
case "`whoami`" in
root)
	;;
*)
	echo "you must get root for $1 option"
	exit 2
	;;
esac
case "$1" in
--update|update|-u)
	cd /usr/local/bin
	mv SSMTP /tmp/SSMTP
	if wget -q http://download.sisalp.net/scripts/SSMTP ; then
		chmod 755 SSMTP
		diff /tmp/SSMTP SSMTP
		echo "SSMTP is up to date `$0 --version`"
		exit 0
	else
		mv /tmp/SSMTP SSMTP
		echo "SSMTP update failed"
		exit 1
	fi
	;;
--rescue|recuse|-r)
	case "`whoami`" in
	root)
		cd /usr/local/bin
		cp SSMTP rescue.SSMTP
		chmod 755 rescue.SSMTP
		exit 0
		;;
	*)
		echo "you must get root"
		exit 2
		;;
	esac
	exit 0
	;;
--install|install|-i)
	apt-get update
	apt-get install ssmtp
	exit 0
	;;
--off|off)
	if [ -f /etc/ssmtp/ssmtp.conf ] ; then
		if cat /etc/ssmtp/ssmtp.conf | grep -q "unset by " ; then
			echo "/etc/ssmtp/ssmtp.conf is already unset"
		else
			mv /etc/ssmtp/ssmtp.conf /etc/ssmtp/ssmtp.conf.set
			echo "unset by $0" > /etc/ssmtp/ssmtp.conf
			echo "/etc/ssmtp/ssmtp.conf is unset"
		fi
	else

		echo "/etc/ssmtp/ssmtp.conf is not found"
	fi
	exit 0
	;;
--on|on)
	if [ -f /etc/ssmtp/ssmtp.conf ] && [ -f /etc/ssmtp/ssmtp.conf.set ] ; then
		if cat /etc/ssmtp/ssmtp.conf | grep -q "unset by " ; then
			mv /etc/ssmtp/ssmtp.conf.set /etc/ssmtp/ssmtp.conf
			echo "/etc/ssmtp/ssmtp.conf is set"
		else
			echo "/etc/ssmtp/ssmtp.conf is already set"
		fi
	else
		echo "/etc/ssmtp/ssmtp.conf or /etc/ssmtp/ssmtp.conf.set is not found"
	fi
	exit 0
	;;
--alias|alias)
	if ! cat /etc/ssmtp/revaliases | grep -q "^$2:" ; then
		echo "$2:$3:$4" >> /etc/ssmtp/revaliases
	fi
	cat /etc/ssmtp/revaliases
	exit 0
	;;
*:*)
	#ssmtp parameters detected
	;;
*)
	echo "$Syntax"
	exit 1
	;;
esac
case "$2" in
*@*)
	;;
*)
	echo "$Syntax"
	exit 1
	;;
esac
if [ -z "$3" ] ; then
	echo "$Syntax"
	exit 1
fi
if [ ! -f /etc/ssmtp/ssmtp.conf.dist ] ; then
	cp /etc/ssmtp/ssmtp.conf /etc/ssmtp/ssmtp.conf.dist
fi
if [ -f /etc/ssmtp/ssmtp.conf ] ; then
	cp --backup=numbered /etc/ssmtp/ssmtp.conf /etc/ssmtp/ssmtp.conf.save
	echo "#
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
#root=postmaster
root=$2

# The place where the mail goes. The actual machine name is required no 
# MX records are consulted. Commonly mailhosts are named mail.domain.com
#mailhub=mail

# Where will the mail seem to come from?
#rewriteDomain=`echo "$2" | cut -d@ -f2`

# The full hostname
hostname=`hostname -f`

# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
FromLineOverride=YES

UseTLS=YES
UseSTARTTLS=YES
mailhub=$1
AuthUser=$2
AuthPass=$3
" > /etc/ssmtp/ssmtp.conf
	$0 --alias `whoami` $2 $1
	echo "/etc/ssmtp/ssmtp.conf is up to date"

	exit 0
else
	echo "/etc/ssmtp/ssmtp.conf is not found"
	exit 1
fi
exit 0
