#!/bin/bash
# FileName: Postfix-setup-v1.0.sh
# Description: Install Postfix and doevcot
# By shuhui,2014,12,20

# Export Path
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
export PATH

#Mail domain (Setting yourself)
# No hostname,Pls attention
mail_domain="vqiu.cn"
db_user="root"
db_pwd=123456
postfix_db_pwd="postfixadmin"
DB="mysql -u${db_user} -p${db_pwd}"

# Check Root
if [ $(id -u) != "0" ]; then
    echo "Error: Please use root role to install me!"
    exit 1
fi

if [ ! -z "(id -u vmail>/dev/null)" ]; then
	groupadd -g 2000 vmail
	useradd -g 2000 -u 2000 -d /var/vmail -s /sbin/nologin vmail
else
	echo "vmail is exist" 
fi





# Welcome Tips
clear
echo "=======Postfix virtual domain =========="
echo ""
echo "========================================"
echo "       CentOS/RadHat Linux Server       "
echo "========================================"
echo "       Postfix + Dovecot + MySQL        "
echo "========================================"


# Install Base Packages & Dovecot & MySQL
yum -y install gcc gcc-c++ dovecot dovecot-mysql devecot-devel lrzsz mysql-server mysql mysql-devel  

#Setup version
postfix_version=`rpm -qa | grep postfix | cut -c9-13`
dovecot_version=`rpm -qa | grep dovecot | grep -v "mysql" | cut -c9-15`
echo -e "\nPostfix version is: ${postfix_version}"
echo -e "Dovecot version is: ${dovecot_version}"

# System Environment
function sysenviron(){
	# Check Memory
	cat /etc/issue
	uname -a
	MemTotal=`free -m | grep Mem | awk '{print  $2}'`  
	echo -e "\n Memory is: ${MemTotal} MB "
	
	# Set Timezone
#	rm -rf /etc/localtime
#	ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime


	# Set iptables 
	iptables -F
	service iptables stop

	# Update System
	yum -y update

	# Update Date
	if [ ! -e /usr/sbin/ntpdate ];then
		yum install -y ntpdate
	fi
	ntpdate -u pool.ntp.org
	echo "Now time is `date +%F" "%T`"
	# Disable SeLinux
	if [ -s /etc/selinux/config ]; then
	sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
	fi
	setenfore 0
	
	# Setup sshd
	sed -i '/^#UseDNS/s/#UseDNS yes/UseDNS no/g' /etc/ssh/sshd_config
	sed -i 's/#PermitRootLogin yes/PermitRootLogin yes/g' /etc/ssh/sshd_config
	sed -i 's/#PermitEmptyPasswords no/PermitEmptyPasswords no/g' /etc/ssh/sshd_config
	/etc/init.d/sshd restart

	echo "* soft nofile 66666" >> /etc/security/limits.conf  
	echo "* hard nofile 66666" >> /etc/security/limits.conf 
 
 
	# Setup system service
	for server in `chkconfig --list |grep 3:on|awk '{ print $1}'`
	do
	    chkconfig --level 3 $server off
	done
 
	for server in crond network rsyslog sshd postfix dovecot mysqld
	do
	   chkconfig --level 3 $server on
	done

}

# Download configure files
function downloadfiles(){
	echo "============================"
		echo "Download MySQL connection script now..."
		cd /usr/local/src
		mysql_conn="mysql_virtual.tar.gz"
		if [ ! -e ${mysql_conn} ];then
			wget -c http://down.vqiu.cn/conf/Linux_conf/postfix/postfix/${mysql_conn}
			tar zxvf ${mysql_conn} -C /etc/postfix
		fi

		dovecot_conn="dovecot_conf.tar.gz"
		if [ ! -e ${dovecot_conn} ];then
			cd /usr/local/src
			wget -c http://down.vqiu.cn/conf/Linux_conf/postfix/dovecot/${dovecot_conn}
		fi

	echo "============================"
}


# Main configure
function configure(){
	cd /etc/postfix
	mv main.cf main.cf.backup
#	echo "Enter your mail domain, Example(vqiu.cn)"
#	read -p "(Pls input the the domain):" input_domain
	wget -c http://down.vqiu.cn/conf/Linux_conf/postfix/postfix/main.cf
	sed -i "s/myhostname = mail.vqiu.cn/myhostname = ${HOSTNAME}/g" /etc/postfix/main.cf
	sed -i "s/mydomain = vqiu.cn/mydomain = ${mail_domain}/g" /etc/postfix/main.cf
	/etc/init.d/postfix restart

	dovecot_conn="dovecot_conf.tar.gz"
	cd /etc/dovecot
	mkdir backup
	mv conf.d backup
	mv dovecot.conf backup
	tar zxvf /usr/local/src/${dovecot_conn} -C /etc/dovecot
	/etc/init.d/dovecot start
}
# MySQL configure
function mysql_config() {
	# Start mysql service
	sed -i '/mysqld]/a\character-set-server=utf8' /etc/my.cnf
	sed -i '/mysqld]/a\default-character-set=utf8' /etc/my.cnf
	sed -i '1a\[client\]' /etc/my.cnf
	sed -i '/client]/a\default-character-set=utf8' /etc/my.cnf
    chkconfig mysqld on
	/etc/init.d/mysqld restart

	# change mysql password (Default:Empty)
	echo -e '\e[1;35m Please press any key for change mysql password!\e[0m'
	mysqladmin -uroot -p password ${db_pwd} | read -n1

	# Create the postfix database
	$DB -e "create database if not exists postfix";
	$DB -e "grant SELECT ON postfix.* TO postfix@'localhost' identified by '"${postfix_db_pwd}"';"
	$DB -e "flush privileges;"
	echo -e 'MySQL root user password is:\e[1;35m 123456 \e[0m \nPlease remember it!'
	
}

sysenviron
downloadfiles
configure
mysql_config
# End Shell
