#!/bin/bash
#
set -e
if [ -z "`dpkg -l |grep mutt`" ];then
	sudo apt install mutt
fi
echo "install mutt over"
if [ -z "`dpkg -l |grep msmtp`" ];then
	sudo apt install msmtp
fi
echo "install msmtp over"

muttrc=~/.muttrc
msmtprc=~/.msmtprc
if [ -e ${muttrc} ];then
	echo ".muttrc file over"
else
	echo "set sendmail=\"/usr/bin/msmtp\"" >> ${muttrc}
	echo "set use_from=yes" >> ${muttrc}
	echo "set realname=\"RaspberryPi\"" >> ${muttrc}
	echo "set editor=\"nano\"" >> ${muttrc}
fi
if [ -e ${msmtprc} ];then
	echo ".msmtpec file over"
else
	echo "account 163" >> ${msmtprc}
	echo "defaults" >> ${msmtprc}
	echo "host smtp.163.com" >> ${msmtprc}
	echo "from frogchou@163.com" >> ${msmtprc}
	echo "auth plain" >> ${msmtprc}
	echo "user frogchou@163.com" >> ${msmtprc}
	echo "password 修改密码" >> ${msmtprc}
	echo "account default : 163" >> ${msmtprc}
	echo "logfile /var/log/msmtp.log" >> ${msmtprc}
fi

chmod 755 ${muttrc}
chmod 755 ${msmtprc}
if [ -e /var/log/msmtp.log ];then
	chmod 777 /var/log/msmtp.log
else
	touch /var/log/msmtp.log
	chmod 777 /var/log/msmtp.log
fi
if [ -z "`grep 'alias mail=\"mutt -s\"' /etc/profile`" ];then
	sed -i '$a alias mail=\"mutt -s\"' /etc/profile
	source /etc/profile
else
	source /etc/profile
fi
