#!/bin/ksh
mail_address=""
condition=1
yes_no=1
invalid=1
CONFIG="/etc/mail_addr"
smtp_mail=1
del_modiy=0
get_mail_address(){
	while [ $invalid -ne 0  ] ; do
		echo "*******************************************************************"
		echo "*  Please input Administra's mail address for send alert message. *"
		echo "*******************************************************************"
		echo  "Mail Address:\c"
		read mail_address
		echo $mail_address | grep "@" > /dev/null
		if [ $? = 1 ]; then 
			echo "**********************************"
			echo "*      Invalid Mail address      *"
			echo "**********************************"
		else
			grep  $mail_address $CONFIG >/dev/null
			if [ $? = 1 ]; then 
				echo  "MAIL ADDRESS: $mail_address" >> $CONFIG
				invalid=0
				yes_no=1
			else
				echo "*************************************************"
				echo "*      You had inputed the same mail address    *"
				echo "*************************************************"
			fi
		fi
	done
}

input_again(){
	while [ $yes_no -ne 0 ] ; do
		echo  "Input another mail address (y/n):\c"
		read 
		case $REPLY in
		"Y")
		yes_no=0
		invalid=1
		;;
		"y")
		yes_no=0
		invalid=1
		;;
		"N")
		yes_no=0
		condition=0
		;;
		"n")
		yes_no=0
		condition=0
		;;
		*)
		echo "*************************************************"
		echo "*     Invalid input ,please input y or n        *"
		echo "*************************************************"
		;;
		esac
	done
}

get_smtp_server(){
	echo "********************************************"
	echo "*   lease IP or Name of the SMTP server.   *"
	echo "********************************************"
	echo  "SMTP Server :\c"
	read
	echo  "SMTP: $REPLY" >> $CONFIG
}

usage () {
		echo "usage : $0 [add|help|list|modify|new|reset|test]"
		echo "add: add new administrator's mail to config file"		
		echo "del: delete one or serval mail address"
		echo "help: help, show this help messages"
		echo "list: display current context of config file"
        	echo "modify: change curren smtp server address or administor's mail address"
		echo "new: create a new config file and do the first config"
		echo "reset: reset config file and do a config again"
		echo "test: check the if the address of smtp server and mail are right"
}

if_smtp_mail() {
	feedback=1
	while [ $feedback -ne 0 ]; do
		echo  "Change SMTP server or Mail address ?(smtp/mail)\c"
		read
		case $REPLY in
		"smtp")
		feedback=0
		smtp_mail=0
		;;
		"mail")
		feedback=0
		smtp_mail=1
		;;
		*)
		echo "Invalid Input"
		;;
		esac	
	done

}

select_change_item() {
	count=0
	unset old_mail
#	read < $CONFIG
	while read  ; do
		if [ $count -ne 0 ]; then 
				old_mail[$count -1]=` echo $REPLY | awk  -F'\ ' '{print $3;}'`
		fi
		count=`expr $count + 1`
	done < $CONFIG
	select index in ${old_mail[*]}  none
	do
		if [ $index = "none" ]; then
			break
		else
			if [ $del_modify = 1 ]; then
				update_mail
			fi
			break;
		fi
	done
}

delete_mail(){
    del_modify=0
	while [ 1 = 1 ] ;do
		line=`grep -c "MAIL ADDRESS" $CONFIG`
		if [ $line = 1 ]; then
			 echo "You must had at least one mail address,can not delete last one"	
		 	exit 0
 		fi
		select_change_item
		while read ; do
	   		echo $REPLY |grep SMTP >>/dev/null
	  		if [ $? = 0 ] ; then
	    		echo $REPLY >> /etc/mail_addr.tmp
	       		continue
	   		else
	       		UPDATE=$( echo $REPLY | awk  -F'\ ' '{print $3;}')
	       		if [ $UPDATE != $index ]; then
	       			echo $REPLY >> /etc/mail_addr.tmp
	    		fi
			fi
   		done < $CONFIG
   		mv -f /etc/mail_addr.tmp $CONFIG 
		while [ 1 = 1 ] ; do
			echo  "Delete another mail address (Y/N)\c"
			read
			case $REPLY in
			"Y")
			break;;
			"y")
			break;;
			"N")
			 exit	;;
			"n")
			break 2	;;
			*)
			echo "Invalid Input"
			;;
			esac
		done
done
		
}

update_mail(){
	echo  "Change mail address $index:\c"
	while read new_mail ; do
		if [ -z $new_mail] ; then 
			new_mail=$index
			break	
		fi
		echo $new_mail | grep "@" > /dev/null
	    if [ $? = 1 ]; then
		    echo "**********************************"
		    echo "*      Invalid Mail address      *"
		    echo "**********************************"
			echo  "Change mail address $index:\c"
		else
		    grep  $new_mail $CONFIG >/dev/null
		    if [ $? = 1 ]; then
				break
			else
				echo "*************************************************"
		        echo "*      You had inputed the same mail address    *"
		        echo "*************************************************"
				echo  "Change mail address $index:\c"
		    fi
		fi
	done
	touch /etc/mail_addr.tmp
	while read ; do
		 echo $REPLY |grep SMTP >>/dev/null 
		if [ $? = 0 ] ; then
			echo $REPLY >> /etc/mail_addr.tmp
			continue
		else
			UPDATE=$( echo $REPLY | awk  -F'\ ' '{print $3;}')
			if [ $UPDATE = $index ]; then
				echo "MAIL ADDRESS: $new_mail" >> /etc/mail_addr.tmp
			else
				echo $REPLY >> /etc/mail_addr.tmp 
			fi
		fi
	done < $CONFIG
	mv -f /etc/mail_addr.tmp $CONFIG 
}

change_mail(){
	del_modify=1
	while [ 1 = 1 ]; do
		select_change_item
		while [ 1 = 1 ] ; do
	        echo  "Change another mail address (Y/N)\c"
    	    read
		    case $REPLY in
		    "Y")
		    break;;
		    "y")
		    break;;
		    "N")
		    exit   ;;
		    "n")
		    break 2 ;;
		    *)
		    echo "Invalid Input"
		    ;;
		    esac
		done
	done
}

change_smtp() {
	old_smtp=$(grep  "SMTP:" $CONFIG | awk -F'\ ' '{print $2;}')
	echo  "Input new SMTP server address ($old_smtp):\c"
	read
	if [ -z $REPLY  ]; then 
			break;
	else
			touch /etc/mail_addr.tmp
			echo "SMTP: $REPLY" >> /etc/mail_addr.tmp
			grep "MAIL ADDRESS:"  $CONFIG >> /etc/mail_addr.tmp
			mv -f /etc/mail_addr.tmp $CONFIG 
	fi
			
}

case $1 in 
	"add")
	while [ $condition -ne 0 ] ; do
		get_mail_address
		input_again
	done
	;;
	"del")
		delete_mail
		;;
	"help")
	usage
	;;
	"list")
	if [ -e $CONFIG ] ; then 
		cat $CONFIG
	else
		echo "Config file did not exist"
	fi
	;;
	"modify")
	if_smtp_mail
	if [ $smtp_mail -eq 0 ]; then 
		change_smtp 
	else
		change_mail
	fi
	;;
	"new")
	if [ ! -e $CONFIG ]; then
		touch  $CONFIG
		get_smtp_server
		while [ $condition -ne 0 ] ; do
			get_mail_address
			input_again		
		done
	fi
	;;
	"reset")
	if [ -e $CONFIG ]; then
			rm $CONFIG -f
	fi
	touch $CONFIG
	get_smtp_server
	while [ $condition -ne 0 ] ; do
		get_mail_address
		input_again
	done
	;;
	"test")
	if [  -e $CONFIG ]; then
		/bin/mshd_mail test
	else
		echo "Config file did not exist"
	fi
	;;
	*)
	echo "usage : $0 [add|del|help|list|modify|new|reset|test]"
	;;
esac

