#!/bin/bash
#Script to configurate dual omc
#create by Liang Hanxi 2002/8/24
#changed by hewd 2002/09/16

#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

if [ `uname -r |grep -c '2.4.7-10'` -eq 1 ] ; then
	mysqllib_dir="/usr/local/mysql/lib"
else
	mysqllib_dir=/usr/lib/mysql
fi
omcBin="/usr/local/omc/bin"

# by simon,for ubuntu 22.04
#mysql_user=`/usr/local/mssBak/script/encryption 1 "6266706d736f7a7c7b6b7f7b7f"`
#mysql_pw=`/usr/local/mssBak/script/encryption 1 "2b3a392757557b697b7e7e757b777c"`



arg_1=$1

##################################################################
######  Function
##################################################################
chkHostName()
{
	thisHostName=`hostname -s`
	
	if [ `echo "$thisHostName"|grep -c '\-[01]$'` -eq 1 ] ;then
		if [ `echo "$thisHostName"|grep -c '\-0$'` -eq 1 ] ;then
			echo "This is Master server!!"
		else
			echo "This is Slave server!!"
		fi
	else
		echo "The server's hostname must be configured like following[x is the any displayable ASCII character]"
		echo "xxx-0 or xxx-1"
		sleep 3
                exit 1
	fi	



	#if [ "$thisHostName" != "omc-0" ] && [ "$thisHostName" != "omc-1" ] ; then
	#	echo "This server's hostname is $thisHostName !!"
	#	echo "It must be \"omc-0\" or  \"omc-1\" !! Please set it."
	#	sleep 3
	#	exit 1
	#else
	#	if [ "$thisHostName" == "omc-0" ] ; then
	#		echo "This is Master server!!"
	#	else
	#		echo "This is Slave server!!"
	#	fi
	#fi
}

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 "icmp_seq="`
		if [ -z "$result" ];then
			echo "fail"
		else
			echo "ok"
		fi
	fi
}

getipbyname()
{
	hostlist="/etc/hosts"

	[ -f $hostlist ] || exit 1

	cmdline='/'$1'/{print $1}'
	awkcmd="awk '$cmdline' $hostlist"
	ip=`echo $awkcmd|bash`
	
	if [ -z "$ip" ];then
		echo "Can't resolve ip address of host. 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' '$1'.iwv '$1>>$hostlist
	fi
}

replaceValue()
{
	tmpFileName=${1}_tmp
	bakFileName==${1}_bak
	if [ `grep -c $2 $1` -gt 0 ] ; then
		sed -e 's/$2/$3/' $1 > $tmpFileName
		mv $1 $bakFileName
		rm $1
		mv $tmpFileName $1
	fi
}

##################################################################
cd $omcBin
#check hostname,must =xxx-0 or xxx-1
chkHostName ""

./changehostname

if [ "$arg_1" == "-auto" ] ; then
	runMode=2
else
	echo "+-------Running Mode------+" 
	echo "| 1.Single EMS            |" 
	echo "| 2.Dual EMS              |" 
	echo "+-------------------------+" 
	echo -n "Please select EMS running mode(1/2):" 
	read runMode
	echo ""
fi

case "$runMode" in
	'1')
		echo "You have selected 'Single EMS' mode"
		echo ""
		#my.conf
		hostName='single'
		#mv -f /etc/my.cnf /etc/my.cnf_bak 
		cp -f ../config/mariadb-50-server.cnf /etc/mysql/mariadb.conf.d/50-server.cnf
		chmod 644 /etc/mysql/mariadb.conf.d/50-server.cnf
		echo "set configuration file: /etc/mysql/mariadb.conf.d/50-server.cnf ok!"

		#hosts
		#mv /etc/hosts  /etc/hosts_bak 
		#cp ../config/hosts /etc/hosts
		#chmod 644 /etc/hosts
		#echo "set config file: /etc/hosts ok!"

		#server.conf
		mv ./conf/server.conf ./conf/server.conf_bak
		cp ../config/server.conf ./conf/server.conf
		chmod 644 ./conf/server.conf
		echo "set configuration file: ./conf/server.conf ok!"

		#omcd.conf
		mv ./conf/omcd.conf ./conf/omcd.conf_bak
		cp ../config/omcd.conf_$hostName ./conf/omcd.conf
		chmod 644 ./conf/omcd.conf
		echo "set configuration file: ./conf/omcd.conf ok!"
	;;
	'2')
		echo "You have selected 'Dual EMS' mode"
		echo ""

		#reset database sync
		#/usr/local/mysql/bin/mysql -u$mysql_user -p$mysql_pw <<_EOF_
		#	reset master;
		#	reset slave;
		#_EOF_

		#get hostname
		hostName='single'
			
		#my.conf		
		mv -f /etc/my.cnf /etc/my.cnf_bak 
		cp -f ../config/mariadb-50-server.cnf /etc/mysql/mariadb.conf.d/50-server.cnf
		chmod 644 /etc/mysql/mariadb.conf.d/50-server.cnf
		echo "set configuration file: /etc/mysql/mariadb.conf.d/50-server.cnf ok!"

		#hosts
		#mv /etc/hosts  /etc/hosts_bak 
		#cp ../config/hosts /etc/hosts
		#chmod 644 /etc/hosts
		#echo "set config file: /etc/hosts ok!"

		#server.conf
		mv ./conf/server.conf ./conf/server.conf_bak
		cp ../config/server.conf ./conf/server.conf
		chmod 644 ./conf/server.conf
		echo "set configuration file: ./conf/server.conf ok!"

		#omcd.conf
		hostName=`hostname -s|sed 's/^.*-/omc-/'`
		mv ./conf/omcd.conf ./conf/omcd.conf_bak
		cp ../config/omcd.conf_$hostName ./conf/omcd.conf
		chmod 644 ./conf/omcd.conf
		echo "set configuration file: ./conf/omcd.conf ok!"
	;;
	*)
		echo "Invalid input,Exited."
		exit
	;;
esac

#reset database sync
if [ `uname -r |grep -c '2.4.7-10'` -eq 1 ] ; then
	mysqlBinDir=/usr/local/mysql/bin
else
	mysqlBinDir=/usr/bin
fi
# by simon,for ubuntu 22.04
#$mysqlBinDir/mysql -u$mysql_user -p$mysql_pw <<_EOF_
#	slave stop;
#	reset master;
#	reset slave;
#_EOF_
initDir=/etc/init.d
service mysql stop
if [ -n "`pidof mysqld`" ] ;then
        kill -9 `pidof mysqld`
fi
service mysql start

if [ "$2" == "install" ] ; then
	echo " "
else
	serivce omcd restart
fi

exit 1

##############################
#echo "configurate /etc/my.cnf......"
#cp ../config/my.cnf_

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

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

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

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