#! /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 msmtp configuration
$0 --on|on	repair msmtp configuration
$0 --gmail|--ovh|--ovh_pro|gmail|ovh|ovh_pro mail@address PaSsWoRd [from_real_name]

MSMTP is GPL V3 licensed by SISalp, first version 2025
----------------------------------------"

case "$1" in
--version|version|-v)
	MSMTPVersion="version 0.9, 24 November 2025 rev 0"
	echo "<!--MSMTP version is $MSMTPVersion -->"
	exit 0
	;;
--help|help|-h)
	$0 --version
	echo "$Syntax"
	exit 0
	;;
--list|list|-l)
	if [ -f /etc/msmtprc ] ; then
		cat /etc/msmtprc
	fi
	exit 0
	;;
--status|status|-s)
	if [ ! -f /etc/msmtprc ] ; then
		echo "$LINENO|msmtp is not installed"
	else
		if cat /etc/msmtprc | grep -q "unset by " ; then
			echo "msmtp is off"
		else
			echo "msmtp is on"
		fi
	fi
	exit 0
	;;
--test|test|-t)
	if [ -f /etc/msmtprc ] ; then
		if cat /etc/msmtprc | grep -q "unset by " ; then
			echo "msmtp is off"
		fi
		case "$2" in
		*@*)
			;;
		*)
			echo "$LINENO|$Syntax"
			exit 1
			;;
		esac
		LOREM="/tmp/lorem"
		if [ ! -f $LOREM ] ; then
			wget http://download.sisalp.net/lorem -O $LOREM
		fi
		if [ -e ~/.muttrc ] ; then
			if cat $LOREM | mutt -s "MSMTP test mail sent by mutt on $HOSTNAME" -a  $LOREM -- $2 ; then
				rm -f $LOREM
				echo "$LINENO|mail is sent to $2 by mutt"
				exit 0
			else
				rm -f $LOREM
				echo "$LINENO|FAILURE when mail is sent to $2 by mutt"
				exit 1
			fi
		else
			if cat $LOREM | mail -s \"MSMTP test mail sent by mail on $HOSTNAME\" $2 ; then
				rm -f $LOREM
				echo "$LINENO|mail is sent to $2 by mail"
				exit 0
			else
				rm -f $LOREM
				echo "$LINENO|FAILURE when mail is sent to $2 by mail"
				exit 1
			fi
		fi
	else
		echo "$LINENO|/etc/msmtprc is not found"
		if echo "/etc/msmtprc is not found" | mail -s "MSMTP test mail /etc/msmtprc is not found on $HOSTNAME" $2 ; then
			echo "$LINENO|mail is sent to $2"
			exit 0
		else
			echo "$LINENO|FAILURE when mail is sent to $2"
			exit 1
		fi
	fi
	exit 0
	;;
esac
case "`whoami`" in
root)
	;;
*)
	echo "$LINENO|you must get root for $1 option"
	exit 2
	;;
esac
case "$1" in
--update|update|-u)
	cd /usr/local/bin
	mv MSMTP previous.MSMTP
	if wget -q http://download.sisalp.net/scripts/MSMTP ; then
		chmod 755 MSMTP
		diff previous.MSMTP MSMTP
		echo "$LINENO|MSMTP is up to date `$0 --version`"
		exit 0
	else
		mv previous.MSMTP MSMTP
		echo "$LINENO|MSMTP update failed"
		exit 1
	fi
	;;
--rescue|rescue|-r)
	case "`whoami`" in
	root)
		cd /usr/local/bin
		cp MSMTP rescue.MSMTP
		chmod 755 rescue.MSMTP
		exit 0
		;;
	*)
		echo "$LINENO|you must get root"
		exit 2
		;;
	esac
	exit 0
	;;
--install|install|-i)
	apt-get update
	apt-get install msmtp msmtp-mta mutt
	exit 0
	;;
--off|off)
	if [ -f /etc/msmtprc ] ; then
		if cat /etc/msmtprc | grep -q "unset by " ; then
			echo "$LINENO|/etc/msmtprc is already unset"
		else
			mv /etc/msmtprc /etc/msmtprc.set
			rm -f ~/.muttrc
			echo "unset by $0" > /etc/msmtprc
			echo "$LINENO|/etc/msmtprc is unset"
		fi
	else
		echo "$LINENO|/etc/msmtprc is not found"
	fi
	exit 0
	;;
--on|on)
	if [ -f /etc/msmtprc ] && [ -f /etc/msmtprc.set ] ; then
		if cat /etc/msmtprc | grep -q "unset by " ; then
			mv /etc/msmtprc.set /etc/msmtprc
			echo "$LINENO|/etc/msmtprc is set"
		else
			echo "$LINENO|/etc/msmtprc is already set"
		fi
	else
		echo "$LINENO|/etc/msmtprc or /etc/msmtprc.set is not found"
	fi
	if [ -f /etc/msmtprc ] && [ ! -e ~/.muttrc ] ; then
		cat /etc/msmtprc | grep "^#mutt:" | cut -f2- > /etc/muttrc
		ln -s /etc/muttrc ~/.muttrc
	else
		echo "$LINENO|mutt configuration ~/.muttrc is unchanged"
	fi
	exit 0
	;;
--gmail|--ovh|--ovh_pro|gmail|ovh|ovh_pro)
	#msmtp parameters are checked

	case "$2" in
	*@*)
		;;
	*)
		echo "$LINENO|$Syntax"
		exit 1
		;;
	esac
	if [ -z "$3" ] ; then
		echo "$Syntax"
		exit 1
	fi
	if [ -f /etc/msmtprc ] && [ ! -f /etc/msmtprc.dist ] ; then
		cp /etc/msmtprc /etc/msmtprc.dist
	fi
	if [ -f /etc/msmtprc ] ; then
		cp --backup=numbered /etc/msmtprc /etc/msmtprc.save
	fi
	if [ -f /etc/muttrc ] && [ ! -f /etc/muttrc.dist ] ; then
		cp /etc/muttrc /etc/muttrc.dist
	fi
	msmtp_account=`echo "$1" | cut -d- -f3`
	shift
	msmtp_username=`echo "$1" | cut -d@ -f1`
	msmtp_domain=`echo "$1" | cut -d@ -f2`
	shift
	msmtp_password="$1"
	shift
	msmtp_real_name="$*"
	if [ -z "$msmtp_real_name" ] ; then
		mutt_realname=""
	else
		mutt_realname="set realname=\"$msmtp_real_name\""
	fi
	echo "#
# Config file for MSMTP sendmail service by $0 on `date`
# Valeurs par défaut pour tous les comptes.
defaults
auth           on
tls            on
tls_starttls   on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile        /var/log/msmtp

# Exemple pour un compte Gmail
account        gmail
auth           plain
host           smtp.gmail.com
port           587
from           $msmtp_username@gmail.com
user           $msmtp_username
password       $msmtp_password

# Exemple pour un compte OVH MX PLAN
account        ovh
host           ssl0.ovh.net
port           587
from           $msmtp_username@$msmtp_domain
user           $msmtp_username@$msmtp_domain
password       $msmtp_password

# Exemple pour un compte OVH E-MAIL PRO
account        ovh_pro
host           proX.mail.ovh.net
port           587
from           $msmtp_username@$msmtp_domain
user           $msmtp_username@$msmtp_domain
password       $msmtp_password

# Définir le compte par défaut
account default : $msmtp_account

#mutt:	#configuration
#mutt:	set sendmail=\"/usr/bin/msmtp\"
#mutt:	set use_from=yes
#mutt:	$mutt_realname
#mutt:	set from=$msmtp_username@$msmtp_domain
#mutt:	set envelope_from=yes
" > /etc/msmtprc
	cat /etc/msmtprc | grep "^#mutt:" | cut -f2- > /etc/muttrc
	ln -s /etc/muttrc ~/.muttrc
	echo "$LINENO|/etc/msmtprc is up to date"
	exit 0
	;;
*)
	echo "$LINENO|$Syntax"
	exit 1
	;;
esac
exit 0
