#!/bin/bash
#Script to configurate dual omc


#my.cnf and server.conf should be in current directory
#or this script will not work

#Three file will be changed:./server.conf /etc/hosts /etc/my.cnf
#backup them at first if needed
#I will make a backup of /etc/my.cnf for you,path of which is /etc/my.cnf.bak

hostlist="/etc/hosts"

getipbyname()
{
	[ -f $hostlist ] || exit 1
	cmdline='/'$1'/{print $1}'
	echo "awk '"$cmdline"' "$hostlist|bash
}

checkip()
{	
	result=`echo "$1" |grep "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}"`
	if [ -z "$result" ];then
		echo "invalid"
	else	
		result=`ping $1 -c 1 |grep Unreachable`
		if [ -z "$result" ];then
			echo "ok"
		else
			echo "fail"
		fi
	fi
	
}

if [ ! -f "server.conf" ];then 
	echo "./server.conf not found" 
	exit 1
fi
if [ ! -f "my.cnf" ];then
	echo "./my.conf not found"
	exit 1
fi


echo "Please select server mode: (0:Master 1:Slave 2:Single)"
read omcmode

case $omcmode in
0) echo "you select Master"
	omc0=`hostname -s`
	echo "hostname is "$omc0
	
	echo "Input hostname of slave"
	read omc1
	ip=`getipbyname $omc1`
	if [ -z "$ip" ];then
		echo "Can't resolve slave's ip address. You can input it now:"
		read ip

		echo "Check ip address now.pleae wait for a while......"
		rtn=`checkip $ip`
		
		if [ "$rtn" = "invalid" ];then
			echo "Invalid IP!"
			exit 1		
		elif [ "$rtn" = "fail" ];then
			echo 'Dest ip Unreachable! I(gnore)/E(xit)'
			read act
			case $act in
				"I") echo "continue......"
					;;
				*) exit 1
					;;
			esac
		fi
		echo $ip' '$omc1'.iwv '$omc1>>$hostlist
	fi
	echo "ip of slave is "$ip
	mysql_id=1;
	mysql_master=$omc1
	;;
1) echo "you select slave"
	omc1=`hostname -s`
	echo "hostname is "$omc1
	echo "Input hostname of master"
	read omc0
	ip=`getipbyname $omc0`
    if [ -z $ip ];then
        echo "Can't resolve slave's ip address. You can input it now:"
        read ip
        rtn=`checkip $ip`
        echo $rtn
        if [ "$rtn" = "fail" ];then
            echo "Dest ip Unreachable! I(gnore)/E(xit)"
            read act
            case $act in
                "I") echo "continue......"
                    ;;
                *) exit 1
                    ;;
            esac
        fi
        echo $ip' '$omc0'.iwv '$omc0>>$hostlist
    fi
    echo "ip of slave is "$ip
	mysql_id=2;
	mysql_master=$omc0
	;;
2) echo "you select single,nothing should be done"
		exit 1
	;;
*) echo "invalid mode"
	exit 1
	;;
esac

echo "configurate server.conf......"

echo "#!/bin/sed -f" > omcconf.sed
echo "/omc_slave/ c\\" >> omcconf.sed
echo "omc_slave = "$omc1>>omcconf.sed

echo "/mysql_slave/ c\\" >> omcconf.sed
echo "mysql_slave = "$omc1>>omcconf.sed

echo "/omc_master/ c\\" >> omcconf.sed
echo "omc_master = "$omc0>>omcconf.sed

echo "/mysql_master/ c\\" >> omcconf.sed
echo "mysql_master = "$omc0>>omcconf.sed

chmod 777 omcconf.sed
omcconf.sed server.conf>server.conf.tmp
rm -f omcconf.sed
mv server.conf.tmp server.conf

echo "configurate /etc/my.cnf......"

echo "#!/bin/sed -f" > myconf.sed
echo "/master-host/ c\\" >> myconf.sed
echo "master-host = "$mysql_master >>myconf.sed

echo "/server-id/ c\\" >> myconf.sed
echo "server-id = "$mysql_id >>myconf.sed

mv -f /etc/my.cnf /etc/my.cnf.bak

chmod 777 myconf.sed
myconf.sed my.cnf > /etc/my.cnf
rm -f myconf.sed

/etc/rc.d/init.d/mysqld stop
/etc/rc.d/init.d/mysqld start

