#!/bin/bash

export LANG="ko_KR.EUC-KR"
export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/local/sbin
tempFile="/tmp/autosetup.tmp"
insPath="`pwd`"
insTime=`date '+%s'`
dfPath="/usr/local/zwms"

clear
echo ""
echo "---------------------------------------------------------------------------"
echo "ƿƼ  [/bin/mail] [/usr/sbin/sendmail]"
echo "( α׷ v2.4.8 ̻ Ͻ  ֽϴ.)"
echo "---------------------------------------------------------------------------"
echo ""

if [ `id -u` -ne 0 ] ; then
	echo "ѿ"
	echo "ROOT  α Ͻʽÿ."
	echo ""
	exit 0
fi

sdCnt=`ps -ef | grep sendmail | grep -v grep | wc -l`
if [ $sdCnt -ne 0 ] ; then
	echo ""
	echo "sendmail  ǰ ֽϴ."
	echo "sendmail  Ͻ , õ ֽʽÿ."
	echo ""
	exit 0
fi


if [ ! -f ${insPath}/mailserver ] || [ ! -f ${insPath}/languagepack.txt ] ; then
	echo ""
	echo "ϼ ġ ġ  ֽʽÿ."
	echo ""
	exit 0
fi

read -p "޴ Էϼ. [ġ:1 / :2 / :N] : " proc1
echo ""

if [ "$proc1" == "N" ] ; then
	echo " Ǿϴ."
	echo ""
	exit 0
fi

if [ "$proc1" == "1" ] ; then
	if [ -f ${insPath}/mail ] && [ -f ${insPath}/sendmail ] ; then
		chown root.root ${insPath}/mail ${insPath}/sendmail
		chmod 755 ${insPath}/mail ${insPath}/sendmail
		chmod +s ${insPath}/sendmail
		if [ -f /bin/mail ] && [ ! -h /bin/mail ] ; then
			mv /bin/mail /bin/mail.back.${insTime}
			ln -s ${insPath}/mail /bin/mail
		else
			if [ ! -f /bin/mail ] ; then
				ln -s ${insPath}/mail /bin/mail
			fi
		fi
		if [ -h /usr/sbin/sendmail ] ; then
			mv /usr/sbin/sendmail /usr/sbin/sendmail.back.${insTime}
			ln -s ${insPath}/sendmail /usr/sbin/sendmail
		else
			if [ ! -f /usr/sbin/sendmail ] ; then
				ln -s ${insPath}/sendmail /usr/sbin/sendmail
			fi
		fi
		if [ "${insPath}" != "${dfPath}" ] ; then
			echo "${insPath}/" >> ${tempFile}
			cat ${tempFile} >> /etc/zwms.conf
			rm -f ${tempFile}
		else
			if [ -f /etc/zwms.conf ] ; then
				rm -f /etc/zwms.conf
			fi
		fi
		echo "ϷǾϴ."
		echo "OS    Ʒ   ˴ϴ."
		echo "/bin/mail -> /bin/mail.back.0000000000"
		echo "/usr/sbin/sendmail -> /usr/sbin/sendmail.back.0000000000"
		echo ""
		exit 0
	else
		echo "  : ${insPath}/"
		echo "mail Ǵ sendmail ƿƼ  "
		echo ""
		exit 0
	fi
fi
	

if [ "$proc1" == "2" ] ; then
	if [ -f /etc/zwms.conf ] ; then
		rm -f /etc/zwms.conf
	fi
	chown root.root ${insPath}/mail ${insPath}/sendmail
	chmod 755 ${insPath}/mail ${insPath}/sendmail
	chmod -s ${insPath}/sendmail
	if [ -h /bin/mail ] ; then
		chkArr=`ls -l /bin/mail | awk '{print NF}'`
		trgArr=`ls -l /bin/mail | awk '{print $'${chkArr}'}'`
		chkFile="${insPath}/mail"
		if [ "${chkFile}" == "${trgArr}" ] ; then
			rm -f /bin/mail
		fi
	fi
	if [ -h /usr/sbin/sendmail ] ; then
		chkArr=`ls -l /usr/sbin/sendmail | awk '{print NF}'`
		trgArr=`ls -l /usr/sbin/sendmail | awk '{print $'${chkArr}'}'`
		chkFile="${insPath}/sendmail"
		if [ "${chkFile}" == "${trgArr}" ] ; then
			rm -f /usr/sbin/sendmail
		fi
	fi
	echo "ŵǾϴ."
	echo "OS     ̿Ͻñ ٶϴ."
	echo "/bin/mail.back.0000000000"
	echo "/usr/sbin/sendmail.back.0000000000"
	echo ""
	exit 0
fi


exit 0


