#! /bin/sh
#
######################################
# Email Alert #
######################################

cd /etc/sss_script/email
. ./alert.conf > /dev/null 2>&1

   target=$EMAIL
   replace=$1
	  ./replace.sh "EMAIL=${target}" "EMAIL=${replace}" '/etc/sss_script/email/alert.conf'
	
   target=$SMTP_SERVER
   replace=$2
	  ./replace.sh "SMTP_SERVER=${target}" "SMTP_SERVER=${replace}" '/etc/sss_script/email/alert.conf'
	  ./replace.sh "mailhub=${target}" "mailhub=${replace}" '/usr/local/etc/ssmtp/ssmtp.conf'
	  
   target=$MAILTO
   replace=$3
	  ./replace.sh "MAILTO=${target}" "MAILTO=${replace}" '/etc/sss_script/email/alert.conf'
	  MAILTO=$3
	  
   target=$HDD_REPORT
   replace=$4
	  ./replace.sh "HDD_REPORT=${target}" "HDD_REPORT=${replace}" '/etc/sss_script/email/alert.conf'
	  
   target=$REPORT_TERM
   replace=$5
    if [ "$REPORT_TERM" != "$5" ]; then 
    	if [ "$REPORT_TERM" == 'Daily' ]; then
    		rm /etc/cron/cron.daily/hdd_report
    	fi
    	if [ "$REPORT_TERM" == 'Weekly' ]; then
    		rm /etc/cron/cron.weekly/hdd_report
    	fi
    	if [ "$REPORT_TERM" == 'Monthly' ]; then
    		rm /etc/cron/cron.monthly/hdd_report
    	fi
    	./replace.sh "REPORT_TERM=${target}" "REPORT_TERM=${replace}" '/etc/sss_script/email/alert.conf'
   fi 
   REPORT_TERM=$5
   
   rm /etc/cron/cron.daily/hdd_report                              
   rm /etc/cron/cron.weekly/hdd_report                             
   rm /etc/cron/cron.monthly/hdd_report  

   if [ "$1" == 'ON' ] ; then  
   	if [ "$4" == 'ON' ] ; then 
		if [ "$REPORT_TERM" == 'Daily' ]; then
   			cp /etc/cron/cron.d/hdd_report /etc/cron/cron.daily/
   		fi
    		
   		if [ "$REPORT_TERM" == 'Weekly' ]; then
   			cp /etc/cron/cron.d/hdd_report /etc/cron/cron.weekly/
   		fi
    				
   		if [ "$REPORT_TERM" == 'Monthly' ]; then
   			cp /etc/cron/cron.d/hdd_report /etc/cron/cron.monthly/
   		fi 
	fi
  fi

if [ "$6" == 'TEST' ]; then 
   HOSTNAME=`cat /etc/hostname`
   echo "To:${MAILTO}" > ./test.eml
   echo "From:${HOSTNAME} Notification System<${MAILTO}>" >> ./test.eml
   echo "Subject:`cat /etc/sss_script/email/alert.conf | grep "SUBJECT="|cut -d "=" -f 2` : EMAIL CONFIGURATION TEST Message" >> ./test.eml 
   echo "">> ./test.eml
   echo "Email Notification System is working" >> ./test.eml
   echo "">> ./test.eml
   /sbin/ssmtp ${MAILTO} < ./test.eml
   rm ./test.eml 
fi	
    
   
