#!/bin/bash
PATH=$PATH
export PATH

RUN="bash"
WRKDIR=`pwd`
UNZIP="unzip"
UNAME=`uname -s`
ROOT_ID=`id -u`

USER_ADD=/usr/sbin/useradd
USER_DEL=/usr/sbin/userdel
mysqld_user="mysql"
mysqld_group="mysql"

MYSQL_USER="root"		
MYSQL_PASSWORD=""
MYSQL_PORT="3306"
MYSQL_HOSTNAME="localhost"
SGDATABASE="SGDatabase"
MYSQL_DATA_DIRECTORY="/var/lib/mysql"
MYSQL_DEBUNDENT=0
FRESH_DATABASE=0

MYSQL_VERSION="5.5.20"
MYSQL51_VERSION="5.1.39"
UNIXODBC_VERSION="2.2.14-1"
MYSQL_CONNECTOR_VERSION="3.51.27"
MYSQL_RH_VERSION="${MYSQL51_VERSION}-0"
MYSQL_DBN_VERSION="${MYSQL51_VERSION}-1"

MYSQL55_DBN_VERSION="${MYSQL_VERSION}"
MYSQL55_RH_VERSION="${MYSQL_VERSION}"


MYSQL5540_VERSION="5.5.40"
MYSQL5540_DBN_VERSION="${MYSQL5540_VERSION}" 

RH_MYSQL_SERVER="MySQL-server-5.1.39-0.glibc23"
RH_MYSQL_CLIENT="MySQL-client-5.1.39-0.glibc23"
RH_UNIXODBC="unixODBC-2.2.14-1"

RH_MYSQL_SERVER55="MySQL-server-5.5.20-1.linux2.6"
RH_MYSQL_CLIENT55="MySQL-client-5.5.20-1.linux2.6"

RH_MYSQL_SERVER_ARCH="MySQL-server-5.1.39-0.glibc23.i386"
RH_MYSQL_CLIENT_ARCH="MySQL-client-5.1.39-0.glibc23.i386"
RH_UNIXODBC_ARCH="unixODBC-2.2.14-1.i386"

RH_MYSQL_SERVER55_ARCH="MySQL-server-5.5.20-1.linux2.6.i386"
RH_MYSQL_CLIENT55_ARCH="MySQL-client-5.5.20-1.linux2.6.i386"

RH_MYSQL_SERVER_ARCH64="MySQL-server-5.1.39-0.glibc23.x86_64"
RH_MYSQL_CLIENT_ARCH64="MySQL-client-5.1.39-0.glibc23.x86_64"

RH_MYSQL_SERVER55_ARCH64="MySQL-server-5.5.20-1.linux2.6.x86_64"
RH_MYSQL_CLIENT55_ARCH64="MySQL-client-5.5.20-1.linux2.6.x86_64"

MYSQL_SCRIPT_LOCATION=`pwd`
MYSQL_SOCKET="/tmp/mysql.sock"
MYSQL_DOWNLOADS="mysql-downloads"
LOGFILE="$MYSQL_SCRIPT_LOCATION/mysqlInstall.log"
BUILD_INFO_FILE="SGSPLin_263.info"

mysql_server_status=0
mysql_client_status=0
mysql_connector_status=0
unixodbc_status=0
all_packages_installed=0

SIMPLE_ONLINE_INSTALLER=1

STOREGRID_DOWNLOADS="vembubdr-downloads"
export storegrid
SGVN="3.6.0"

MACHINE_ARC="`/usr/bin/getconf LONG_BIT`"

if [ `/usr/bin/getconf LONG_BIT` -ne 64 ]; then
	echo "Installation on 32 bit machine is not supported"
	exit 1
fi

SERVICE_DIR=/etc/init.d

#checking Vembu OffsiteDR installed or not
if [ -f ${SERVICE_DIR}/vembuoffsitedr ]; then
 tput setaf 2
 echo "Unable to proceed Vembu BDR installation. Seems Vembu OffsiteDR is already installed in this machine."
  tput sgr0
  exit 1
fi

HEADER_URL="http://sg-build-release.s3.amazonaws.com/products/vembu/vembuBDR/pro/3602016101922/Linux/OnlineInstaller/Server/Xml/${BUILD_INFO_FILE}"
#HEADER_URL="http://192.168.100.125/builds/Automation/Linux_Online_Installer/Linux/${BUILD_INFO_FILE}"
#HEADER_URL="http://10.0.0.125/builds/kamesh/Online_Installer/Linux/${BUILD_INFO_FILE}"

download_source="storegrid.com"

Exit()
{
	echo -n " Exiting "
	i=3
	while [ $i -gt 0 ]; do
		echo -n "."
		sleep 1
		i=`expr $i - 1`
	done
	echo
	echo
	exit 
}

log_success_msg()
{
	echo " SUCCESS! $@"
}

log_failure_msg()
{
	echo " ERROR! $@"
}

if [ ${ROOT_ID} -ne 0 ]; then
	echo
   	echo " $0 : you must be root to do that!."
   	Exit
fi

if [ -f "${WRKDIR}/SGLoc.conf" ]; then
	rm -f ${WRKDIR}/SGLoc.conf 1> /dev/null 2> /dev/null
fi

preparing_installs()
{
	if [ $UNAME = "FreeBSD" ]; then
		echo 
	else
		if [ -f /etc/debian_version ]; then
			query="dpkg -S"
			install="dpkg -i --force-all"
			depinstall="dpkg -iE --force-all"
			uninstall="dpkg -r"
			purge="dpkg --purge"
		else
			query="rpm -q"
			install="rpm -ivh "
			depinstall="rpm -ivh --nodeps"
			uninstall="rpm -ev --nodeps"
		fi

		WGET=`/usr/bin/whereis wget |grep -w wget |cut -d":" -f2`
		if [ "$WGET" = "" ]; then
			echo "bash: wget: command not found. Please install it and try again."
			echo 
			Exit
		fi

		cd ${MYSQL_DOWNLOADS} >> /dev/null 2>> /dev/null 
		if [ ! -f "${BUILD_INFO_FILE}" ]; then
			echo " " >> ${LOGFILE}
	  		wget -c ${HEADER_URL} -a ${LOGFILE} 
		else
			rm -f ${BUILD_INFO_FILE} >> /dev/null 
			echo " " >> ${LOGFILE}
	  		wget -c ${HEADER_URL} -a ${LOGFILE} 
		fi	
		cd ${WRKDIR}
	fi
}

Welcome()
{
	clear
	if [ "$1" = "" ]; then
        	echo "		#######################################################"
        	if [ -f /etc/lsb-release ] && [ "`egrep -i "SUSE" /proc/version`"  = "" ]; then
                	echo     "                     \033[33mWELCOME TO THE VEMBUBDR  SERVER INSTALLATION \033[0m         "
        	else
                	echo  -e "                     \033[33mWELCOME TO THE VEMBUBDR  SERVER INSTALLATION \033[0m         "
        	fi
        	echo "		#######################################################"
		echo
		echo "This installer will download and install,"
		echo
		echo " 1. MySQL RDBMS (5.5.40)"
		echo " 2. MySQL Connector (3.51.27)"
		echo " 3. ODBC driver (2.2.14)"
		echo " 4. MongoDB (3.2.0)"
		if [ ${SIMPLE_ONLINE_INSTALLER} -eq 1 ]; then
			echo " 5. VembuBDR  Server ($SGVN)"
		fi
		

		#if [ -f /etc/debian_version ] || [ -f /etc/mandrake-release ]; then
		#	echo ""
		#else
			if [ ${SIMPLE_ONLINE_INSTALLER} -eq 1 ]; then
				echo " 6. If SELinux is enabled, it will be set to Permissive Mode. This change is needed for MySQL Server to run as a service."
			else
				echo " 6. If SELinux is enabled, it will be set to Permissive Mode. This change is needed for MySQL Server to run as a service."
			fi
		#fi
		echo
		echo "####################################################################################################################"
        	echo
	fi

	
	while true; do
		echo -n "Do you want to proceed [y/n]: "
		read proceed
	
		case ${proceed} in
			[yY]|[yY][eE][sS])
					break;;
			[nN]|[nN][oO])
						if [ -d ${MYSQL_DOWNLOADS} ]; then
							cd ${MYSQL_DOWNLOADS}
							if [ -f "${BUILD_INFO_FILE}" ]; then
								echo " " >> $LOGFILE
								echo " Deleting previous build information file \"${BUILD_INFO_FILE}\".">> $LOGFILE
								rm -f ${BUILD_INFO_FILE} 2>> LOGFILE
							fi
						fi
						cd ${WRKDIR}
						Exit;;
			*)
						echo "Please enter [y/n] to procced with the installation."
						continue;;
		esac
	done		      		
}

MySQL_Disclaimer()
{
	if [ -f /etc/lsb-release ]  && [ "`egrep -i "SUSE" /proc/version`"  = "" ]; then
		echo "####################################################################################################################"
		echo 	" 		\033[33mAbout Compatible Relational Database Management System [RDBMS]\033[0m			  "
		echo "####################################################################################################################"
		echo				                                  
		echo 	"\033[1mMySQL Community Server\033[0m is a free software that is licensed by Sun Microsystems Inc., or its successors, under the \033[1mGNU General Public License version 2\033[0m. A link to the copy of that license is available below. You can find the link to a copy of the complete corresponding source code below. Do not install  \033[1mMySQL Community Server\033[0m , if the terms of the said license are not acceptable to you."              
		echo
		echo    "\033[1mMySQL Connector/ODBC driver\033[0m is a free software that is licensed by Sun Microsystems Inc., or its successors, under the \033[1mGNU General Public License version 2\033[0m. A link to the copy of that license is available below. You can find the link to a copy of the complete corresponding source code below. Do not install \033[1mMySQL Connector/ODBC driver\033[0m, if the terms of the said license are not acceptable to you."
		echo			                                  
		echo    "GNU General Public License v2 \033[1;34m<http://www.gnu.org/licenses/gpl-2.0.html>\033[0m"
		echo			                                  
		echo    "MySQL Community Server source code \033[1;34m <http://downloads.mysql.com/archives.php?p=mysql-5.1&v=${MYSQL_VERSION}>\033[0m"
		echo				                                  
		echo    "If you would like to download and install \033[1mVembuBDR  Server\033[0m and \033[1mMySQL Community Server\033[0m and \033[1mMySQL Connector/ODBC driver\033[0m, provide input 'yes' and proceed with \033[1mVembuBDR  Server\033[0m and \033[1mMySQL Community Server\033[0m and \033[1mMySQL Connector/ODBC driver\033[0m download and installation."
		echo				                                  
		echo "####################################################################################################################"
	else
		echo "####################################################################################################################"
		echo -e " 		\033[33mAbout Compatible Relational Database Management System [RDBMS]\033[0m			  "
		echo "####################################################################################################################"
		echo				                                  
		echo -e "\033[1mMySQL Community Server\033[0m is a free software that is licensed by Sun Microsystems Inc., or its successors, under the \033[1mGNU General Public License version 2\033[0m. A link to the copy of that license is available below. You can find the link to a copy of the complete corresponding source code below. Do not install  \033[1mMySQL Community Server\033[0m , if the terms of the said license are not acceptable to you."              
		echo
		echo -e "\033[1mMySQL Connector/ODBC driver\033[0m is a free software that is licensed by Sun Microsystems Inc., or its successors, under the \033[1mGNU General Public License version 2\033[0m. A link to the copy of that license is available below. You can find the link to a copy of the complete corresponding source code below. Do not install \033[1mMySQL Connector/ODBC driver\033[0m, if the terms of the said license are not acceptable to you."
		echo			                                  
		echo -e "GNU General Public License v2 \033[1;34m<http://www.gnu.org/licenses/gpl-2.0.html>\033[0m"
		echo			                                  
		echo -e "MySQL Community Server source code \033[1;34m <http://downloads.mysql.com/archives.php?p=mysql-5.1&v=${MYSQL_VERSION}>\033[0m"
		echo				                                  
		echo "####################################################################################################################"
		echo
	fi

	while true; do
	echo -n "Do you want to proceed [y/n]: "
	read proceed
	
		case ${proceed} in
			[yY]|[yY][eE][sS])
					if [ ! -d "${MYSQL_DOWNLOADS}" ]; then
						mkdir -p ${MYSQL_DOWNLOADS}
					fi
					preparing_installs
					break;;
			[nN]|[nN][oO])
						if [ -d ${MYSQL_DOWNLOADS} ]; then
							cd ${MYSQL_DOWNLOADS}
							if [ -f "${BUILD_INFO_FILE}" ]; then
								echo " " >> $LOGFILE
								echo " Deleting previous build information file \"${BUILD_INFO_FILE}\".">> $LOGFILE
								rm -f ${BUILD_INFO_FILE} 2>> LOGFILE
							fi
						fi
						cd ${WRKDIR}
						Exit;;
						*)
						echo "Please enter [y/n] to procced with the installation."
						continue;;
		esac
	done		 

}

SE_Linux_Settings()
{
	if [ ! -f /etc/debian_version ]; then
		SELINUX=`/usr/sbin/sestatus |grep -w "SELinux status" |cut -d":" -f2`
		if [ $SELINUX = "enabled" ]; then
			echo
			echo " " >> ${LOGFILE}	
			/usr/sbin/sestatus -b |grep mysql >> ${LOGFILE} 
			echo " " >> ${LOGFILE} 
			#/usr/sbin/setsebool -P mysqld_disable_trans=1 allow_user_mysql_connect=1 >> ${LOGFILE}
			
			if [ -f /selinux/enforce ]; then
			  echo 0 > /selinux/enforce 2>> /dev/null
			else 
			  if [ -f /usr/sbin/setenforce ]; then
			    /usr/sbin/setenforce 0
			  fi
			fi

			sed "s/SELINUX=enforcing/SELINUX=Permissive/g" /etc/selinux/config > /etc/selinux/config.temp 2>> $LOGFILE
			if [ -f /etc/selinux/config.temp ]; then
				SELINUX=`cat /etc/selinux/config.temp | grep SELINUX= | grep -v "#" | cut -d "=" -f2` 2>> $LOGFILE
				if [ $SELINUX = "Permissive" ]; then
					mv -f /etc/selinux/config /etc/selinux/config.save 2>> $LOGFILE
					mv -f /etc/selinux/config.temp /etc/selinux/config 2>> $LOGFILE
				fi
			fi
			echo " " >> ${LOGFILE}
			/usr/sbin/sestatus -b |grep mysql >> ${LOGFILE}
			echo 
		fi
	fi
}
env_var_set()
{
	if [ -d /opt/mysql/server-5.5/bin/ ]; then
		export PATH=$PATH:/opt/mysql/server-5.5/bin
	fi
}

mysql_server_create_database()
{
	echo
	echo "*************************************************************"
	echo " Creating backup or replication server database 		       "
	echo "*************************************************************"	
	if [ "${MYSQL_PASSWORD}" = "" ]; then
		stty -echo
		echo -n 1>&2 " Enter MySQL server 'root' user password : "		
		while true; do
			read mysqlpwd
			stty echo
			echo "use mysql;" > mysql_query.sql
			mysql -u root -p$mysqlpwd < mysql_query.sql
			if [ $? -ne 0 ]; then
				echo
				stty -echo
				echo -n 1>&2 " Please enter the right password : "
				continue;
			else
				rm -f mysql_query.sql 1> /dev/null 2> /dev/null
				echo 
				break;
			fi
		done
	else
		mysqlpwd=${MYSQL_PASSWORD}
	fi
	echo "use $SGDATABASE;" > mysql_query.sql
	while true; do
		/usr/bin/mysql -u root -p$mysqlpwd < mysql_query.sql 2>> ${LOGFILE}
		if [ $? -eq 0 ]; then
			echo
			echo " Default database \"$SGDATABASE\" already exists."
			echo -n " Do you want to create a new database for the server? [y/n]: "
			read proceed
			case ${proceed} in
				[yY]|[yY][eE][sS])
							echo -n " Enter the Database name : "
							read dbName
							dbName_len=`echo -n $dbName | wc -c`
							if [ $dbName_len -eq 0 ]; then
								echo " Database name cannot be empty."
							else
								echo "use $dbName;" > mysql_query.sql
								SGDATABASE="$dbName"
							fi										
							continue;;
					[nN]|[nN][oO])
							echo
							break;;
							*)
							echo
							echo -n " Please enter [y/n] : "
							continue;;
			esac
		else
			echo "create database $SGDATABASE collate latin1_general_cs;" > mysql_query.sql
			echo "GRANT ALL PRIVILEGES ON *.* TO root@'`hostname`' IDENTIFIED BY "$mysqlpwd";" >> mysql_query.sql

			/usr/bin/mysql -u root -p$mysqlpwd < mysql_query.sql 2> ${LOGFILE}
			echo
			echo " Database \"$SGDATABASE\" is successfully created."
			export SGDATABASE
			FRESH_DATABASE=1
			export FRESH_DATABASE
			echo
			break;
			rm -f mysql_query.sql 1> /dev/null 2> /dev/null
		fi
	done
	rm -f mysql_query.sql 1> /dev/null 2> /dev/null
}

mysql_configuration_details()
{
	if [ -f /etc/lsb-release ]  && [ "`egrep -i "SUSE" /proc/version`"  = "" ]; then
		echo "\033[1;2mMySQL Configurtion Details : \033[0m"
	else
		echo -e "\033[1;2mMySQL Configurtion Details : \033[0m"
	fi
	echo "data-directory	: $MYSQL_DATA_DIRECTORY"
	echo "hostname	: $MYSQL_HOSTNAME"
	echo "userName	: $MYSQL_USER"		
	echo "password	: $MYSQL_PASSWORD"
	echo "port		: $MYSQL_PORT"
	echo "socket		: $MYSQL_SOCKET"
}

mysql_server_password_setting()
{
	echo
	echo "*************************************************************"
	echo "	Setting MySQL password for 'root' user 			           " 
	echo "*************************************************************"	
	while true; do
		echo -n 1>&2 " Enter password: "
		stty -echo
		read pswd
		stty echo
		pwd_length=`echo -n $pswd | wc -c`
		if [ $pwd_length -eq 0 ];then
			echo
			echo " Password cannot be empty."
			echo
			continue
		fi
		echo
		echo
		echo -n 1>&2 " Confirm password: "
		stty -echo
		read cpswd
		stty echo
		if [ "$pswd" != "$cpswd" ];then
			echo
			echo " Password mismatch. Try again."
			echo	
			continue
		fi
# 		if [ ${MYSQL_DEBUNDENT} -eq 1 ]; then
		if [ -f /etc/debian_version ]; then
			/opt/mysql/server-5.5/bin/mysqladmin -u root password $pswd 2> mysqlpwd.log 
		else		
			/usr/bin/mysqladmin -u root password $pswd 2> mysqlpwd.log 
		fi
		if [ $? -ne 0 ];then
			echo
			create=`cat mysqlpwd.log |grep -w "using password: NO"`
			if [ $? -eq 0 ]; then
				echo " Warning : MySQL password was already set for 'root' user."
				echo
			else	
				create=`cat mysqlpwd.log |grep -w "mysql.sock"`
				if [ $? -eq 0 ]; then
					echo " Warning : MySQL Server is not running. Please start it and then try again."
					echo " "
					Exit			
				fi
			fi		
			break
		else
			MYSQL_PASSWORD="$pswd"
			export MYSQL_PASSWORD
			echo
			echo " MySQL 'root' user password has been successfully set."
			echo
			echo "*************************************************************"
			mysql_configuration_details
			echo "*************************************************************"
			echo
			break
		fi
# 		else
# 			echo 
# 			echo
# 			echo " MySQL-server installation settings are completed and server will start now.."
# 			echo " Please be patient. It could take up to 1 minute."
# 			echo
# 			echo "UPDATE mysql.user SET Password=PASSWORD('$pswd') WHERE User='root';" > ${WRKDIR}/mysqlpwd.log
# 			echo "FLUSH PRIVILEGES;" >> ${WRKDIR}/mysqlpwd.log
# 			/usr/bin/mysqld_safe --init-file=${WRKDIR}/mysqlpwd.log >/dev/null 2>&1 &
# 			echo -n " Starting MySQL "
# 			i=45
# 			while [ $i -gt 0 ]; do
# 				echo -n "."
# 				sleep 1
# 				i=`expr $i - 1`
# 			done
# 			libexecdir="/usr/sbin"
# 			SERVER_PID_FILE=$MYSQL_DATA_DIRECTORY/`/bin/hostname`.pid
# 			ps ax | grep "$libexecdir/mysqld" | awk '"mysqld" && !/grep/ {print $1}' > ${SERVER_PID_FILE}
# 			if [ -s "$SERVER_PID_FILE" ]; then
# 				read mysqld_pid < $SERVER_PID_FILE
#      			if kill -0 $mysqld_pid 2>/dev/null ; then
# 					log_success_msg " MySQL running ($mysqld_pid)"
#        				echo
# 					echo " MySQL 'root' user password has been successfully set."
# 					echo
# 					MYSQL_PASSWORD="$pswd"
# 					export MYSQL_PASSWORD
# 					echo
# 					echo "*************************************************************"
# 					mysql_configuration_details
# 					echo "*************************************************************"
# 					echo
# 					break
#       			else
# 					echo
#       			    log_failure_msg " MySQL is not running."
# 					SERVER_ERR_FILE=$MYSQL_DATA_DIRECTORY/`/bin/hostname`.err
# 					echo -n " Please check the MySQL error log in \"$SERVER_ERR_FILE\" file and try to start"
# 					echo " MySQL server by executing \"/etc/init.d/mysql start\ as a root user."
# 					echo
# 					Exit 
#         		fi
# 			fi
# 		fi
 	done
	rm -f mysqlpwd.log 1> /dev/null 2>/dev/null 		
}

mysql_server_change_password()
{
	echo
	echo "*************************************************************"
	echo "	Changing MySQL password for 'root' user		               "
	echo "*************************************************************"
	stty -echo
	echo -n 1>&2 " Current password: "
	read oldpwd
	stty echo
	while true ; do
		old_pwd_len=`echo -n $oldpwd | wc -c`
		if [ $old_pwd_len -eq 0 ];then
			echo
			echo " Password must not be empty"
			echo
			continue
		else
			echo "use mysql;" > mysql_query.sql
			mysql -u root -p$oldpwd < mysql_query.sql
			if [ $? -ne 0 ]; then
				echo
				stty -echo
				echo -n 1>&2 " Please provide the right password : "
				read oldpwd
				stty echo
				continue;
			else
				rm -f mysql_query.sql 1> /dev/null 2> /dev/null
				echo 
				break;
			fi
		fi
	done
	
	while true ; do
		echo
		echo -n 1>&2 " New password: "
		stty -echo
		read newpwd
		stty echo
		new_pwd_len=`echo -n $newpwd | wc -c`
		if [ $new_pwd_len -eq 0 ];then
			echo
			echo " Password cannot be empty."
			echo
			continue
		fi
		if [ $oldpwd = $newpwd ];then
			echo
			echo " The new password should be different from the Current password."
			echo
			continue
		fi	
		echo
		echo -n 1>&2 " confirm new password: "
		stty -echo
		read newcpwd
		stty echo
		if [ "$newpwd" != "$newcpwd" ];then
			echo
			echo " Confirmation Failed : new password mismatch."
			echo
			continue
		else
			break;
		fi
	done

	echo
	/usr/bin/mysqladmin -u root -p$oldpwd password $newpwd 1> /dev/null
	if [ $? -ne 0 ];then
		echo
		if [ -f "/etc/debain_version" ]; then
			echo "\033[31m Error occured while updating mysql password due to the following reason: "
			echo " -> The entered MySQL 'root' user's current password is wrong.\033[0m"
		else
			echo -e "\033[31m Error occured while updating mysql password due to one of the following reasons: "
			echo -e " -> MySQL server is not running or"
			echo -e	" -> the entered MySQL 'root' user's current password is wrong.\033[0m"
		fi
		echo	
	else
		echo
		echo " MySQL password for 'root' user is successfully updated."
		echo
	fi
}

mysql_server_drop_database()
{
	echo
	echo "*************************************************************"
	echo "	Drop backup or replication server database 		           "
	echo "*************************************************************"
	while true ; do
		echo -n " Enter database name : "
		read dbName 
		db_strlen=`echo $dbName | wc -c`
		if [ $db_strlen -eq 0 ];then
			echo
			echo " Database name must not be empty."
			continue
		else
			break;
		fi
	done
	
	while true; do
		echo -n 1>&2 " Enter mysql 'root' user password : "
		stty -echo
		read dbpwd
		stty echo
		mysql_pwd_len=`echo $dbpwd | wc -c`
		if [ $mysql_pwd_len -eq 0 ];then
			echo
			echo " Password cannot be empty."
			continue
		else
			break;
		fi
	done
	
	while true; do
		echo -n " Are you sure want to drop \"$dbName\" database? [y/n] : "
		read choice
		case ${choice} in 
			[yY]|[yY][eE][sS])
				echo " drop database $dbName;" > mysql_query.sql
				/usr/bin/mysql -u root -p$dbpwd < mysql_query.sql 
				if [ $? -ne 0 ];then
					echo
					echo -e "\033[31m Error in dropping database due to one of the following reasons: "
					echo -e "............................................."	
					echo -e " -> MySQL server is not yet started. [OR]"
					echo -e " -> Password entered was wrong. [OR]"
					echo -e " -> Database doesnt exist."
					echo -e ".............................................\033[0m"	
					echo
				else
					echo " "
					echo " SuccessFully dropped database."
					echo " "
				fi
				rm -f mysql_query.sql 1> /dev/null 2> /dev/null
				break;;
			[nN]|[nN][oO])
				rm -f mysql_query.sql 1> /dev/null 2> /dev/null
				break;;
			*)
				echo " Please enter [y/n] to proceed."
				;;
		esac	
	done
	rm -f mysql_query.sql 1> /dev/null 2> /dev/null
}

download_odbc_zip()
{
	if [ -f "${BUILD_INFO_FILE}" ]; then
		odbcURL=`grep -rni "odbcInfo" ${BUILD_INFO_FILE} |cut -d"=" -f2`
		if [ "${odbcURL}" = "" ]; then
			echo " ERROR: Failed to retrieve odbc header information. Please try again..."
			Exit
		fi
		wget -c ${odbcURL} -a ${LOGFILE}
		"${UNZIP}" -o ODBCInformation.zip >> ${LOGFILE}
		if [ "$?" -gt 1 ]; then
			echo
			echo " Unable to extract odbc files. Please verify the downloaded file."
			echo
			Exit
		fi
	fi
		
}

odbc_configuration()
{
	echo
	echo "****************************************************************"
	echo " Test Connectivity between \"ODBC-driver\" and \"MySQL-Server\" "
	echo "****************************************************************"
	cd ${WRKDIR}/${MYSQL_DOWNLOADS}
	if [ ! -f "ODBCInformation.zip" ]; then
		download_odbc_zip
	else
		"${UNZIP}" -o ODBCInformation.zip 1> /dev/null 2> /dev/null
		if [ "$?" -gt 1 ]; then
			rm -f ODBCInformation.zip 1> /dev/null 2> /dev/null
			download_odbc_zip
		fi
	fi

	# copying odbcinst.ini information
	if [ ! -s /etc/odbcinst.ini ]; then	
		rm -f /etc/odbcinst.ini >> /dev/null
		cp -f odbc/odbcinst.ini /etc/odbcinst.ini >> /dev/null
	else
		if [ -f /etc/odbcinst.ini ] && [ ! -f /etc/odbcinst.ini.sg ]; then
			cp -f /etc/odbcinst.ini /etc/odbcinst.ini.sg >> /dev/null
			cp -f odbc/odbcinst.ini /etc/odbcinst.ini
		else
			cp -f odbc/odbcinst.ini /etc/odbcinst.ini >> /dev/null
		fi
	fi

	# copying odbc.ini information
	while true ; do
		if [ -s /etc/odbc.ini ]; then
			cp -f /etc/odbc.ini /etc/odbc.ini.sg
		else
			cp -f odbc/odbc.ini /etc/odbc.ini >> /dev/null
		fi

		if [ -s "/etc/odbc.ini" ]; then
			odbc_database=`cat /etc/odbc.ini |grep -w "DATABASE" |cut -d"=" -f2 |tr -d " "`
			if [ "${odbc_database}" = "SGDB" ] || [ ${FRESH_DATABASE} -eq 1 ] ; then
				sed "s/${odbc_database}/${SGDATABASE}/g" /etc/odbc.ini > /etc/odbc.ini.temp	
				mv /etc/odbc.ini.temp /etc/odbc.ini
			fi
		fi
		
		env_var_set
		# Checking Test Connectivity between odbc-driver and MySQL server
		if [ "${MYSQL_PASSWORD}" = "" ]; then
			echo " Please enter MySQL 'root' password to connect to the MySQL Server"
			echo
			pwd_count=1
			while true; do
				echo -n 1>&2 " Enter MySQL password : "
				stty -echo
				read mysql_passwd
				stty echo
				MYSQL_PWD_LEN=`echo -n $mysql_passwd | wc -c`
				if [ $MYSQL_PWD_LEN -eq 0 ]; then
					echo ""
					echo " Password must not be empty."
					echo ""
					continue
				else
					# Validate  mysql password
					echo "\q" > mysql_query.sql	
					mysql -u root -p$mysql_passwd < mysql_query.sql 2>> /dev/null
					if [ $? -ne 0 ]; then
						echo " Access denied : You have entered a wrong password for MySQL 'root' User. Please try again."
						pwd_count=`expr $pwd_count + 1`
						if [ $pwd_count -gt 3 ]; then
							echo
							echo " You have tried using wrong password too many times. Please create and verify MySQL 'root' password and try again." 
							echo " Command to create MySQL 'root' password : /usr/bin/mysqladmin -u root password 'password123'"
							echo
							Exit
						else
							continue;
						fi
					else
						odbc_passwd=`cat /etc/odbc.ini |grep "PWD" |cut -d"=" -f2 |tr -d " "`
						/bin/sed "s/${odbc_passwd}/${mysql_passwd}/g" /etc/odbc.ini > /etc/odbc.ini.temp
						/bin/mv /etc/odbc.ini.temp /etc/odbc.ini
					fi
					rm -f mysql_query.sql 1> /dev/null 2> /dev/null
					break
				fi
			done
		else
			mysql_passwd=${MYSQL_PASSWORD}
			odbc_passwd=`cat /etc/odbc.ini |grep "PWD" |cut -d"=" -f2 |tr -d " "`
			/bin/sed "s/${odbc_passwd}/${mysql_passwd}/g" /etc/odbc.ini > /etc/odbc.ini.temp
			/bin/mv /etc/odbc.ini.temp /etc/odbc.ini
		fi
		echo
		
		# Verify existance of  mysql database 
		if [ -f "/etc/odbc.ini" ]; then
			odbc_database=`cat /etc/odbc.ini |grep -w "DATABASE" |cut -d"=" -f2 |tr -d " "`
			if [ ! -d  "/var/lib/mysql/$odbc_database" ]; then
				echo " create database $odbc_database collate latin1_general_cs;" > mysql_query.sql
				echo "GRANT ALL PRIVILEGES ON *.* TO root@'`hostname`' IDENTIFIED BY "$mysqlpwd";" >> mysql_query.sql
				mysql -u $MYSQL_USER -p$mysql_passwd < mysql_query.sql 2>> /dev/null
				rm -f mysql_query.sql 1> /dev/null 2> /dev/null
			fi
		fi

		echo
		# Verify the location of isql command
		if [ "`whereis isql |cut -d":" -f2`" = "" ]; then
			echo " Warning : unixodbc is not properly installed in this system. Please install it correctly and try again."
			Exit
		elif [ -f /usr/bin/isql ]; then
			ISQL="/usr/bin/isql"
		elif [ -f /usr/local/bin/isql ]; then
			if [ -f /usr/local/etc/odbc.ini ] && [ ! -s /usr/local/etc/odbc.ini ]; then
				rm -f /usr/local/etc/odbc.ini 1> /dev/null 2> /dev/null 
				ln -s /etc/odbc.ini /usr/local/etc/odbc.ini
			fi
			if [ -f /usr/local/etc/odbcinst.ini ] && [ ! -s /usr/local/etc/odbcinst.ini ]; then
				rm -f /usr/local/etc/odbcinst.ini 1> /dev/null 2> /dev/null 
				ln -s /etc/odbcinst.ini /usr/local/etc/odbcinst.ini
			fi
			ISQL="/usr/local/bin/isql"
		fi
		
		if [ -f /usr/local/etc/odbc.ini ] && [ ! -s /usr/local/etc/odbc.ini ]; then
			rm -f /usr/local/etc/odbc.ini 1> /dev/null 2> /dev/null 
			ln -s /etc/odbc.ini /usr/local/etc/odbc.ini
		elif [ ! -f /usr/local/etc/odbc.ini ]; then 
			ln -s /etc/odbc.ini /usr/local/etc/odbc.ini
		fi
		if [ -f /usr/local/etc/odbcinst.ini ] && [ ! -s /usr/local/etc/odbcinst.ini ]; then
			rm -f /usr/local/etc/odbcinst.ini 1> /dev/null 2> /dev/null 
			ln -s /etc/odbcinst.ini /usr/local/etc/odbcinst.ini
		elif [ ! -f /usr/local/etc/odbcinst.ini ]; then 
			ln -s /etc/odbcinst.ini /usr/local/etc/odbcinst.ini
		fi
		if [ -f /etc/debian_version ] && [ `/usr/bin/getconf LONG_BIT` -eq 64 ]; then
			/usr/bin/isql 1> /dev/null 2> /dev/null
			if [ $? -eq 127 ]; then
				#echo "Installing missing package - ia32-libs. It may take a few minutes...\n"
				#echo "Please be patient ....\n";
				#apt-get -y install ia32-libs 1>> ${LOGFILE} 2>> ${LOGFILE}
				/usr/bin/isql 1> /dev/null 2> /dev/null	
				#if [ $? -eq 127 ]; then
					#echo "\033[0;31m Installation of is32-libs package failed..  \033[0m"
                    #exit	
			#	fi
			fi
		elif [ ! -f /etc/debian_version ] && [ `/usr/bin/getconf LONG_BIT` -eq 64 ]; then
			/usr/bin/isql 1> /dev/null 2> /dev/null
			if [ $? -eq 126 ]; then				
				echo "Installing missing package - glibc. It may take a few minutes...\n"
				echo "Please be patient ....\n";
				yum -y install glibc.i686 1>> ${LOGFILE} 2>> ${LOGFILE} || yum -y install glibc.i386 1>> ${LOGFILE} 2>> ${LOGFILE}
				/usr/bin/isql 1> /dev/null 2> /dev/null
				if [ $? -eq 127 ]; then
					echo "\033[0;31m Installation of glibc package failed..  \033[0m"
                    Exit	
				fi
			fi	
		fi
		
		echo " On success it will show SQL> prompt. Type quit or press enter to exit."
		echo
		# isql test
	   # $ISQL -v sgserver root $mysql_passwd
		if [ $? -eq 0 ]; then
			cp -f /etc/odbc.ini ${WRKDIR}/odbcSuccess.info
			if [ -f /etc/odbc.ini.sg ] && [ -s /etc/odbc.ini.sg ] ; then
				/usr/bin/diff /etc/odbc.ini /etc/odbc.ini.sg >> /dev/null
				if [ $? -ne 0 ]; then
					grep -w sgserver /etc/odbc.ini.sg
					if [ $? -ne 0 ]; then
						cp -f /etc/odbc.ini.sg /etc/odbc.ini >> /dev/null
						echo " " >> /etc/odbc.ini
						cat odbcSuccess.info >> /etc/odbc.ini
					fi
				fi  
			fi
			
			if [ -f /etc/odbcinst.ini.sg ]; then
				grep -w MySQL /etc/odbcinst.ini.sg >> /dev/null
				if [ $? -ne 0 ]; then
					# Bundling current information with Previous information
					cp -f /etc/odbcinst.ini.sg /etc/odbcinst.ini >> /dev/null
					echo " " >> /etc/odbcinst.ini
					cat odbc/odbcinst.ini >> /etc/odbcinst.ini  
				fi
			fi
			echo
			echo " SUCCESS : ODBC driver is successfully connected to MySQL server"
			echo
			echo "*************************************************************"
			odbc_database=`cat /etc/odbc.ini |grep -w "DATABASE" |cut -d"=" -f2 |tr -d " "`
			odbc_hostname=`cat /etc/odbc.ini |grep -w "SERVER" |cut -d"=" -f2 |tr -d " "`
			odbc_username=`cat /etc/odbc.ini |grep -w "UID" |cut -d"=" -f2 |tr -d " "`
			odbc_password=`cat /etc/odbc.ini |grep -w "PWD" |cut -d"=" -f2 |tr -d " "`
			if [ -f /etc/lsb-release ]  && [ "`egrep -i "SUSE" /proc/version`"  = "" ]; then
				echo "\033[1;2mODBC Driver Configuration Details : \033[0m";
			else
				echo -e "\033[1;2mODBC Driver Configuration Details : \033[0m"
			fi
			if [ "${odbc_database}" = "" ] || [ "${odbc_hostname}" = "" ] || [ "${odbc_username}" = "" ] || [ "${odbc_password}" = "" ]; then
				cat /etc/odbc.ini
			else
				echo "DatasourceName    : sgserver"
				echo "DatabaseName      : $odbc_database"
				echo "HostName  	  : $odbc_hostname"
				echo "UserID            : $odbc_username"
				echo "Password  	  : $odbc_password"
			fi
			echo
			echo "*************************************************************"
#			if [ -f /etc/lsb-release ]  && [ "`egrep -i "SUSE" /proc/version`"  = "" ]; then
#				echo "\033[0;33mSpecify The Attributes During Backup Server Installation\033[0m" 
#				echo "You need to provide above shown DatasourceName, DatabaseName, HostName, UserID and Password during backup server installation."
#			else
#				echo -e "\033[0;33mSpecify The Attributes During Backup Server Installation\033[0m" 
#				echo "You need to provide above shown DatasourceName, DatabaseName, HostName, UserID and Password during backup server installation."
#			fi
			
			if [ ${FRESH_DATABASE} -eq 1 ]; then
				echo "Installation of compatible RDBMS for backup or replication server is successfully completed."
			fi
			rm ${WRKDIR}/odbcSuccess.info
			echo
			while true; do
				echo -n "Do you want to install MongoDB Now ? Enter [y/n] : "
				read urChoice
				case ${urChoice} in
					[yY]|[yY][eE][sS])
							break;;	
					[nN]|[nN][oO])
						Exit;;
					*)
					echo "Please enter [y/n] to proceed."
					echo
					continue;;
				esac
			done
			#echo "SIMPLE_ONLINE_INSTALLER ::: ${SIMPLE_ONLINE_INSTALLER}"
			if [ ${SIMPLE_ONLINE_INSTALLER} -eq 1 ]; then
				break
				echo
				while true; do
					echo -n "Do you want to install VembuBDR's server Now ? Enter [yes/no] : "
					read urChoice
					case ${urChoice} in
						[yY]|[yY][eE][sS])
								break;;	
						[nN]|[nN][oO])
							Exit;;
						*)
						echo "Please enter [y/n] to proceed."
						echo
						continue;;
					esac
				done
			fi
			break
		else
			if [ -f /etc/odbcinst.ini.sg ]; then
				/bin/grep -w "MySQL" /etc/odbcinst.ini.sg >> /dev/null
				if [ $? -ne 0 ]; then
					/bin/cp -f /etc/odbcinst.ini.sg /etc/odbcinst.ini >> /dev/null
					echo " " >> /etc/odbcinst.ini
					/bin/cat odbc/odbcinst.ini >> /etc/odbcinst.ini  
				fi
			fi
			if [ -f /etc/odbc.ini.sg ]; then
				/usr/bin/diff /etc/odbc.ini /etc/odbc.ini.sg >> /dev/null
				if [ $? -ne 0 ]; then
					grep -w sgserver /etc/odbc.ini.sg
					if [ $? -ne 0 ]; then
						/bin/mv /etc/odbc.ini /etc/odbc.ini.tmp
						/bin/cp -f /etc/odbc.ini.sg /etc/odbc.ini >> /dev/null
						echo " " >> /etc/odbc.ini
						/bin/cat /etc/odbc.ini.tmp >> /etc/odbc.ini 
						/bin/rm -f /etc/odbc.ini.tmp >> /dev/null 
					fi
				fi
			fi
			echo
			if [ -f /etc/lsb-release ]  && [ "`egrep -i "SUSE" /proc/version`"  = "" ]; then
				echo  " \033[38mConnection Status\033[0m: ODBC driver failed to connect MySQL server. Please verify the content"
				echo  " of \"/etc/odbc.ini\" file and also read \"\033[1;34mCreate System DSN\033[0m\" column in MySQL Help page."
				echo  " Help page Link: \033[1;34mhttps://storegrid.com/online-backup/sp-help/mysql-linux-manual.html\033[0m"
			else
				echo -e " \033[38mConnection Status\033[0m: ODBC driver failed to connect MySQL server. Please verify the content"
				echo -e " of \"/etc/odbc.ini\" file and also read \"\033[1;34mCreate System DSN\033[0m\" column in MySQL Help page."
				echo -e " Help page Link: \033[1;34mhttps://storegrid.com/online-backup/sp-help/mysql-linux-manual.html\033[0m"
			fi
			echo
			while true; do
				echo "*************************************************************"
				echo "1. Do you want run this test again ?"
				echo "2. Do you want to install VembuBDR server now ?"
				echo "*************************************************************"
				echo -n "Please enter your choice [1/2] : "
				read urChoice
				case ${urChoice} in
				1)
					echo ""
					break;;
				2)
					echo 
					if [ -f /etc/lsb-release ]  && [ "`egrep -i "SUSE" /proc/version`"  = "" ]; then
						echo -e "\033[33mNote: After creating the MySQL driver and DSN in the server, check the connectivity from Storegrid server to the MySQL Server. Execute the following command 'isql -v [dsn name] [user name] [password]' and check if you are able to connect the MySQL Server from the VembuBDR  Server.\033[0m"
					else
						echo -e "\033[33mNote: After creating the MySQL driver and DSN in the VembuBDR server, check the connectivity from VembuBDR  Server to the MySQL Server. Execute the following command 'isql -v [dsn name] [user name] [password]' and check if you are able to connect the MySQL Server from the VembuBDR  Server.\033[0m"
					fi
					echo 
					sleep 4	
					break;;
				*)
					echo "Please enter [1/2] ]"
					continue;;
				esac
			done
		fi 
	done
	cd ${WRKDIR}
}

admin_settings()
{
	agreed=""
	while true; do
		echo
		echo "*************************************************************"
		echo "	  Mysql Server Administrator Settings  			           "
		echo "*************************************************************"
		#echo " 1. Set mysql server 'root' user password."
		echo " 1. Change mysql server 'root' user password."
		#echo " 3. Create VembuBDR  Server database."
		echo " 2. Check connectivity between odbc driver and mysql server."
		echo " q. exit. "
		echo
		echo -n " choose : "
		read reply
	
		case ${reply} in	
			#1)	
			#	mysql_server_password_setting
			#	continue;;
			1)
				mysql_server_change_password
				continue;;
			#3)	
			#	mysql_server_create_database
			#	continue;;
			2)	
				odbc_configuration
				continue;;
			q)
				Exit;;
		esac
	done
}

mysql_server_configuration()
{
	if [ -f "/etc/init.d/mysql" ]; then
		/etc/init.d/mysql status >> ${LOGFILE}
		if [ $? -eq 0 ]; then
			/etc/init.d/mysql stop >> ${LOGFILE}
			echo
			echo "Giving mysqld a couple of seconds to exit cleanly"
			echo
			sleep 5
		fi
	fi

	cp -f my-medium.cnf /etc/my.cnf >> ${LOGFILE}
	chmod 644 /etc/my.cnf >> ${LOGFILE}
	perl -e "s:datadir=/var/lib/mysql:datadir=${MYSQL_DATA_DIRECTORY}:g" -p -i /etc/my.cnf
	if [ -f /etc/mysql/my.cnf ]; then
		mv /etc/mysql/my.cnf /etc/mysql/my.cnf_old >> ${LOGFILE}
	fi
	
	if [ -f /etc/debian_version ]; then
		if [ -f /opt/mysql/server-5.5/bin/mysql ]; then
			rm -f /usr/bin/mysql 2>/dev/null
			ln -s /opt/mysql/server-5.5/bin/mysql /usr/bin/mysql
		fi
		if [ -f /opt/mysql/server-5.5/bin/mysqldump ]; then
			rm -f /usr/bin/mysqldump 2>/dev/null
			ln -s /opt/mysql/server-5.5/bin/mysqldump /usr/bin/mysqldump
		fi
		if [ -f /opt/mysql/server-5.5/bin/mysqladmin ]; then
			rm -f /usr/bin/mysqladmin 2>/dev/null
			ln -s /opt/mysql/server-5.5/bin/mysqladmin /usr/bin/mysqladmin
		fi
	fi

	if [ -f "/usr/local/bin/mysqldump" ]; then
		rm -f /usr/local/bin/mysqldump >> ${LOGFILE}
		ln -s /usr/bin/mysqldump /usr/local/bin/mysqldump >> ${LOGFILE}
	fi
	
	egrep -i "^mysql" /etc/passwd 1>> /dev/null
	if [ $? -ne 0 ]; then
		groupadd -f ${mysqld_group} 2> /dev/null || true
		useradd -r -d $MYSQL_DATA_DIRECTORY -s /bin/bash -c "MySQL server" -g $mysqld_group $mysqld_user 2> /dev/null || true
		usermod -g $mysqld_group $mysqld_user 2> /dev/null || true 
	fi
	
	if [ -d "${MYSQL_DATA_DIRECTORY}" ]; then
		if [ ! -d "${MYSQL_DATA_DIRECTORY}_old" ]; then
			mkdir -p ${MYSQL_DATA_DIRECTORY}_old 1> ${LOGFILE} 2> ${LOGFILE}
		else
			rm -rf  ${MYSQL_DATA_DIRECTORY}_old/* 1> ${LOGFILE} 2> ${LOGFILE}
		fi
		mv -f ${MYSQL_DATA_DIRECTORY}/* ${MYSQL_DATA_DIRECTORY}_old/ 1> ${LOGFILE} 2> ${LOGFILE}
		rm -rf ${MYSQL_DATA_DIRECTORY} 1> ${LOGFILE} 2> ${LOGFILE}	
 	fi
	echo 1> ${LOGFILE} 2> ${LOGFILE}
	echo 1> ${LOGFILE} 2> ${LOGFILE}
	
	
	#MYSQL_DATA_DIRECTORY=/var/lib/mysql
	#echo " MYSQL DATA DIRECTOREY :: " ${MYSQL_DATA_DIRECTORY};

	if [ -f /etc/debian_version ]; then
		/opt/mysql/server-5.5/scripts/mysql_install_db --user=mysql --datadir=${MYSQL_DATA_DIRECTORY} 1> ${LOGFILE} 2> ${LOGFILE}
	else
		/usr/bin/mysql_install_db --user=mysql --datadir=${MYSQL_DATA_DIRECTORY} 1> ${LOGFILE} 2> ${LOGFILE}
	fi
	echo 1> ${LOGFILE} 2> ${LOGFILE}
	echo 1> ${LOGFILE} 2> ${LOGFILE}
	
	chown -R ${mysqld_user}:${mysqld_group} $MYSQL_DATA_DIRECTORY >> ${LOGFILE} 
	if [ -f /etc/debian_version ]; then
		cp -f /opt/mysql/server-5.5/support-files/mysql.server /etc/init.d/mysql >> ${LOGFILE}
	else	
		cp -f /usr/share/mysql/mysql.server /etc/init.d/mysql >> ${LOGFILE}
	fi
	chmod 755 /etc/init.d/mysql >> ${LOGFILE}

	if [ -f /etc/debian_version ]; then	
		/usr/sbin/update-rc.d mysql defaults >> ${LOGFILE}
	else
		/sbin/chkconfig --add mysql >> ${LOGFILE}
	fi

	if [ -f "${MYSQL_DATA_DIRECTORY}/ib_logfile0" ]; then
		mv ${MYSQL_DATA_DIRECTORY}/ib_logfile0 ${MYSQL_DATA_DIRECTORY}/ib_logfile0_OLD >> ${LOGFILE}
	fi
 	
	if [ -f "${MYSQL_DATA_DIRECTORY}/ib_logfile1" ]; then
		mv ${MYSQL_DATA_DIRECTORY}/ib_logfile1 ${MYSQL_DATA_DIRECTORY}/ib_logfile1_OLD >> ${LOGFILE}
	fi

	echo 
	echo "MySQL-server installation settings are completed and server will start now.."
	echo "Please be patient. It could take a few minutes."
	echo
	/etc/init.d/mysql start
	#if [ $? -ne 0 ]; then
	#	echo
	#	echo "If MySQL server could not be started as service. Please start MySQL Server as an application by running the command \"/usr/bin/mysqld_safe --user=root &\" to check if it starts. Also go through the \"TroubleShooting Tips\" from the following URL: "
	#	if [ -f "/etc/debian_version" ]; then
	#		echo " < \033[1;34mhttps://storegrid.com/online-backup/sp-help/mysql-linux-manual.html\033[0m >" 
	#	else
	#		echo -e " < \033[1;34mhttps://storegrid.com/online-backup/sp-help/mysql-linux-manual.html\033[0m >"
	#	fi
	#	echo
	#	Exit
	#else
	
		echo
		/etc/init.d/mysql status
		echo
		mysql_server_status=1
		export mysql_server_status
		if [ ! -f /etc/debian_version ]; then	
			if [ ! -f /usr/bin/mysql ]; then
				Installing_mysql_client Mysql-client
			fi
		fi
		env_var_set
		mysql_server_password_setting
		mysql_server_create_database
#		echo
#		if [ -f /etc/lsb-release ]  && [ "`egrep -i "SUSE" /proc/version`"  = "" ]; then
#			echo " \033[0;33mNote : Please don't forget to check connectivity between ODBC driver and MySQL-Server.\033[0m"
#		else
#			echo -e " \033[0;33mNote : Please don't forget to check connectivity between ODBC driver and MySQL-Server.\033[0m"
#		fi
#		echo 
#	fi
	echo
}

check_mysql_version()
{
	echo "MySQL was already installed in this machine. Please provide the MySQL root password for check the MySQL compatibility."
	echo
	mysql_compat=0
	while true; do
		echo -n 1>&2 " Enter password: "
		stty -echo
		read pswd
		stty echo
		pwd_length=`echo -n $pswd | wc -c`
		if [ $pwd_length -eq 0 ];then
			echo
			echo " Password cannot be empty."
			echo
			continue
		fi
		mysql_version=`mysql -u root -p$pswd -e "select version()" --skip-column-names`
		if [ $? -eq 0 ]; then
			mysql_version=`echo $mysql_version | awk "/5.5/ || /5.1/"`
			if [ ! -z $mysql_version ]; then				
				for storage_engine in `mysql -u root -p$pswd -e "show global variables like 'storage_engine'" --skip-column-names`
				do
					if [ "$storage_engine" = "InnoDB" ]; then
						mysql_compat=1
						MYSQL_PASSWORD=$pswd
					fi
				done
			fi
		else
			continue
		fi
		break
	done
	echo
	echo
}

installing_mysql_server_dependencies()
{
	cd  ${WRKDIR}/${MYSQL_DOWNLOADS}/${mysql_package_folder}/dep-Pkgs
	dep_package=`ls |grep -i $1`
	if [ $? -ne 0 ]; then
		echo " Warning : MySQL dependency package $dep_package is missing. Please verify the downloaded file."
	else
		echo -n " MySQL dependency package - $1"
		case $1 in 
			perl-DBI)
					if [ -f /etc/debian_version ]; then
						dpkg -S $1 1> /dev/null 2> /dev/null
						if [ $? -eq 0 ]; then
							if [ -f /etc/lsb-release ]  && [ "`egrep -i "SUSE" /proc/version`"  = "" ]; then
								echo "  : \033[0;32mInstalled\033[0m"
							else
								echo -e "  : \033[0;32mInstalled\033[0m"
							fi
						else
							echo "  installing.."
							$depinstall $dep_package 2>> ${LOGFILE}
						fi
					else
						rpm -q --whatprovides perl-DBI 1> /dev/null 2>/dev/null
						if [ $? -eq 0 ]; then
							echo -e "  : \033[0;32mInstalled\033[0m"
						else
							echo "  installing.."
							$depinstall $dep_package 2>> ${LOGFILE}
						fi
					fi;;
			ncurses)
					if [ -f /etc/debian_version ]; then
						dpkg -S $1 1> /dev/null 2> /dev/null
						if [ $? -eq 0 ]; then
							if [ -f /etc/lsb-release ]  && [ "`egrep -i "SUSE" /proc/version`"  = "" ]; then
								echo "  : \033[0;32mInstalled\033[0m"
							else
								echo -e "  : \033[0;32mInstalled\033[0m"
							fi
						else
							echo "  installing.."
							$depinstall $dep_package 2>> ${LOGFILE}
						fi
					else
						rpm -q --whatprovides ncurses 1> /dev/null 2>/dev/null
						if [ $? -eq 0 ]; then
							echo -e "  : \033[0;32mInstalled\033[0m"
						else
							echo "  installing.."
							$depinstall $dep_package 2>> ${LOGFILE}
						fi
					fi;;
			ncurses-devel)
					if [ -f /etc/debian_version ]; then
						dpkg -S $1 1> /dev/null 2> /dev/null
						if [ $? -eq 0 ]; then
							if [ -f /etc/lsb-release ]  && [ "`egrep -i "SUSE" /proc/version`"  = "" ]; then
								echo "  : \033[0;32mInstalled\033[0m"
							else
								echo -e "  : \033[0;32mInstalled\033[0m"
							fi
						else
							echo "  installing.."
							$depinstall $dep_package 2>> ${LOGFILE}
						fi
					elif [ -f /etc/mandrake-release ]; then
						rpm -q --whatprovides libncurses-devel 1> /dev/null 2>/dev/null
						if [ $? -eq 0 ]; then
							echo -e "  : \033[0;32mInstalled\033[0m"
						else
							echo "  installing.."
							$depinstall $dep_package 2>> ${LOGFILE}
						fi
					else
						rpm -q --whatprovides ncurses-devel 1> /dev/null 2>/dev/null
						if [ $? -eq 0 ]; then
							echo -e "  : \033[0;32mInstalled\033[0m"
						else
							echo "  installing.."
							$depinstall $dep_package 2>> ${LOGFILE}
						fi
					fi;;
			libaio)
					if [ -f /etc/debian_version ]; then
						dpkg -S libaio1 1> /dev/null 2> /dev/null
						if [ $? -eq 0 ]; then
							if [ -f /etc/lsb-release ]  && [ "`egrep -i "SUSE" /proc/version`"  = "" ]; then
								echo "  : \033[0;32mInstalled\033[0m"
							else
								echo -e "  : \033[0;32mInstalled\033[0m"
							fi
						else
							echo "  installing.."
							$depinstall $dep_package 2>> ${LOGFILE}
						fi
					elif [ -f /etc/mandrake-release ]; then
						rpm -q --whatprovides $1 1> /dev/null 2>/dev/null
						if [ $? -eq 0 ]; then
							echo -e "  : \033[0;32mInstalled\033[0m"
						else
							echo "  installing.."
							$install $dep_package 2>> ${LOGFILE}
						fi
					else
						rpm -q --whatprovides $1 1> /dev/null 2>/dev/null
						if [ $? -eq 0 ]; then
							echo -e "  : \033[0;32mInstalled\033[0m"
						else
							echo "  installing.."
							$install $dep_package 2>> ${LOGFILE}
						fi
					fi;; 	
		esac
	fi
	echo		
	cd ${WRKDIR}
}

Installing_mysql_server()
{
	echo
	echo "*************************************************************"
	echo "	Checking MySQL Server dependency package(s)			       "
	echo "*************************************************************"
	installing_mysql_server_dependencies perl-DBI
	installing_mysql_server_dependencies ncurses
	installing_mysql_server_dependencies ncurses-devel
	installing_mysql_server_dependencies libaio
	echo
	echo "*************************************************************"
	echo "	Installing MySQL Server 			       			       "
	echo "*************************************************************"
	cd  ${WRKDIR}/${MYSQL_DOWNLOADS}/${mysql_package_folder}
	mysql_server_package=`ls |grep -i $1`
	if [ $? -ne 0 ]; then
		echo " Warning : mysql server package file is missing. Please verify the downloaded file."
	else
		if [ -f /etc/mandrake-release ]; then
			$install $mysql_server_package 1>> ${LOGFILE} 2>> ${LOGFILE}
			if [ $? -eq 0 ]; then
				echo
				echo "$mysql_server_package is successfully installed."
				echo
				mysql_server_configuration
			fi
		elif [ -f /etc/debian_version ]; then		
			$install $mysql_server_package 1>> ${LOGFILE} 2>> ${LOGFILE}
			if [ $? -eq 0 ]; then			
				echo -n " mysql-server :  "
				i=8
				while [ $i -gt 0 ]; do
					echo -n "##"
					sleep 1
					i=`expr $i - 1`
				done
				echo "[100%]"
				echo
				echo " $mysql_server_package is successfully installed."
				echo
				mysql_server_configuration			
			fi
		else
			$install $mysql_server_package 1>> ${LOGFILE} 2>> ${LOGFILE} 
			if [ $? -eq 0 ]; then
				echo
				echo "$mysql_server_package is successfully installed."
				echo
				mysql_server_configuration
			fi
		fi
	fi
	cd ${WRKDIR}
}

Installing_mysql_client()
{
	echo
	echo "*************************************************************"
	echo "		Installing MySQL Client 			        		   "
	echo "*************************************************************"
	cd  ${WRKDIR}/${MYSQL_DOWNLOADS}/${mysql_package_folder}
	mysql_client_package=`ls |grep -i $1`
	if [ $? -ne 0 ]; then
		echo
		echo " Warning : mysql client package file is missing. Please verify the downloaded file."
		echo
	else
		if [ -f /etc/debian_version ]; then	
			$install $mysql_client_package 1> /dev/null 2> /dev/null
			if [ $? -eq 0 ]; then
				echo -n " mysql-client :  "
				i=8
				while [ $i -gt 0 ]; do
					echo -n "##"
					sleep 1
					i=`expr $i - 1`
				done
				echo "[100%]"
				echo
				echo " $mysql_client_package is successfully installed."
				echo
				mysql_client_status=1
			fi
		else		
			$install $mysql_client_package
			if [ $? -eq 0 ]; then
				echo
				echo " $mysql_client_package is successfully installed."
				echo
				mysql_client_status=1
			fi
		fi
	fi
	export mysql_client_status
	cd ${WRKDIR}
}

Installing_mysql_connector()
{
	if [ $1 -eq 1 ]; then
		echo
		echo "*************************************************************"
		echo "		Installing MySQL-connector-odbc 	         		   "
		echo "*************************************************************"

	if [ "$MACHINE_ARC" = "32" ]; then
		cd  ${WRKDIR}/${MYSQL_DOWNLOADS}/${mysql_package_folder}
		mysql_connector_package=`ls |grep -i mysql-connector-odbc`	
		if [ $? -ne 0 ]; then
			echo
			echo " Warning : mysql-connector-odbc package file is missing. Please verify the downloaded file."
		else
			if [ -f "/usr/local/lib/libmyodbc3-3.51.27.so" ]; then
				echo
				echo " mysql-connector-odbc package is already installed in this machine."
				echo
			else
				unzip $mysql_connector_package 1> /dev/null 2> /dev/null
				cp -f mysql-connector-odbc-${MYSQL_CONNECTOR_VERSION}-linux/lib/* /usr/local/lib/ >> ${LOGFILE}
				rm -rf mysql-connector-odbc-${MYSQL_CONNECTOR_VERSION}-linux >> ${LOGFILE}
				echo -n " mysql-connector-odbc :  "
				i=8
				while [ $i -gt 0 ]; do
					echo -n "##"
					sleep 1
					i=`expr $i - 1`
				done
				echo "[100%]"	
				echo
				echo " $mysql_connector_package is successfully installed."
				mysql_connector_status=1
				echo
			fi
		fi
		cd ${WRKDIR}
		elif [ "$MACHINE_ARC" = "64" ]; then
			mySqlconnector64Folder=mysql-connector-odbc-3.51.27-linux-x86-64bit
			mySqlconnector64bitPkg=${mySqlconnector64Folder}.tar.gz
			cd  ${WRKDIR}/${MYSQL_DOWNLOADS}
			mySQL64bitconDir=${WRKDIR}/${MYSQL_DOWNLOADS}/mysql-connector-odbc-3.51.27-linux-x86-64bit
			[ -d ${mySQL64bitconDir} ] && rm -rf ${mySQL64bitconDir}
			 mysql64BitCon=$(grep -rni "mysql-connector64" ${BUILD_INFO_FILE} |cut -d"=" -f2)
			[ ! -f ${mySqlconnector64Folder} ] && \
			{ 
				wget -c ${mysql64BitCon}  
			}
			tar -xzvf ${mySqlconnector64bitPkg} >/dev/null
			cd  ${mySqlconnector64Folder}
			[ ! -d  /usr/local/lib64 ] && ( mkdir /usr/local/lib64)
			cp -f lib/* /usr/local/lib64/. >/dev/null
			[ $? -ne 0 ] && \
			{ 
				echo " Installation of $mySqlconnector64bitPkg failed.."
				Exit
			}
			echo -e "\n $mySqlconnector64bitPkg is successfully installed.\n"
			mysql_connector_status=1
		fi
	elif [ $1 -eq 2 ]; then
		while true; do
			echo
			echo " Warning : This will uninstall mysql-connector-odbc."
			echo -n " Proceed? [y/n] : "
			read proceed
				case ${proceed} in
						[yY]|[Yy][eE][sS])
							if [ "$MACHINE_ARC" = "64" ]; then
								rm -f /usr/local/lib64/libmyodbc3*
							else
								rm -f /usr/local/lib/libmyodbc3*
							fi
							echo
							echo " MySQL-connector-odbc is successfully uninstalled."
							echo
							break;;
	
						[nN]|[nN][oO])
							echo
							break;;
						*)
							echo " Please enter [y/n] to proceed"
							continue;;
				esac
		done
	fi	 
}

unixodbc_settings()
{
	if [ -f /etc/debian_version ]; then
		#For Debian machines
		if [ `/usr/bin/getconf LONG_BIT` -eq 64 ]; then
			if [ ! -d "/lib32" ];then
				mkdir -p /lib32/
			fi
			if [ ! -f "/lib/libtinfo.so.5" ]; then
				cp -f libs/libtinfo.so.5 /lib/  >> ${LOGFILE}
			fi
			if [ ! -f "/lib32/libreadline.so.4.3" ] && [ ! -f "/lib32/libreadline.so.4" ]; then
				rm -f /lib32/libreadline.so.4 >> ${LOGFILE}
				cp -f libs/libreadline.so.4.3 /lib32/  >> ${LOGFILE}
				/bin/ln -s /lib32/libreadline.so.4.3 /lib32/libreadline.so.4 >> ${LOGFILE}
			fi
			if [ ! -f "/lib32/libncurses.so.5" ]; then
				cp -f ../odbc/libncurses.so.5 /lib32/libncurses.so.5 >> /dev/null
			fi
		else
			if [ ! -f "/lib/libtinfo.so.5" ]; then
				cp -f libs/libtinfo.so.5 /lib/  >> ${LOGFILE}
			fi
			if [ ! -f "/usr/lib/libreadline.so.4.3" ] && [ ! -f "/usr/lib/libreadline.so.4" ]; then
				rm -f /usr/lib/libreadline.so.4 >> ${LOGFILE} 
				cp -f libs/libreadline.so.4.3 /usr/lib/  >> ${LOGFILE}
				/bin/ln -s /usr/lib/libreadline.so.4.3 /usr/lib/libreadline.so.4 >> ${LOGFILE}
			fi
			if [ ! -f "/lib/libncurses.so.5" ]; then
				cp -f ../odbc/libncurses.so.5 /lib/libncurses.so.5 >> /dev/null
			fi
		fi
	else
		#For RedHat machines
		if [ `/usr/bin/getconf LONG_BIT` -eq 64 ]; then
			if [ -f /usr/lib64/libodbcinst.so ] && [ ! -f /lib64/libodbcinst.so.1 ]; then
				/bin/ln -s /usr/lib64/libodbcinst.so /lib64/libodbcinst.so.1 >> ${LOGFILE}
			fi
		fi
	fi
	
	if [ ! -d "/usr/local/etc" ]; then
    	mkdir -p /usr/local/etc >> ${LOGFILE}
    fi
	rm -f /usr/local/etc/odbc* >> ${LOGFILE}
	/bin/ln -s /etc/odbc.ini /usr/local/etc/odbc.ini >> ${LOGFILE}
	/bin/ln -s /etc/odbcinst.ini /usr/local/etc/odbcinst.ini >> ${LOGFILE}
	unixodbc_status=1
	export unixodbc_status
	echo
	echo " unixodbc is successfully installed."
	echo
}

Installing_unixodbc()
{
	echo
	echo "*************************************************************"
	echo "		Installing Unixodbc				 	   				   "
	echo "*************************************************************"
	if [ "${MACHINE_ARC}" = "32" ]; then
		cd  ${WRKDIR}/${MYSQL_DOWNLOADS}/${mysql_package_folder}
		unixodbc_package=`ls |grep -i unixodbc`	
		if [ $? -ne 0 ]; then
			echo " Warning :  unixodbc package file is missing. Please verify the downloaded file."
			echo
		else	
			if [ -f /etc/debian_version ]; then	
				$depinstall $unixodbc_package 1> /dev/null 2> /dev/null
				if [ $? -eq 0 ]; then
					echo -n " unixodbc :  "
					i=8
					while [ $i -gt 0 ]; do
						echo -n "##"
						sleep 1
						i=`expr $i - 1`
					done
					echo "[100%]"
					unixodbc_settings
				fi
			else
				$depinstall $unixodbc_package
				if [ $? -eq 0 ]; then 
					unixodbc_settings
				fi
			fi
		fi
		cd ${WRKDIR}
	elif [ "${MACHINE_ARC}" = "64" ]; then
		if [ -f /etc/debian_version ]; then	
			echo >&2 " Installing UnixODBC for 64-bit. It may take few minutes.\n Please be patient ...."
			apt-get -y install unixodbc 1>> ${LOGFILE} 2>> ${LOGFILE}
			[ $? -ne 0 ] &&  \
			{ 
			echo  >&2 "UnixODBC Installation failed... "
			Exit  
			}
		else
			#Installing UnixODBC through YUM 
			echo >&2 -e " Installing UnixODBC for 64-bit. It may take few minutes.\n Please be patient ...."
			yum -y install unixODBC.x86_64 1>> ${LOGFILE} 2>> ${LOGFILE}
			[ $? -ne 0 ] &&  \
			{ 
			echo  >&2 "UnixODBC Installation failed... "
			Exit  
			}
			if [ -f /usr/lib64/libodbcinst.so ] && [ ! -f /lib64/libodbcinst.so.1 ]; then
				/bin/ln -s /usr/lib64/libodbcinst.so /lib64/libodbcinst.so.1 >> ${LOGFILE}
			fi
			#unixodbc_settings
		fi
	fi
	cd ${WRKDIR}
}

uninstall_rpm_package()
{
  	echo 	
	while true; do
		unistall_mysql_package=0
		count=0; server_count=0; client_count=0; connector_count=0; unixodbc_count=0
		if [ "`rpm -q --whatprovides mysql-server`" = "${RH_MYSQL_SERVER}" ]; then
			count=`expr $count + 1`
			server_count=$count
			#echo " $count. MySQL-server"
			unistall_mysql_package=1
		elif [ "`rpm -q --whatprovides mysql-server`" = "${RH_MYSQL_SERVER_ARCH}" ]; then
			count=`expr $count + 1`
			server_count=$count
			#echo " $count. MySQL-server"
			unistall_mysql_package=1
		elif [ "`rpm -q --whatprovides mysql-server`" = "${RH_MYSQL_SERVER_ARCH64}" ]; then
			count=`expr $count + 1`
			server_count=$count
			#echo " $count. MySQL-server"
			unistall_mysql_package=1
		elif [ "`rpm -q --whatprovides mysql-server`" = "${RH_MYSQL_SERVER55}" ]; then
			count=`expr $count + 1`
			server_count=$count
			#echo " $count. MySQL-server"
			unistall_mysql_package=1
		elif [ "`rpm -q --whatprovides mysql-server`" = "${RH_MYSQL_SERVER55_ARCH}" ]; then
			count=`expr $count + 1`
			server_count=$count
			#echo " $count. MySQL-server"
			unistall_mysql_package=1
		elif [ "`rpm -q --whatprovides mysql-server`" = "${RH_MYSQL_SERVER55_ARCH64}" ]; then
			count=`expr $count + 1`
			server_count=$count
			#echo " $count. MySQL-server"
			unistall_mysql_package=1
		fi
		if [ "`rpm -q --whatprovides mysql-client`" = "${RH_MYSQL_CLIENT}" ]; then
			count=`expr $count + 1`
			client_count=$count
			#echo " $count. MySQL-client"
			unistall_mysql_package=1
		elif [ "`rpm -q --whatprovides mysql-client`" = "${RH_MYSQL_CLIENT_ARCH}" ]; then
			count=`expr $count + 1`
			client_count=$count
			#echo " $count. MySQL-client"
			unistall_mysql_package=1
		elif [ "`rpm -q --whatprovides mysql-client`" = "${RH_MYSQL_CLIENT_ARCH64}" ]; then
			count=`expr $count + 1`
			client_count=$count
			#echo " $count. MySQL-client"
			unistall_mysql_package=1
		elif [ "`rpm -q --whatprovides mysql-client`" = "${RH_MYSQL_CLIENT55}" ]; then
			count=`expr $count + 1`
			client_count=$count
			#echo " $count. MySQL-client"
			unistall_mysql_package=1
		elif [ "`rpm -q --whatprovides mysql-client`" = "${RH_MYSQL_CLIENT55_ARCH}" ]; then
			count=`expr $count + 1`
			client_count=$count
			#echo " $count. MySQL-client"
			unistall_mysql_package=1
		elif [ "`rpm -q --whatprovides mysql-client`" = "${RH_MYSQL_CLIENT55_ARCH64}" ]; then
			count=`expr $count + 1`
			client_count=$count
			#echo " $count. MySQL-client"
			unistall_mysql_package=1
		fi
		if [ -f "/usr/local/lib64/libmyodbc3-${MYSQL_CONNECTOR_VERSION}.so" ]; then
			count=`expr $count + 1`
			connector_count=$count
			#echo " $count. MySQL-connector-odbc"
			unistall_mysql_package=1
		fi
		if [ "`rpm -q --whatprovides unixODBC`" = "${RH_UNIXODBC}" ]; then
			count=`expr $count + 1`
			unixodbc_count=$count
			#echo " $count. unixODBC"
			unistall_mysql_package=1
		elif [ "`rpm -q --whatprovides unixODBC`" = "${RH_UNIXODBC_ARCH}" ]; then
			count=`expr $count + 1`
			unixodbc_count=$count
			#echo " $count. unixODBC"
			unistall_mysql_package=1
		fi
		if [ ${unistall_mysql_package} -eq 0 ]; then
			echo " All MySQL, Connector and ODBC packages are uninstalled."
			echo
			Exit	
		fi
		echo "*************************************************************"
		echo "  Do you want to uninstall the following package(s)          "	
		echo "*************************************************************"
		if [ $server_count -gt 0 ]; then
			echo " $server_count. MySQL-server"
		fi
		if [ $client_count -gt 0 ]; then
			echo " $client_count. MySQL-client"
		fi
		if [ $connector_count -gt 0 ]; then
			echo " $connector_count. MySQL-connector-odbc"	
		fi
		if [ $unixodbc_count -gt 0 ]; then
			echo " $unixodbc_count. unixODBC"
		fi
		echo " q. Exit"
		echo
		while true; do
			echo -n " choice : "
			read package_count
			case ${package_count} in 
				$server_count)
					while true; do
						echo
						echo " Warning : This will uninstall MySQL-Server rpm package. "
						echo -n " Proceed? [y/n] : "
						read proceed
						case ${proceed} in
							[yY]|[Yy][eE][sS])
								if [ -f "/etc/init.d/mysql" ]; then
									netstat -an |grep $MYSQL_PORT 1> /dev/null
									if [ $? -eq 0 ]; then
										/etc/init.d/mysql stop >> ${LOGFILE}
										echo
										echo " Giving mysqld a couple of seconds to exit cleanly before un-installing."
										echo
										sleep 3
									fi
								fi
								$uninstall `rpm -q --whatprovides mysql-server`  >>  /dev/null
								echo 
								echo " MySQL-Server is successfully uninstalled."
								echo
								break;;
							[nN]|[nN][oO])
								echo
								break;;
							*)
								echo " Please enter [y/n] to proceed"
								continue;;
						esac
					done ;;	
				$client_count)
					while true; do
						echo
						echo " Warning : This will uninstall MySQL-Client rpm package. "
						echo -n " Proceed? [y/n] : "
						read proceed
						case ${proceed} in
							[yY]|[Yy][eE][sS])
								$uninstall `rpm -q --whatprovides mysql-client`  >> /dev/null
								echo 
								echo " MySQL-Client is successfully uninstalled."
								echo
								break;;
							[nN]|[nN][oO])
								echo
								break;;
							*)
								echo " Please enter [y/n] to proceed"
								continue;;
						esac
					done ;;	
				$connector_count)
					Installing_mysql_connector 2
					break;;
				$unixodbc_count)
					while true; do
						echo
						echo " Warning : This will uninstall UnixODBC rpm package."
						echo -n " Proceed? [y/n] : "
						read proceed
						case ${proceed} in
							[yY]|[Yy][eE][sS])
								$uninstall `rpm -q --whatprovides unixODBC`  >> /dev/null				
								rm -f /etc/odbc.ini.sg /etc/odbcinst.ini.sg 1> /dev/nulll 2> /dev/null
								echo 
								echo " UnixODBC is successfully uninstalled."
								echo
								break;;
							[nN]|[nN][oO])
								echo
								break;;
							*)
								echo " Please enter [y/n] to proceed"
								continue;;
						esac
					done ;;	
				q)
					Exit;;
				*)
					echo " Please enter the right number to proceed "
					continue;;
			esac
			break;
		done
	done
}

uninstall_deb_package()
{
	echo 	
	while true; do
		unistall_mysql_package=0
		count=0; server_count=0; client_count=0; connector_count=0; unixodbc_count=0
		MYSQL_SERVER55_CURRENT_VERSION=`dpkg -s mysql |grep -w Version|cut -d":" -f2 | tr -d " "` 1> /dev/null 2> /dev/null
		if [ "${MYSQL_SERVER55_CURRENT_VERSION}" = "${MYSQL55_DBN_VERSION}" ]; then
			count=`expr $count + 1`
			server_count=$count
			unistall_mysql_package=1
	        elif [ "${MYSQL_SERVER55_CURRENT_VERSION}" = "${MYSQL5540_DBN_VERSION}" ]; then
			count=`expr $count + 1`
			server_count=$count
			unistall_mysql_package=1
		fi		
		if [ -f "/usr/local/lib64/libmyodbc3-${MYSQL_CONNECTOR_VERSION}.so" ]; then
			count=`expr $count + 1`
			connector_count=$count
			unistall_mysql_package=1
		fi
		
		#dpkg -s unixodbc 1> /dev/null 2> /dev/null
		#if [ $? -eq 0 ]; then
		UNIXODBC_CURRENT_VERSION=`dpkg -s unixodbc |grep -w Version|cut -d":" -f2 |tr -d " "` 1> /dev/null 2> /dev/null
		#UNIXODBC_CURRENT_VERSION_ARCH=`dpkg -s unixodbc:i386 |grep -w Version|cut -d":" -f2 |tr -d " "` 1> /dev/null 2> /dev/null
		if [ ! "${UNIXODBC_CURRENT_VERSION}" = "" ] || [ ! "${UNIXODBC_CURRENT_VERSION_ARCH}" = "" ]; then
			count=`expr $count + 1`
			unixodbc_count=$count
			unistall_mysql_package=1
		fi
		if [ ${unistall_mysql_package} -eq 0 ]; then
			echo " ALL MySQL, Connector and ODBC packages are un-installed."
			echo
			Exit	
		fi
		echo "*************************************************************"
		echo "  Do you want to uninstall the following package(s)          "	
		echo "*************************************************************"
		if [ $server_count -gt 0 ]; then
			echo " $server_count. MySQL-server"
		fi
		if [ $client_count -gt 0 ]; then
			echo " $client_count. MySQL-client"
		fi
		if [ $connector_count -gt 0 ]; then
			echo " $connector_count. MySQL-connector-odbc"	
		fi
		if [ $unixodbc_count -gt 0 ]; then
			echo " $unixodbc_count. unixODBC"
		fi
		echo " q. Exit"
		echo
		while true; do
			echo -n " choice : "
			read package_count
			case ${package_count} in 
				$server_count)
					while true; do
						echo
						echo " Warning : This will uninstall MySQL-Server debian package. "
						echo -n " Proceed? [y/n] : "
						read proceed
						case ${proceed} in
							[yY]|[Yy][eE][sS])
								if [ -f "/etc/init.d/mysql" ]; then
									netstat -an |grep $MYSQL_PORT 1> /dev/null
									if [ $? -eq 0 ]; then
										/etc/init.d/mysql stop >> ${LOGFILE}
										echo
										echo " Giving mysqld a couple of seconds to exit cleanly before uninstalling."
										sleep 3
									fi
								fi
								$uninstall mysql 1> /dev/null 2> /dev/null
								dpkg --purge mysql  1> /dev/null 2> /dev/null
								echo 
								echo " MySQL-Server is successfully uninstalled."
								echo
								break;;
							[nN]|[nN][oO])
								echo
								break;;
							*)
								echo " Please enter [y/n] to proceed"
								continue;;
						esac
					done ;;	
				$client_count)
					while true; do
						echo
						echo " Warning : This will uninstall MySQL-Client debian package. "
						echo -n " Proceed? [y/n] : "
						read proceed
						case ${proceed} in
							[yY]|[Yy][eE][sS])
								$uninstall mysql-client 1> /dev/null 2> /dev/null
								dpkg --purge mysql-client 1> /dev/null 2> /dev/null
								echo 
								echo " MySQL-Client is successfully uninstalled."
								echo
								break;;
							[nN]|[nN][oO])
								echo
								break;;
							*)
								echo " Please enter [y/n] to proceed"
								continue;;
						esac
					done ;;	
				$connector_count)
					Installing_mysql_connector 2
					break;;
				$unixodbc_count)
					while true; do
						echo
						echo " Warning : This will uninstall UnixODBC debain package."
						echo -n " Proceed? [y/n] : "
						read proceed
						case ${proceed} in
							[yY]|[Yy][eE][sS])
								$uninstall unixodbc 1> /dev/null 2> /dev/null
								dpkg --purge unixodbc 1> /dev/null 2> /dev/null
								sudo dpkg --force-all -P unixodbc 1> /dev/null 2> /dev/null
								rm -f /etc/odbc.ini /etc/odbcinst.ini 1> /dev/nulll 2> /dev/null
								rm -f /etc/odbc.ini.sg /etc/odbcinst.ini.sg 1> /dev/nulll 2> /dev/null
								echo 
								echo " UnixODBC is successfully uninstalled."
								echo
								break;;
							[nN]|[nN][oO])
								echo
								break;;
							*)
								echo " Please enter [y/n] to proceed"
								continue;;
						esac
					done ;;	
				q)
					Exit;;
				*)
					echo " Please enter the right number to proceed "
					continue;;
			esac
			break;
		done
	done
}

list_unsupported_packages()
{
	outside=0
	if [ ${mysql_server_status} -eq 3 ]; then
		echo
		if [ -f /etc/lsb-release ]  && [ "`egrep -i "SUSE" /proc/version`"  = "" ]; then
			echo -n "\033[1;2mMYSQL-SERVER :\033[0m"
			echo "\033[0;31m If MySQL server is already installed through \"source\" make sure that it is running with \"InnoDB\" as DEFAULT engine. VembuBDR  Server requires \"InnoDB\" as DEFAULT engine. Make sure that the following information is available in mysql confiuration file \"/etc/my.cnf\".\033[0m" 
			echo "=================================" 
			echo " socket = /tmp/mysql.sock"
			echo " user = root"
			echo " default-storage_engine = innodb"
			echo " max_connections = 800"
			echo " slow_query_log = 1"
			echo " innodb_log_file_size = 512M"
			echo " innodb_flush_method = O_DIRECT"
			echo " wait_timeout = 604800"
			echo " skip-locking"
			echo "================================="
 			echo "\033[1;2mResolution:\033[0m\033[0;31m Please add the above information under [mysqld] section in \"/etc/my.cnf\" file and restart the MySQL server \"/etc/init.d/mysql restart\".\033[0m"
		elif [ -f "/etc/debian_version" ]; then
			echo -ne "\033[1;2mMYSQL-SERVER :\033[0m"
			echo -e "\033[0;31m If MySQL server is already installed through \"source\" make sure that it is running with \"InnoDB\" as DEFAULT engine. VembuBDR  Server requires \"InnoDB\" as DEFAULT engine. Make sure that the following information is available in mysql confiuration file \"/etc/my.cnf\".\033[0m"
			echo "=================================" 
			echo " socket = /tmp/mysql.sock"
			echo " user = root"
			echo " default-storage_engine = innodb"
			echo " max_connections = 800"
			echo " slow_query_log = 1"
			echo " innodb_log_file_size = 512M"
			echo " innodb_flush_method = O_DIRECT"
			echo " wait_timeout = 604800"
			echo " skip-locking"
			echo "================================="
			echo -e "\033[1;2mResolution:\033[0m\033[0;31m Please add the above information under [mysqld] section in \"/etc/my.cnf\" file  and restart the MySQL server \"/etc/init.d/mysql restart\".\033[0m"
		else
			echo -ne "\033[1;2mMYSQL-SERVER :\033[0m"
			echo -e "\033[0;31m If MySQL server is already installed through \"source\" make sure that it is running with \"InnoDB\" as DEFAULT engine. VembuBDR  Server requires \"InnoDB\" as DEFAULT engine. Make sure that the following information is available in mysql confiuration file \"/etc/my.cnf\".\033[0m"
			echo "=================================" 
			echo " socket = /tmp/mysql.sock"
			echo " user = root"
			echo " default-storage_engine = innodb"
			echo " max_connections = 800"
			echo " slow_query_log = 1"
			echo " innodb_log_file_size = 512M"
			echo " innodb_flush_method = O_DIRECT"
			echo " wait_timeout = 604800"
			echo " skip-locking"
			echo "================================="
			echo -e "\033[1;2mResolution:\033[0m\033[0;31m Please add the above information under [mysqld] section in \"/etc/my.cnf\" file and restart the MySQL server \"/etc/init.d/mysql restart\".\033[0m"
		fi
		echo
		outside=1
	fi
	
	if [ ${unixodbc_status} -eq 2 ]; then
		echo
		if [ -f /etc/lsb-release ]  && [ "`egrep -i "SUSE" /proc/version`"  = "" ]; then
			echo "Unixodbc should be 32bit and it should be configured with \"--enable-threads=yes\". If it was already installed using package management utility, please uninstall any such existing unixodbc package(s) and try to install it using this $0 installer. To uninstall package Execute \"dpkg --remove package-name\" and \"dpkg --purge package-name\" or \"apt-get remove package-name\" as a 'root' user where package-name is 'unixodbc' or 'unixodbc-devel'"
			echo
		elif [ -f /etc/debian_version ]; then
			echo -e "Unixodbc should be 32bit and it should be configured with \"--enable-threads=yes\".  If it was already installed using package management utility, please uninstall any such existing unixodbc package(s) and try to install it using this $0 installer. To uninstall package Execute \"dpkg --remove package-name\" and \"dpkg --purge package-name\" or \"apt-get remove package-name\" as a 'root' user where package-name is 'unixodbc' or 'unixodbc-devel'"
			echo
		else
			echo -e "Unixodbc should be 32bit and it should be configured with \"--enable-threads=yes\".  If it was already installed using package management utility, please uninstall any such existing unixodbc package(s) and try to install it using this $0 installer. To uninstall package Execute \"rpm -ev --nodeps package-name\" or \"yum remove package-name\" as a 'root' user where package-name is 'unixodbc' or 'unixodbc-devel'"
			echo
		fi
		outside=1
	fi
			
	if [ ${mysql_server_status} -eq 2 ] || [ ${mysql_client_status} -eq 2 ]; then
		if [ -f /etc/lsb-release ]  && [ "`egrep -i "SUSE" /proc/version`"  = "" ]; then
			echo
			echo "NOTE: VembuBDR requires MySQL version $MYSQL_VERSION and its appropriate components. If any of the mysql packages are already installed using package management utility \"apt-get\" or if the version of the packages are different from MySQL-$MYSQL_VERSION, installation of MySQL package(s) will not go through. Therefore, please uninstall any such existing mysql package(s) and then try to running this $0 installer again. To uninstall such package(s) Execute : \"dpkg --remove package-name\" and \"dpkg --purge package-name\" or \"apt-get remove package-name\" as a root user where package-name is 'mysql', 'mysql-libs', 'mysql-devel', 'mysql-server', 'mysql-client' or 'mysql-common'."
			echo
		elif [ -f "/etc/debian_version" ]; then
			echo
			echo -e "NOTE: VembuBDR requires MySQL version $MYSQL_VERSION and its appropriate components. If any of the mysql packages are already installed using package management utility \"apt-get\" or if the version of the packages are different from MySQL-$MYSQL_VERSION, installation of MySQL package(s) will not go through. Therefore, please uninstall any such existing mysql package(s) and then try to running this $0 installer again. To uninstall such package(s) Execute: \"dpkg --remove package-name\" and \"dpkg --purge package-name\" or \"apt-get remove package-name\" as a root user where package-name is 'mysql', 'mysql-libs', 'mysql-devel', 'mysql-server', 'mysql-client' or 'mysql-common'."
			echo
		else
			echo
			echo -e "NOTE: VembuBDR requires MySQL version $MYSQL_VERSION and its appropriate components. If any of the mysql packages are already installed using package management utility \"yum\" or if the version of the packages are different from MySQL-$MYSQL_VERSION, installation of MySQL package(s) will not go through. Therefore, please uninstall any such existing mysql package(s) and then try to running this $0 installer again. To uninstall such package(s) Execute :  \"rpm -ev --nodeps package-name\"  or \"rpm -ev --nodeps \`rpm -q --whatprovides package-name\`\" or \"yum remove package-name\" as a root user where package-name is 'mysql', 'mysql-libs', 'mysql-devel', 'mysql-server', 'mysql-client' or 'mysql-common'."
			echo
		fi
		outside=1
	fi
	
	if [ $outside -eq 1 ]; then
		Exit
	fi
}

redhat_mysql_server_status()
{
	#if [ ${mysql_server_status} -ne 2 ]; then
		if [ "`rpm -q --whatprovides mysql-server`" = "${RH_MYSQL_SERVER}" ]; then
			echo -n " MySQL-server	       :  "
			echo -e "\033[0;32minstalled\033[0m"
			mysql_server_status=1
		elif [ "`rpm -q --whatprovides mysql-server`" = "${RH_MYSQL_SERVER_ARCH}" ]; then
			echo -n " MySQL-server	       :  "
			echo -e "\033[0;32minstalled\033[0m"
			mysql_server_status=1
		elif [ "`rpm -q --whatprovides mysql-server`" = "${RH_MYSQL_SERVER_ARCH64}" ]; then
			echo -n " MySQL-server	       :  "
			echo -e "\033[0;32minstalled\033[0m"
			mysql_server_status=1
		elif [ "`rpm -q --whatprovides mysql-server`" = "${RH_MYSQL_SERVER55}" ]; then
			echo -n " MySQL-server	       :  "
			echo -e "\033[0;32minstalled\033[0m"
			mysql_server_status=1
		elif [ "`rpm -q --whatprovides mysql-server`" = "${RH_MYSQL_SERVER55_ARCH}" ]; then
			echo -n " MySQL-server	       :  "
			echo -e "\033[0;32minstalled\033[0m"
			mysql_server_status=1
		elif [ "`rpm -q --whatprovides mysql-server`" = "${RH_MYSQL_SERVER55_ARCH64}" ]; then
			echo -n " MySQL-server	       :  "
			echo -e "\033[0;32minstalled\033[0m"
			mysql_server_status=1
		elif [ -f "/usr/bin/mysqld_safe" ] || [ -f "/usr/sbin/mysqld" ]; then
			check_mysql_version
			echo -n " mysql-server	       :  "			
			if [ "$mysql_compat" = "1" ]; then
				echo -e "\033[0;32minstalled\033[0m"
				mysql_server_status=1
			else
				echo -e "Already installed using package-management utility \"yum\"(Incompatible)"
				mysql_server_status=2
			fi
		elif [ -f "/usr/local/bin/mysqld_safe" ]; then
			echo -n " MySQL-server	       :  "
			echo -e "Already installed using \"source\""
			mysql_server_status=3
		fi
	#fi
	
	if [ "$mysql_compat" != "1" ]; then
		rpm -q --whatprovides mysql-libs 1> /dev/null 2> /dev/null
		if [ $? -eq 0 ]; then
				echo -n " mysql-libs	       :  "
				echo -e "Already installed using package-management utility \"yum\" (Incompatible)"
				mysql_server_status=2
				mysql_client_status=2
		fi

		rpm -q --whatprovides mysql-devel 1> /dev/null 2> /dev/null
		if [ $? -eq 0 ]; then
				echo -n " mysql-devel	       :  "
				echo -e "Already installed using package-management utility \"yum\" (Incompatible)"
				mysql_server_status=2
				mysql_client_status=2
		fi
	fi
	
	export mysql_server_status
}

redhat_mysql_client_status()
{
	#if [ ${mysql_client_status} -ne 2 ]; then
		if [ "`rpm -q --whatprovides mysql-client`" = "${RH_MYSQL_CLIENT}" ]; then
			echo -n " MySQL-client	       :  "
			echo -e "\033[0;32minstalled\033[0m"
			mysql_client_status=1
		elif [ "`rpm -q --whatprovides mysql-client`" = "${RH_MYSQL_CLIENT_ARCH}" ]; then
			echo -n " MySQL-client	       :  "
			echo -e "\033[0;32minstalled\033[0m"
			mysql_client_status=1
		elif [ "`rpm -q --whatprovides mysql-client`" = "${RH_MYSQL_CLIENT_ARCH64}" ]; then
			echo -n " MySQL-client	       :  "
			echo -e "\033[0;32minstalled\033[0m"
			mysql_client_status=1
		elif [ "`rpm -q --whatprovides mysql-client`" = "${RH_MYSQL_CLIENT55}" ]; then
			echo -n " MySQL-client	       :  "
			echo -e "\033[0;32minstalled\033[0m"
			mysql_client_status=1
		elif [ "`rpm -q --whatprovides mysql-client`" = "${RH_MYSQL_CLIENT55_ARCH=}" ]; then
			echo -n " MySQL-client	       :  "
			echo -e "\033[0;32minstalled\033[0m"
			mysql_client_status=1
		elif [ "`rpm -q --whatprovides mysql-client`" = "${RH_MYSQL_CLIENT55_ARCH64}" ]; then
			echo -n " MySQL-client	       :  "
			echo -e "\033[0;32minstalled\033[0m"
			mysql_client_status=1
		elif [ -f "/usr/bin/mysql" ]; then
			echo -n " mysql	  	       :  "
			if [ "$mysql_compat" = "1" ]; then
				echo -e "\033[0;32minstalled\033[0m"
				mysql_client_status=1
			else
				echo -e "Already installed using package-management utility \"yum\"(Incompatible)"
				mysql_client_status=2
			fi						
		fi
		export mysql_client_status
	#fi
}

redhat_unixODBC_status()
{
	if [ -f /etc/mandrake-release ]; then
		rpm -q --whatprovides libunixODBC2 1> /dev/null 2> /dev/null
		if [ $? -eq 0 ]; then
			echo -n " libunixODBC2	       :  "
			echo -e "Already installed using package-management utility \"yum\"(Incompatible)"
			unixodbc_status=2
		elif [ "`rpm -q --whatprovides unixODBC`" = "${RH_UNIXODBC}" ]; then
			echo -n " unixodbc  	       :  "
			echo -e "\033[0;32minstalled\033[0m"
			unixodbc_status=1
		fi
	else
	if [ ${MACHINE_ARC} == 32 ]; then
		if [ "`rpm -q --whatprovides unixODBC`" = "${RH_UNIXODBC}" ]; then
			echo -n " unixodbc  	       :  "
			echo -e "\033[0;32minstalled\033[0m"
			unixodbc_status=1
		elif [ "`rpm -q --whatprovides unixODBC`" = "${RH_UNIXODBC_ARCH}" ]; then
			echo -n " unixodbc  	       :  "
			echo -e "\033[0;32minstalled\033[0m"
			unixodbc_status=1
		elif [ -f "/usr/bin/isql" ] || [ -f "/usr/bin/odbcinst" ]; then
			echo -n " unixodbc  	       :  "
			echo -e "Already installed using package-management utility \"yum\"(Incompatible)"
			unixodbc_status=2
		fi
	elif [ ${MACHINE_ARC} == 64 ]; then
		odbc64bitPkg=$(rpm -q --whatprovides unixODBC | grep -v "no package")
		if [ ${#odbc64bitPkg} -gt 0 ]; then
			isqlPath=$(whereis isql |cut -d " " -f2 ) 2>/dev/null
			which readelf 1> /dev/null 2> /dev/null
			if [ $? -eq 0 ]; then
				isqlArch=$(readelf -e ${isqlPath} | grep Class | awk {'print $2'} )
				if [ "${isqlArch}" == "ELF64" ]; then
					echo -e " unixodbc  	       :  \033[0;32minstalled\033[0m "
					unixodbc_status=1
				elif [ "${isqlArch}" == "ELF32" ]; then
					echo " unixodbc  	       :  Already installed using package-management utility \"yum\"(Incompatible)"
					unixodbc_status=2
				fi

			else
				isqlArch=$(file ${isqlPath} |cut -d " " -f3)
				if [ "${isqlArch}" == "64-bit" ]; then
					echo -e " unixodbc  	       :  \033[0;32minstalled\033[0m "
					unixodbc_status=1
				elif [ "${isqlArch}" == "32-bit" ]; then
					echo " unixodbc  	       :  Already installed using package-management utility \"yum\"(Incompatible)"
					unixodbc_status=2
				fi
			fi
		 fi
	  fi
	fi
	export unixodbcStatus
}

check_redhat_packages_status()
{
	clear
	echo "**********************************************"
	echo "  Checking For MySQL Package Compatibility "
	echo "**********************************************"
    echo " Please be patient. It could take a few minutes."
	echo 	
	redhat_mysql_server_status
	if [ ${mysql_server_status} -eq 0 ]; then
		echo -n " MySQL-server	       :  "
		echo -e "\033[1;31mnot installed\033[0m"
	fi
	
	redhat_mysql_client_status
	if [ ${mysql_client_status} -eq 0 ]; then
		echo -n " MySQL-client	       :  "
		echo -e "\033[1;31mnot installed\033[0m"
	fi
	
	if [ ! -f "/usr/local/lib64/libmyodbc3-${MYSQL_CONNECTOR_VERSION}.so" ]; then
		echo -n " MySQL-connector-odbc  :  "
		echo -e "\033[1;31mnot installed\033[0m"
	else
		echo -n " MySQL-connector-odbc  :  "
		echo -e "\033[0;32minstalled\033[0m"
		mysql_connector_status=1
		export mysql_connector_status
	fi
	
	redhat_unixODBC_status
	if [ ${unixodbc_status} -eq 0 ]; then
		echo -n " unixodbc  	       :  "
		echo -e "\033[1;31mnot installed\033[0m"
	fi
	
	if [ $mysql_server_status -eq 1 ] && [ $mysql_client_status -eq 1 ] && [ $mysql_connector_status -eq 1 ] && [ $unixodbc_status -eq 1 ]; then
		echo 
		all_packages_installed=1
		export all_packages_installed
		echo -n " MySQL packages are already installed in this machine."
		netstat -an |grep $MYSQL_PORT 1> /dev/null
		if [ $? -ne 0 ]; then
			echo " But MySQL server is down."
			while true; do
				echo -n " Do you want to start MySQL server now? [y/n]: "
				read proceed
				
					case ${proceed} in
						[yY]|[yY][eE][sS])
									mysql_package_folder=`basename ${MYSQL_DOWNLOADS}/mysql*.zip |cut -d"." -f1`
									cd  ${WRKDIR}/${MYSQL_DOWNLOADS}/${mysql_package_folder}
									mysql_server_configuration
									cd  ${WRKDIR}
									break;;
							[nN]|[nN][oO])
									Exit;;
									*)
									echo " Please enter [y/n] to start MySQL server."
									continue;;
					esac
			done
		else
			echo " MySQL server is up and running on port \"$MYSQL_PORT\"."
			echo
		fi
   fi
}

debian_mysql_server_status()
{
	#if [ ${mysql_server_status} -ne 2 ]; then
		MYSQL_SERVER_CURRENT_VERSION=`dpkg -s mysql-server |grep -w Version|cut -d":" -f2 | tr -d " "` 1> /dev/null 2> /dev/null		
		MYSQL_SERVER55_CURRENT_VERSION=`dpkg -s mysql |grep -w Version|cut -d":" -f2 | tr -d " "` 1> /dev/null 2> /dev/null
		
		if [ "${MYSQL_SERVER_CURRENT_VERSION}" = "${MYSQL_DBN_VERSION}" ]; then
			echo -n " MySQL-server	       : "
			if [ -f /etc/lsb-release ]  && [ "`egrep -i "SUSE" /proc/version`"  = "" ]; then
				echo "\033[0;32minstalled\033[0m"
			else
				echo -e "\033[0;32minstalled\033[0m"
			fi
			mysql_server_status=1
		elif [ "${MYSQL_SERVER55_CURRENT_VERSION}" = "${MYSQL55_DBN_VERSION}" ]; then	
			echo -n " MySQL-server	       : "
			if [ -f /etc/lsb-release ]  && [ "`egrep -i "SUSE" /proc/version`"  = "" ]; then
				echo "\033[0;32minstalled\033[0m"
			else
				echo -e "\033[0;32minstalled\033[0m"
			fi
			mysql_server_status=1

		elif [ "${MYSQL_SERVER55_CURRENT_VERSION}" = "${MYSQL5540_DBN_VERSION}" ]; then
			echo -n " MySQL-server         : "
			if [ -f /etc/lsb-release ]  && [ "`egrep -i "SUSE" /proc/version`"  = "" ]; then
					echo "\033[0;32minstalled\033[0m"
			else
					echo -e "\033[0;32minstalled\033[0m"
			fi
			mysql_server_status=1


		elif [ -f "/usr/bin/mysqld_safe" ] || [ -f "/opt/mysql/server-*/bin/mysqld_safe" ]; then
			check_mysql_version
			echo -n " MySQL-server	       : "
			if [ "$mysql_compat" = "1" ]; then
				if [ -f /etc/lsb-release ]  && [ "`egrep -i "SUSE" /proc/version`"  = "" ]; then
					echo "\033[0;32minstalled\033[0m"
				else
					echo -e "\033[0;32minstalled\033[0m"
				fi
				mysql_server_status=1
			else
				if [ -f /etc/lsb-release ]  && [ "`egrep -i "SUSE" /proc/version`"  = "" ]; then
					echo "Already installed using package-management utility \"apt-get\""
				else
					echo -e "Already installed using package-management utility \"apt-get\""
				fi
				mysql_server_status=2
			fi			
		elif [ -f "/usr/local/bin/mysqld_safe" ]; then
			echo -n " MySQL-server	       :  "
			if [ -f /etc/lsb-release ]  && [ "`egrep -i "SUSE" /proc/version`"  = "" ]; then
				echo "Already installed using \"source\""
			else
				echo -e "Already installed using \"source\""
			fi
			mysql_server_status=3
		fi
	#fi
	
	if [ -f /etc/debian_version ] && [ "$mysql_compat" != "1" ]; then  
		dpkg -S mysql-common 1> /dev/null 2> /dev/null
		if [ $? -eq 0 ]; then
				echo -n " mysql-common	       : "
				if [ -f /etc/lsb_release ] && [ "`egrep -i "SUSE" /proc/version`"  = "" ]; then
					echo "Already installed using package-management utility \"apt-get\""
				else
					echo -e "Already installed using package-management utility \"apt-get\""
				fi
				mysql_server_status=2
				mysql_client_status=2
		fi
	fi
	  
	export mysql_server_status
}

debian_mysql_client_status()
{
	if [ ${mysql_client_status} -ne 2 ]; then
		MYSQL_CLIENT_CURRENT_VERSION=`dpkg -s mysql-client |grep -w Version|cut -d":" -f2 | tr -d " "` 1> /dev/null 2> /dev/null
		MYSQL_CLIENT55_CURRENT_VERSION=`dpkg -s mysql |grep -w Version|cut -d":" -f2 | tr -d " "` 1> /dev/null 2> /dev/null
		if [ "${MYSQL_CLIENT_CURRENT_VERSION}" = "${MYSQL_DBN_VERSION}" ]; then
			echo -n " MySQL-client	       : "
			if [ -f /etc/lsb-release ]  && [ "`egrep -i "SUSE" /proc/version`"  = "" ]; then
				echo "\033[0;32minstalled\033[0m"
			else
				echo -e "\033[0;32minstalled\033[0m"
			fi	
			mysql_client_status=1
		elif [ "${MYSQL_CLIENT55_CURRENT_VERSION}" = "${MYSQL55_DBN_VERSION}" ]; then
			echo -n " MySQL-client	       : "
			if [ -f /etc/lsb-release ]  && [ "`egrep -i "SUSE" /proc/version`"  = "" ]; then
				echo "\033[0;32minstalled\033[0m"
			else
				echo -e "\033[0;32minstalled\033[0m"
			fi	
			mysql_client_status=1
		elif [ "${MYSQL_CLIENT55_CURRENT_VERSION}" = "${MYSQL5540_DBN_VERSION}" ]; then
			echo -n " MySQL-client	       : "
			if [ -f /etc/lsb-release ]  && [ "`egrep -i "SUSE" /proc/version`"  = "" ]; then
				echo "\033[0;32minstalled\033[0m"
			else
				echo -e "\033[0;32minstalled\033[0m"
			fi	
			mysql_client_status=1

		elif [ -f "/usr/bin/mysql" ] || [ -f "/opt/mysql/server-*/bin/mysql" ]; then			
			echo -n " MySQL-client	       : "
			if [ "$mysql_compat" = "1" ]; then
				if [ -f /etc/lsb-release ]  && [ "`egrep -i "SUSE" /proc/version`"  = "" ]; then
					echo "\033[0;32minstalled\033[0m"
				else
					echo -e "\033[0;32minstalled\033[0m"
				fi	
				mysql_client_status=1
			else
				if [ -f /etc/lsb-release ]  && [ "`egrep -i "SUSE" /proc/version`"  = "" ]; then
					echo "Already installed using package-management utility \"apt-get\""
				else
					echo -e "Aalready installed using package-management utility \"apt-get\""
				fi
				mysql_client_status=2
			fi
		fi
	fi
  	export mysql_client_status
}

debian_unixodbc_status()
{
	UNIXODBC_CURRENT_VERSION=`dpkg -s unixodbc |grep -w Version|cut -d":" -f2 |tr -d " "` 1> /dev/null 2> /dev/null
	UNIXODBC_CURRENT_VERSION_ARCH=`dpkg -s unixodbc:i386 |grep -w Version|cut -d":" -f2 |tr -d " "` 1> /dev/null 2> /dev/null
	UNIXODBC_CURRENT_VERSION_DEB_ARCH=`dpkg -s unixodbc:i686 |grep -w Version|cut -d":" -f2 |tr -d " "` 1> /dev/null 2> /dev/null
	
	if [ ! "${UNIXODBC_CURRENT_VERSION}" = "" ]; then
		echo -n " unixodbc  	       : "
		if [ -f /etc/lsb-release ]  && [ "`egrep -i "SUSE" /proc/version`"  = "" ]; then
       			echo "\033[0;32minstalled\033[0m"
		else
			echo -e "\033[0;32minstalled\033[0m"
		fi
		unixodbc_status=1
	elif [ "${UNIXODBC_CURRENT_VERSION}" = "" ]; then
		unixodbc_status=0
	elif [ ! "${UNIXODBC_CURRENT_VERSION_ARCH}" = "" ] || [ ! "${UNIXODBC_CURRENT_VERSION_DEB_ARCH}" = "" ]; then
		echo -n " unixodbc  	       : "
		if [ -f /etc/lsb-release ]  && [ "`egrep -i "SUSE" /proc/version`"  = "" ]; then
       			echo "Already installed using package-management utility \"apt-get\""
		else
			echo -e "Already installed using package-management utility \"apt-get\""
		fi
		unixodbc_status=1
	elif [ -f "/usr/bin/isql" ]; then	
        echo -n " unixodbc  	       : "
		if [ -f /etc/lsb-release ]  && [ "`egrep -i "SUSE" /proc/version`"  = "" ]; then
			echo "Already installed using package-management utility \"apt-get\""
		else
			echo -e "Already installed using package-management utility \"apt-get\""
		fi
		unixodbc_status=2
    fi
	export unixodbc_status
    
}

check_debian_packages_status()
{
	clear
	echo "*********************************************"	
	echo "  Checking For MySQL Package Compatibility "
	echo "*********************************************"
    echo " Please be patient. It could take a few minutes."
    echo 
	debian_mysql_server_status
	if [ ${mysql_server_status} -eq 0 ]; then
		echo -n " MySQL-server	       : "
		if [ -f /etc/lsb-release ]  && [ "`egrep -i "SUSE" /proc/version`"  = "" ]; then
			echo "\033[1;31mnot installed\033[0m"
		else
			echo -e "\033[1;31mnot installed\033[0m"
		fi
	fi
	
	debian_mysql_client_status
	if [ ${mysql_client_status} -eq 0 ]; then
		echo -n " MySQL client	       : "
		if [ -f /etc/lsb-release ]  && [ "`egrep -i "SUSE" /proc/version`"  = "" ]; then
			echo "\033[1;31mnot installed\033[0m"
		else
			echo -e "\033[1;31mnot installed\033[0m"
		fi
	fi
	if [ "${MACHINE_ARC}" = "32" ]; then
		if [ ! -f "/usr/local/lib/libmyodbc3-${MYSQL_CONNECTOR_VERSION}.so" ]; then
			echo -n " MySQL-connector-odbc  : "
			if [ -f /etc/lsb-release ]  && [ "`egrep -i "SUSE" /proc/version`"  = "" ]; then
				echo "\033[1;31mnot installed\033[0m"
			else
				echo -e "\033[1;31mnot installed\033[0m"
			fi
		else
			echo -n " MySQL-connector-odbc  : "
			if [ -f /etc/lsb-release ]  && [ "`egrep -i "SUSE" /proc/version`"  = "" ]; then
				echo "\033[0;32minstalled\033[0m"
			else
				echo -e "\033[0;32minstalled\033[0m"
			fi	
			mysql_connector_status=1
			export mysql_connector_status
		fi
	else
		if [ ! -f "/usr/local/lib64/libmyodbc3-${MYSQL_CONNECTOR_VERSION}.so" ]; then
			echo -n " MySQL-connector-odbc  : "
			if [ -f /etc/lsb-release ]  && [ "`egrep -i "SUSE" /proc/version`"  = "" ]; then
				echo "\033[1;31mnot installed\033[0m"
			else
				echo -e "\033[1;31mnot installed\033[0m"
			fi
		else
			echo -n " MySQL-connector-odbc  : "
			if [ -f /etc/lsb-release ]  && [ "`egrep -i "SUSE" /proc/version`"  = "" ]; then
				echo "\033[0;32minstalled\033[0m"
			else
				echo -e "\033[0;32minstalled\033[0m"
			fi	
			mysql_connector_status=1
			export mysql_connector_status
		fi
	fi
	
	debian_unixodbc_status
	if [ ${unixodbc_status} -eq 0 ]; then
		echo -n " unixodbc	       : "
		if [ -f /etc/lsb-release ]  && [ "`egrep -i "SUSE" /proc/version`"  = "" ]; then
			echo "\033[1;31mnot installed\033[0m"
		else
			echo -e "\033[1;31mnot installed\033[0m"
		fi
	fi
	
	if [ $mysql_server_status -eq 1 ] && [ $mysql_client_status -eq 1 ] && [ $mysql_connector_status -eq 1 ] && [ $unixodbc_status -eq 1 ]; then
		echo 
		all_packages_installed=1
		export all_packages_installed
		echo -n " MySQL packages are already installed in this machine."
		netstat -an |grep $MYSQL_PORT 1> /dev/null
		if [ $? -ne 0 ]; then
			echo " But MySQL server is down."
			while true; do
				echo -n " Do you want to start MySQL server now? [y/n]: "
				read proceed
				
					case ${proceed} in
						[yY]|[yY][eE][sS])
									mysql_package_folder=`basename ${MYSQL_DOWNLOADS}/mysql*.zip |cut -d"." -f1`
									cd  ${WRKDIR}/${MYSQL_DOWNLOADS}/${mysql_package_folder}
									mysql_server_configuration
									cd  ${WRKDIR}
									break;;
							[nN]|[nN][oO])
									Exit;;
									*)
									echo " Please enter [y/n] to start MySQL server."
									continue;;
					esac
			done
		else
			echo " MySQL server is up and running on port \"$MYSQL_PORT\"."
			echo
		fi
   fi
}

download()
{
	if [ $mysql_server_status -eq 3 ] || [ $mysql_server_status -eq 2 ] || [ $mysql_client_status -eq 2 ] || [ $unixodbc_status -eq 2 ]; then
		list_unsupported_packages
	elif [ ${mysql_server_status} -eq 0 ] || [ ${mysql_client_status} -eq 0 ] || [ ${mysql_connector_status} -eq 0 ] || [ ${unixodbc_status} -eq 0 ]; then
		echo
		echo "Package Compatibility check completed successfully."
		echo 
		echo "*************************************************************" 
		echo " Downloading MySQL Server and Connector/ODBC driver packages "	
		echo "*************************************************************"
		echo
		cd ${MYSQL_DOWNLOADS}
		if [ ! -f "${BUILD_INFO_FILE}" ]; then
			echo " ${BUILD_INFO_FILE} file is missing in ${MYSQL_DOWNLOADS}. Please verify the downloaded file size."
			Exit
		else
			mysql_download_link=`cat ${BUILD_INFO_FILE} |grep -w "$1" |cut -d"=" -f2`
			mysql_package_zip=`basename $mysql_download_link`
			mysql_package_folder=`basename $mysql_download_link |cut -d"." -f1 |tr -d " "`
			export mysql_package_folder
	
			if [ -d "$mysql_package_folder" ]; then
				echo " The file is already fully retrieved; nothing to do."
			else
				wget -c ${mysql_download_link}
				mysql_package_name=`ls mysql-*.zip`
				"$UNZIP" -o ${mysql_package_name} >> /dev/null
				if [ "$?" -gt 1 ]; then
					echo " Unable to extract the downloaded mysql zip file. Please verify the downloaded file size."
					Exit
				fi
			fi
			download_odbc_zip
			if [ -f /etc/debian_version ]; then
				if [ ${mysql_server_status} -eq 0 ] &&  [ ${mysql_server_status} -ne 2 ] && [ ${mysql_client_status} -ne 2 ]; then
					Installing_mysql_server mysql-5.5
				fi
			else
				if [ ${mysql_server_status} -eq 0 ] &&  [ ${mysql_server_status} -ne 2 ] && [ ${mysql_client_status} -ne 2 ]; then
					Installing_mysql_server Mysql-server
				fi
				if [ ${mysql_client_status} -eq 0 ] &&  [ ${mysql_server_status} -ne 2 ] && [ ${mysql_client_status} -ne 2 ]; then
					Installing_mysql_client Mysql-client
				fi
			fi
			if [ ${mysql_connector_status} -eq 0 ]; then
				Installing_mysql_connector 1
			fi
			if [ ${unixodbc_status} -eq 0 ]; then
				Installing_unixodbc unixODBC
			fi
	
			if [ $mysql_server_status -eq 3 ] || [ $mysql_server_status -eq 2 ] || [ $mysql_client_status -eq 2 ] || [ $unixodbc_status -eq 2 ]; then
				list_unsupported_packages
			else
				odbc_configuration
			fi
			
		fi
		cd ${WRKDIR}		
	fi
}

download_and_install_storegrid()
{
	clear
	echo
	echo "		#####################################################"
	if [ -f /etc/lsb-release ] && [ "`egrep -i "SUSE" /proc/version`"  = "" ]; then
		
		echo  "		\033[1;34m VembuBDR $SGVN Server installation\033[0m"	
	else
		
		echo -e "		\033[1;34m VembuBDR $SGVN Server installation\033[0m"
	fi	
	echo "		#####################################################"
	echo
	echo "	=========================================================================="		
	echo "	|									 |"	
	echo "	| We will now take you through the VembuBDR  Server Installation Process.|"
	echo "	| VembuBDR is proprietary sotware of Vembu Technologies Inc and is       |"
	echo "	| licensed under its own terms which you are required to accept for this |"				
	echo "	| installation.								 |"	
	echo "	|									 |"	
	echo "	| If you would like to install VembuBDR, Please enter yes to proceed.    |"
	echo "	|									 |"	
	echo "	=========================================================================="		
	while true; do
	echo -n "  	Do you want to proceed [yes/no]: "
	read proceed

		case ${proceed} in
		[yY]|[yY][eE][sS])
						break;;
			[nN]|[nN][oO])
						Exit;;
						*)
						echo " Please enter [y/n] to procced installation."
						continue;;
		esac
	done
	echo
	echo " Downloading VembuBDR Now...."
	echo
	
	if [ ! -d "${WRKDIR}/${STOREGRID_DOWNLOADS}" ]; then
		mkdir -p ${WRKDIR}/${STOREGRID_DOWNLOADS} 1> /dev/null 2> /dev/null
	else
		currentTimeStamp=`date +%s`
		mv ${WRKDIR}/${STOREGRID_DOWNLOADS} ${WRKDIR}/$STOREGRID_DOWNLOADS$currentTimeStamp
		mkdir -p ${WRKDIR}/${STOREGRID_DOWNLOADS} 1> /dev/null 2> /dev/null
	fi
	
	cd ${WRKDIR}/${STOREGRID_DOWNLOADS}
	
	if [ ! -f "../${MYSQL_DOWNLOADS}/${BUILD_INFO_FILE}" ]; then
		echo " ${BUILD_INFO_FILE} file is missing in ${MYSQL_DOWNLOADS} folder. Please verify the downloaded file size."
		Exit
	else
		#VembuBDR Downloads here
		echo "$1"
		storegrid_package_link=`cat ../${MYSQL_DOWNLOADS}/${BUILD_INFO_FILE} |grep -w "$1" |cut -d"=" -f2`
		#echo "storegrid_package_link :: $storegrid_package_link"
		storegrid_zip_file=`basename $storegrid_package_link`
		#echo "storegrid_zip_file :: $storegrid_zip_file"
		storegrid_bin_file=`ls *.bin 2> /dev/null`
		if [ -f "$storegrid_bin_file" ]; then
			echo " The file is already fully retrieved; nothing to do."
		else
			wget -c ${storegrid_package_link}
			"$UNZIP" -o ${storegrid_zip_file} >> /dev/null
			if [ "$?" -gt 1 ]; then
				echo " Unable to extract the downloaded VembuBDR zip file. Please verify the downloaded file size."
				Exit
			fi
		fi

		#VembuBDR Installs here
		storegrid_bin_file=`ls *.bin 2> /dev/null`
		cd ${WRKDIR}
		if [ "${storegrid_bin_file}" = "" ]; then
      		echo "Vembu VembuBDR binary not available. Please verify the installation directory."
			Exit
   		else
			if [ "$odbc_database" = "" ] && [ "$odbc_hostname" = "" ] && [ "$odbc_username" = "" ] && [ "$odbc_password" = "" ]; then
					${RUN} ${WRKDIR}/${STOREGRID_DOWNLOADS}/${storegrid_bin_file}
			else
				${RUN} ${WRKDIR}/${STOREGRID_DOWNLOADS}/${storegrid_bin_file} dsname=sgserver hostname=$odbc_hostname databasename=$odbc_database username=$odbc_username password=$odbc_password
			fi
      		if [ -f "${WRKDIR}/SGLoc.conf" ]; then
	 			if [ ! -s ${WRKDIR}/SGLoc.conf ]; then
	    			/bin/rm -f ${WRKDIR}/SGLoc.conf 1>/dev/null 2>/dev/null
				else
					 STOREGRID_HOME=`cat ${WRKDIR}/SGLoc.conf` 
	   				 perl -w -e "s:SG_SHOW_PR_WINDOW = 0:SG_SHOW_PR_WINDOW = 1:g" -p -i ${STOREGRID_HOME}/Vembu/VembuBDR/sgchroot/htmlgui/conf/config.inc
	    			 perl -w -e "s:SG_BLD_IDENTIFIER = '':SG_BLD_IDENTIFIER = '${download_source}':g" -p -i ${STOREGRID_HOME}/Vembu/VembuBDR/sgchroot/htmlgui/conf/config.inc
	    			/bin/rm -f ${WRKDIR}/SGLoc.conf 1>/dev/null 2>/dev/null
         		fi
			fi
 		fi	
	fi
}

Install_32bit_mysql_packages()
{
	if [ -f /etc/debian_version ]; then
		check_debian_packages_status
		if [ $all_packages_installed -eq 0 ]; then
			download mysql-DebianUbuntu-32bit
		else
			if  [ $mysql_server_status -eq 3 ] || [ $mysql_server_status -eq 2 ] || [ $mysql_client_status -eq 2 ] || [ $unixodbc_status -eq 2 ]; then
				list_unsupported_packages
			else
				odbc_configuration
			fi
		fi
	else
		if [ ! -f "/lib/libtinfo.so.5" ]; then
			cd ${MYSQL_DOWNLOADS}
			download_odbc_zip
			cp -f odbc/libtinfo.so.5 /lib/ >> /dev/null
			rm -f odbc/libtinfo.so.5 >> /dev/null
			cd ${WRKDIR}
		fi
		check_redhat_packages_status
		if [ $all_packages_installed -eq 0 ]; then
			if [ -f /etc/mandrake-release ]; then
				download mysql-Mdk-32bit
			else
				download mysql-RedHat-32bit
			fi
		else
			if  [ $mysql_server_status -eq 3 ] || [ $mysql_server_status -eq 2 ] || [ $mysql_client_status -eq 2 ] || [ $unixodbc_status -eq 2 ]; then
				list_unsupported_packages
			else
				odbc_configuration
			fi
		fi
	fi
}

Install_64bit_mysql_packages()
{
	if [ -f /etc/debian_version ]; then
		check_debian_packages_status
		if [ $all_packages_installed -eq 0 ]; then
			download mysql-DebianUbuntu-64bit
		else
			if [ $mysql_server_status -eq 3 ] || [ $mysql_server_status -eq 2 ] || [ $mysql_client_status -eq 2 ] || [ $unixodbc_status -eq 2 ]; then
				list_unsupported_packages
			else
				odbc_configuration
			fi
		fi
	else
		if [ ! -f "/lib/libtinfo.so.5" ]; then
			cd ${MYSQL_DOWNLOADS}
			download_odbc_zip
			cp -f odbc/libtinfo.so.5 /lib/ >> /dev/null
			rm -f odbc/libtinfo.so.5 >> /dev/null
			cd ${WRKDIR}
		fi
		check_redhat_packages_status
		if [ $all_packages_installed -eq 0 ]; then
			if [ -f /etc/mandrake-release ]; then
				download mysql-Mdk-64bit
			else
				download mysql-RedHat-64bit
			fi
		else
			if  [ $mysql_server_status -eq 3 ] || [ $mysql_server_status -eq 2 ] || [ $mysql_client_status -eq 2 ] || [ $unixodbc_status -eq 2 ]; then
				list_unsupported_packages
			else
				odbc_configuration
			fi
		fi
	fi
}

Install_32bit_StoreGrid()
{
	if [ -f /etc/debian_version ]; then
		echo "Debian version 32 bit storegrid is called "
		download_and_install_storegrid Sg-DebianUbuntu-32bit
	elif [ -f /etc/mandrake-release ]; then
		download_and_install_storegrid Sg-Mdk-32bit
	else
		download_and_install_storegrid Sg-RedHat-32bit
	fi
}

Install_64bit_StoreGrid()
{
	if [ -f /etc/debian_version ]; then
		echo "Debian version 64 bit storegrid is called "
		download_and_install_storegrid Sg-DebianUbuntu-64bit
	elif [ -f /etc/mandrake-release ]; then
		download_and_install_storegrid Sg-Mdk-64bit
	else
		download_and_install_storegrid Sg-RedHat-64bit
	fi
}

mysql_installations()
{
	if [ "`uname -s`" = "Linux" ]; then
		Welcome
		MySQL_Disclaimer
		SE_Linux_Settings
		if [ `/usr/bin/getconf LONG_BIT` -eq 32 ]; then
			Install_32bit_mysql_packages		
		elif [ `/usr/bin/getconf LONG_BIT` -eq 64 ]; then
			Ask_MySQLAndMongoDBPath MySQL
			Install_64bit_mysql_packages
		else
			echo " Unknown System Architecture: This Online installer will support only on 32bit and 64bit Linux systems."
			Exit
		fi
	else
		echo " This Online installer will support only on Linux operating systems Like RHEL, Suse, centOS, fedora, mandrake, Debian and Ubuntu."
		Exit
	fi
}

storegrid_installations()
{
	if [ "`uname -s`" = "Linux" ]; then
		if [ `/usr/bin/getconf LONG_BIT` -eq 32 ]; then
			Install_32bit_StoreGrid		
		elif [ `/usr/bin/getconf LONG_BIT` -eq 64 ]; then
			Install_64bit_StoreGrid
		else
			echo " Unknown System Architecture: This Online installer will support only on 32bit and 64bit Linux systems."
			Exit
		fi
	else
		echo " This Online installer will support only on Linux operating systems Like RHEL, Suse, centOS, fedora, mandrake, Debian and Ubuntu."
		Exit
	fi
}

Mongolicensce()
{
echo >&2 "
####################################################################################################################
		 				\033[33mAbout MongoDB \033[0m			  
####################################################################################################################
					                                  
\033[1mMongoDB\033[0m is a free software that is licensed by 10gen Inc.,under the \033[1m GNU Affero General Public License 2\033[0m. 
A link to the copy of that license is available below. You can find the link to a copy of the complete corresponding source code below. 

Do not install \033[1mMongoDB\033[0m, if the terms of the said license are not acceptable to you.              
		
GNU Affero General Public License v3 \033[1;34m<http://www.gnu.org/licenses/agpl-3.0.html>\033[0m

MongoDB source code\033[1;34m <http://www.mongodb.org/display/DOCS/Source+Code >\033[0m

####################################################################################################################
"
}

Check_MongoDBInstall()
{
retVal=0
	if [ -f /opt/config/mongodb ] || [ -d /opt/mongodb ]; then
	   if [ -f /opt/GNU-AGPL-3.0 ]; then
			if [ -f /etc/lsb-release ]  && [ "`egrep -i "SUSE" /proc/version`"  = "" ]; then
				echo "\n\033[0;32m MongoDB is already installed ... \033[0m \n"
				echo "checking the mongo version"
				/opt/mongodb/bin/mongo --version >> /etc/init.d/versionchecking.txt
				chmod 755 /etc/init.d/versionchecking.txt
				previousVersion=`cat /etc/init.d/versionchecking.txt | grep "version" | cut -d ":" -f2`
					if [ "${previousVersion}" = "3.2.0" ]  ;then
					retVal=1;
					fi
			else
				echo "\n\033[0;32m MongoDB is already installed ... \033[0m \n"
				echo "checking the mongo version"
				/opt/mongodb/bin/mongo --version >> /etc/init.d/versionchecking.txt
				chmod 755 /etc/init.d/versionchecking.txt
				previousVersion=`cat /etc/init.d/versionchecking.txt | grep "version" | cut -d ":" -f2`
			fi
				echo "Installed mongo version ******${previousVersion}********"
				rm -rf /etc/init.d/versionchecking.txt
				
	    else
			if [ -f /etc/lsb-release ]  && [ "`egrep -i "SUSE" /proc/version`"  = "" ]; then
				echo "\033[0;31m Unable to install MongoDB. Another application is running  \033[0m"
			else
			echo -e "\033[0;31m Unable to install MongoDB. Another application is running  \033[0m"
			fi
			#if [ "${previousVersion}" = "3.2.0" ]  ;then
		
				Exit
			#fi
	    fi
		#return 1
	fi	
return ${retVal};
}

End_MongoDBInstall()
{
	if [ ${SIMPLE_ONLINE_INSTALLER} -eq 1 ]; then
	while true; do
			echo "\nDo you want to install VembuBDR's backup server Now ? say [y/n] : \c"
				read urChoice
				case ${urChoice} in
					[yY]|[yY][eE][sS])
					break;;	
					[nN]|[nN][oO])
						Exit;;
					*)
					echo "Please provide correct input [y/n] to proceed."
					echo
					continue;;
				esac
			done
			break
	fi
}
Proceed_MongoDBInstall()
{
	Mongolicensce
	while true; do
		echo -n "Do you want to proceed to install MongoDB ? say [y/n] : "
			read urChoice
			case ${urChoice} in
				[yY]|[yY][eE][sS])
				break;;	
				[nN]|[nN][oO])
					Exit;;
				*)
				echo "Please provide correct input [y/n] to proceed."
				echo
				continue;;
			esac
		done
		break
}

Install_MongoDB()
{
	
	Check_MongoDBInstall
	if [ $? -eq 1 ]; then
		return 1
	fi

	clear

	if [ "${previousVersion}" = " 2.4.5" ] || [ "${previousVersion}" = " 2.6.3" ] || [ "${previousVersion}" = " 2.6.8" ]; then
	
	#if [ [ ${previousVersion} = "2.4.5" ] || [ ${previousVersion} = "2.6.3" ] || [ ${previousVersion} = "2.6.8" ] ]; then
	echo "\n\033[0;32m MongoDB version ${previousVersion} is already installed. Now upgrading to 3.2.0 \033[0m \n"
	echo "Before aborting the installation.... mongo upgrade"
	Ask_MySQLAndMongoDBPath MongoDB
	#stopping vembubdr services
	/etc/init.d/vembubdr stop
	#stopping previous mongo
        /etc/init.d/mongo stop
	sleep 3
        rm -rf /etc/init.d/mongo
        mongoDBFolder="MongoDB"
        userName="mongodb"
        serviceName="mongo"
        mongo_install_path="/opt"
       	if [  -d  "${WRKDIR}/${MONGODB_DOWNLOADS:=MongoDB-downloads}" ]; then
	rm -rf ${WRKDIR}/${MONGODB_DOWNLOADS:=MongoDB-downloads}
	fi

	[ ! -d  "${WRKDIR}/${MONGODB_DOWNLOADS:=MongoDB-downloads}" ] && mkdir -p "${WRKDIR}/${MONGODB_DOWNLOADS}"
        cd  ${WRKDIR}/${MONGODB_DOWNLOADS}
	mongoDBURL=$(grep -rni "MongoDB=" ../${MYSQL_DOWNLOADS}/${BUILD_INFO_FILE} |cut -d"=" -f2)
	[ -f "${mongoDBFolder}.zip" ] &&  rm -rf ${mongoDBFolder}.zip 
	wget -c ${mongoDBURL}
        [ -d ${mongoDBFolder} ] &&  rm -rf ${mongoDBFolder}
	if [ -f /etc/lsb-release ]  && [ "`egrep -i "SUSE" /proc/version`"  = "" ]; then
                ${CECHO:=echo} "Extracting ${mongoDBFolder}.zip file...\n\n"
		
        else
        ${CECHO:=echo -e} "Extracting ${mongoDBFolder}.zip file...\n\n"
	fi
        ${UNZIP} "${mongoDBFolder}.zip" >> /dev/null
	
	#getting mongodb path from the previosu config/mongo file
	prevmongodbpath=`awk '/^dbpath/{print $NF}' /opt/config/mongodb`
	echo "The value of previous mongo db path is :: ${prevmongodbpath}"
        
	rm -rf ${mongo_install_path}/bin
	rm -rf ${mongo_install_path}/config
	rm -rf ${mongo_install_path}/GNU-AGPL-3.0
	rm -rf ${mongo_install_path}/mongodb
        #creating installation path.
	${CECHO} "Checking and creating MongoDB installation directories ...\n\n"
        if [ ! -f /etc/debian_version ]; then
                for dir in "${mongo_install_path}"/{,bin,config}
                do
                        [ ! -d $dir ] && { mkdir $dir || { echo "failed to create mongodb installation directory($dir)"; exit 1; }; }
                done
        else
                mkdir -p ${mongo_install_path}
                mkdir -p ${mongo_install_path}/config
                mkdir -p ${mongo_install_path}/bin
        fi
        #creating db storage.
        ${CECHO} "Checking and creating MongoDB log directories ...\n\n"
        if [ ! -d "${MONGO_DATA_DIRECTORY}/srv/db" ]; then
                mkdir -p "${MONGO_DATA_DIRECTORY}/srv/db" || { echo "failed to create mongodb log directory ($dir)"; exit 1; }
        fi
        #creating MongoDB logs.
	
        touch "${MONGO_DATA_DIRECTORY}/srv/db/mongodb.log" >> Mongoinstallation.log
        #copying files to /opt/mongodb/
        ${CECHO} "Copying MongoDB files ...\n\n"
        mv -f ${mongoDBFolder}/mongodb-linux-x86_64-3.2.0 ${mongo_install_path}/mongodb 2>> Mongoinstallation.log
        #copying files to /opt/bin/
        if [ ! -f /etc/debian_version ]; then
                for files in "${mongoDBFolder}"/{"${serviceName}"-start,"${serviceName}"-stop,"${serviceName}"-status}
                do
                        mv -f $files ${mongo_install_path}/bin
                done 2>> Mongoinstallation.log
        else
                mv -f ${mongoDBFolder}/${serviceName}-start ${mongo_install_path}/bin
                mv -f ${mongoDBFolder}/${serviceName}-stop ${mongo_install_path}/bin
                mv -f ${mongoDBFolder}/${serviceName}-status ${mongo_install_path}/bin
        fi
        #creating mongodb dameon
        {       mv -f ${mongoDBFolder}/mongodb ${mongo_install_path}/config/
                mv -f ${mongoDBFolder}/init-rpm.sh /etc/init.d/${serviceName}
                mv -f ${mongoDBFolder}/GNU-AGPL-3.0 /opt/
        } 2>> Mongoinstallation.log
        #changing mode and configuration here.
	if [ -f /etc/debian_version ]; then
#               #update-rc.d USERNAMEOFVSG start 98 2 3 4 5 . stop 99 0 1 6 .
                chmod -R 755  ${mongo_install_path} ${MONGO_DATA_DIRECTORY}
                chmod +x /etc/init.d/${serviceName}
                update-rc.d ${serviceName} defaults 1 >>/dev/null 2>> /dev/null
        else
                for cmd in chmod{" -R 755 ${mongo_install_path} ${MONGO_DATA_DIRECTORY}"," +x /etc/init.d/${serviceName}"}; do eval $cmd; done
                for cmd in chkconfig{" --add ${serviceName}"," ${serviceName} on"}; do eval $cmd; done
        fi
        # creating mongoDB user
        ${CECHO}  "checking and creating  ${userName} user...\n\n"
        user_exist=$(grep "^${userName}" /etc/passwd| cut -d":" -f1)
        if [ "${#user_exist}" -eq 0 ]; then
                useradd -M -r --home-dir ${mongo_install_path}/mongodb ${userName} || { echo "can not create user"; exit 1; }
        fi
        ${CECHO} "Changing db storage and log path in ${mongo_install_path}/config/mongodb ...\n\n"
	
        #editing the db path & log path
        eval "${SED:=sed -i} 's/dbPath/#dbPath/' ${mongo_install_path}/config/mongodb"
        eval "${SED} '/#dbPath/i \  dbPath: ${prevmongodbpath}' /opt/config/mongodb"
        eval "${SED} 's/path/#path/' ${mongo_install_path}/config/mongodb"
        eval "${SED} '/#path/i \  path: ${MONGO_DATA_DIRECTORY}/srv/db/mongodb.log' /opt/config/mongodb"
        eval "${SED} 's/bindIp/bindIp/' ${mongo_install_path}/config/mongodb"
        eval "${SED} 's/auth/#auth/' ${mongo_install_path}/config/mongodb"
        #changing storage path owner to mongodb and starting mongodb.
        eval "chown ${userName}:${userName} -R ${MONGO_DATA_DIRECTORY}/srv/db/ 1 >>/dev/null 2>> /dev/null"
        ${CECHO} "Starting MongoDB service ......\n"
        eval "/etc/init.d/${serviceName} start"
	#starting vembubdr		
	/etc/init.d/vembubdr start
	else
	if [ "${previousVersion}" = "" ]; then
	echo "\n\033[0;32m MongoDB Fresh Installation \033[0m \n"
		Proceed_MongoDBInstall
	
	Ask_MySQLAndMongoDBPath MongoDB
    	echo >&2 "
	
	***********************************************************************
				Installing MongoDB
	***********************************************************************
        "
	mongoDBFolder="MongoDB"
	userName="mongodb"
	serviceName="mongo"
	mongo_install_path="/opt"
	[ ! -d  "${WRKDIR}/${MONGODB_DOWNLOADS:=MongoDB-downloads}" ] && mkdir -p "${WRKDIR}/${MONGODB_DOWNLOADS}"
	cd  ${WRKDIR}/${MONGODB_DOWNLOADS}
	mongoDBURL=$(grep -rni "MongoDB=" ../${MYSQL_DOWNLOADS}/${BUILD_INFO_FILE} |cut -d"=" -f2)
	[ ! -f "${mongoDBFolder}.zip" ] &&  wget -c ${mongoDBURL}
	[ -d ${mongoDBFolder} ] &&  rm -rf ${mongoDBFolder}
	if [ -f /etc/lsb-release ]  && [ "`egrep -i "SUSE" /proc/version`"  = "" ]; then
		${CECHO:=echo} "Extracting ${mongoDBFolder}.zip file...\n\n"
	else
	${CECHO:=echo -e} "Extracting ${mongoDBFolder}.zip file...\n\n"
	fi
	${UNZIP} "${mongoDBFolder}.zip" >> /dev/null
	#creating installation path.
	${CECHO} "Checking and creating MongoDB installation directories ...\n\n"
	if [ ! -f /etc/debian_version ]; then
		for dir in "${mongo_install_path}"/{,bin,config}
		do
			[ ! -d $dir ] && { mkdir $dir || { echo "failed to create mongodb installation directory($dir)"; exit 1; }; }
		done 
	else
		mkdir -p ${mongo_install_path}
		mkdir -p ${mongo_install_path}/config
		mkdir -p ${mongo_install_path}/bin
	fi
	#creating db storage.
	${CECHO} "Checking and creating MongoDB storage directories ...\n\n"
	if [ ! -d "${MONGO_DATA_DIRECTORY}/srv/db/mongodb" ]; then
		mkdir -p "${MONGO_DATA_DIRECTORY}/srv/db/mongodb" || { echo "failed to create mongodb storage directory ($dir)"; exit 1; }
	fi
	#creating MongoDB logs.
	touch "${MONGO_DATA_DIRECTORY}/srv/db/mongodb.log" >> Mongoinstallation.log
	#copying files to /opt/mongodb/
	${CECHO} "Copying MongoDB files ...\n\n"
	mv -f ${mongoDBFolder}/mongodb-linux-x86_64-3.2.0 ${mongo_install_path}/mongodb 2>> Mongoinstallation.log
	#copying files to /opt/bin/
	if [ ! -f /etc/debian_version ]; then
		for files in "${mongoDBFolder}"/{"${serviceName}"-start,"${serviceName}"-stop,"${serviceName}"-status}
		do
			mv -f $files ${mongo_install_path}/bin
		done 2>> Mongoinstallation.log
	else
		mv -f ${mongoDBFolder}/${serviceName}-start ${mongo_install_path}/bin
		mv -f ${mongoDBFolder}/${serviceName}-stop ${mongo_install_path}/bin
		mv -f ${mongoDBFolder}/${serviceName}-status ${mongo_install_path}/bin
	fi
	#creating mongodb dameon 
	{	mv -f ${mongoDBFolder}/mongodb ${mongo_install_path}/config/ 
		mv -f ${mongoDBFolder}/init-rpm.sh /etc/init.d/${serviceName}
		mv -f ${mongoDBFolder}/GNU-AGPL-3.0 /opt/
	} 2>> Mongoinstallation.log
	#changing mode and configuration here.
	
	if [ -f /etc/debian_version ]; then
#		#update-rc.d USERNAMEOFVSG start 98 2 3 4 5 . stop 99 0 1 6 .
		chmod -R 755  ${mongo_install_path} ${MONGO_DATA_DIRECTORY}
		chmod +x /etc/init.d/${serviceName}
		update-rc.d ${serviceName} defaults 1 >>/dev/null 2>> /dev/null
	else
		for cmd in chmod{" -R 755 ${mongo_install_path} ${MONGO_DATA_DIRECTORY}"," +x /etc/init.d/${serviceName}"}; do eval $cmd; done
		for cmd in chkconfig{" --add ${serviceName}"," ${serviceName} on"}; do eval $cmd; done
	fi
	# creating mongoDB user
	${CECHO}  "checking and creating  ${userName} user...\n\n"
	user_exist=$(grep "^${userName}" /etc/passwd| cut -d":" -f1)
	if [ "${#user_exist}" -eq 0 ]; then
		useradd -M -r --home-dir ${mongo_install_path}/mongodb ${userName} || { echo "can not create user"; exit 1; }
	fi
	${CECHO} "Changing db storage and log path in ${mongo_install_path}/config/mongodb ...\n\n"
	#editing the db path & log path
	eval "${SED:=sed -i} 's/dbPath/#dbPath/' ${mongo_install_path}/config/mongodb"
	eval "${SED} '/#dbPath/i \  dbPath: ${MONGO_DATA_DIRECTORY}/srv/db/mongodb' /opt/config/mongodb"
	eval "${SED} 's/path/#path/' ${mongo_install_path}/config/mongodb"
	eval "${SED} '/#path/i \  path: ${MONGO_DATA_DIRECTORY}/srv/db/mongodb.log' /opt/config/mongodb"
	eval "${SED} 's/bindIp/bindIp/' ${mongo_install_path}/config/mongodb"
	eval "${SED} 's/auth/#auth/' ${mongo_install_path}/config/mongodb"
	#changing storage path owner to mongodb and starting mongodb.
	eval "chown ${userName}:${userName} -R ${MONGO_DATA_DIRECTORY}/srv/db/ 1 >>/dev/null 2>> /dev/null"
	${CECHO} "Starting MongoDB service ......\n"
	eval "/etc/init.d/${serviceName} start"
	fi 
	fi
}

MogoDB_installations()
{
	Install_MongoDB
	End_MongoDBInstall
}

Ask_MySQLAndMongoDBPath()
{
	# Reteriving Data directory Location for MySQL DB and MongoDB.
	if [ "${SG_METADBS_DIRPATH:-NOT_SET}" = "NOT_SET" ]; then
		dbPath=""
		[ -f "${WRKDIR}/dbinstallpath.txt" ] && { read dbPath < "${WRKDIR}/dbinstallpath.txt"; }
		if [ ${#dbPath} -eq 0 ]; then
			if [ -f /etc/lsb-release ] && [ "`egrep -i "SUSE" /proc/version`"  = "" ]; then
				echo "\033[35m\nVembuBDR will basically require 10% of the backup data storage for the internal meta-data store. Hence please assess and configure a storage medium appropriate to the storage requirements. Besides please ensure your drives have higher I/O performance.\n\033[0m"
			else
				echo -e "\033[35m\nVembuBDR will basically require 10% of the backup data storage for the internal meta-data store. Hence please assess and configure a storage medium appropriate to the storage requirements. Besides please ensure your drives have higher I/O performance.\n\033[0m"
			fi
			echo "Please specify the path."	
			while true; do
				read dbPath
				case ${dbPath} in 
				[/]*) SG_METADBS_DIRPATH=${dbPath}
					  #creating the parent directory directory.	
					 [ ! -d "${SG_METADBS_DIRPATH}" ] && { 
						mkdir -p "${SG_METADBS_DIRPATH}"
1>/dev/null 2>/dev/null || {
							echo "Directory[${SG_METADBS_DIRPATH}] is invalid or not accessible.Please specify valid path. " 
							continue 
							} 
					 }
					echo ${SG_METADBS_DIRPATH} > "${WRKDIR}/dbinstallpath.txt"
					break ;;
				 *) [ ! -z ${dbPath} ] && echo -e "\nPlease specify the full path." || echo "Please specify the path."
				 esac
			done
		else
			SG_METADBS_DIRPATH=${dbPath}
		fi

	fi
	tempDBPath="";
	case ${1:-empty} in 
		MySQL) 
		MYSQL_DATA_DIRECTORY="${SG_METADBS_DIRPATH}/MySQL"
		export MYSQL_DATA_DIRECTORY
		tempDBPath="${MYSQL_DATA_DIRECTORY}" ;;
		MongoDB) 
		MONGO_DATA_DIRECTORY="${SG_METADBS_DIRPATH}/MongoDB"
		# echo " what is the mongo data directory while upgrade : ${MONGO_DATA_DIRECTORY}"
		export MONGO_DATA_DIRECTORY
		tempDBPath="${MONGO_DATA_DIRECTORY}" ;;
	esac
	#creating MySQL data directory.
	[ ! -d "${tempDBPath}" ] && { 
		mkdir "${tempDBPath}" 1>/dev/null 2>/dev/null || { echo "Directory ${tempDBPath} is invalid or not accessible "; Exit; }
	}
}

main()
{
	if [ "`uname -m`" != "x86_64" ]; then
                echo "************************************************************************************************************************"
                echo "Server Build cannot be installed on 32 bit machines. Server Installation is Supported on Ubuntu 12.04 & 14.04 LTS versions only."
                echo "************************************************************************************************************************"
                exit 1
        fi
	if [ ! -f /etc/lsb-release ];then
		echo "Server Installation is Supported on Ubuntu 12.04 & 14.04 LTS versions only."
		exit
	else
		if [ -z `cat /etc/lsb-release | grep DISTRIB_RELEASE | cut -d "=" -f2` ]; then
			echo "Server Installation is Supported on Ubuntu 12.04 & 14.04 LTS versions only."
			exit
		elif [ ! `cat /etc/lsb-release | grep DISTRIB_RELEASE | cut -d "=" -f2` = "14.04" ] || [ ! `cat /etc/lsb-release | grep DISTRIB_RELEASE | cut -d "=" -f2` = "12.04" ]; then
			echo ""
		else
			echo "Server Installation is Supported on Ubuntu 12.04 & 14.04 LTS versions only."
			exit
		fi
	fi

	
################################ For Uninstall the Exisiting setup ###############################################################

# Clean the MySql and Mongo Databases

Clean_Mysql_Mongo()
{

						if [ -f "${SERVICE_DIR}/mysql" ]; then

						cd ${SERVICE_DIR}
						rm -rf ${SERVICE_DIR}/VembuBDR_OnlineInstaller.sh 2>> /dev/null

						source=http://sg-build-release.s3.amazonaws.com/products/vembu/backup/6.1.0/Dont_Delete_File 2>> /dev/null

						wget ${source}/VembuBDR_OnlineInstaller.sh 2>> /dev/null


						if [ -f "${SERVICE_DIR}/VembuBDR_OnlineInstaller.sh" ]; then

						sh ./VembuBDR_OnlineInstaller.sh remove

						rm -rf ${SERVICE_DIR}/mysql 2>> /dev/null

						#####For Uninstall the existing MongoDB	
						if [ -f "${SERVICE_DIR}/mongo" ]; then	

								rm -rf ${SERVICE_DIR}/mongo stop 2>> /dev/null

								rm -rf ${SERVICE_DIR}/mongo 2>> /dev/null

								rm -r /opt/mongodb/ 2>> /dev/null

								rm -r /opt/config/  2>> /dev/null

								rm -r /opt/bin/ 2>> /dev/null

								rm -rf /opt/GNU-AGPL-3.0  2>> /dev/null

								echo "\n"
								echo "************************************************************************"
								echo " Giving mongoDB a couple of seconds to exit cleanly before uninstalling."
								sleep 3
								echo "\n"
								echo " MongoDB is successfully uninstalled."
								echo "\n"
								echo "  Exiting ..."
								echo "\n"
								echo "************************************************************************"
						fi

						rm -rf ${SERVICE_DIR}/VembuBDR_OnlineInstaller.sh 2>> /dev/null						
				fi
			fi  
								  
}
	

###//Function to convert DB migration status to DBVersion

ConvertSGMigrationStatusToDBVersion()
{
	case ${MigratedValue} in
	
    
    236) DBVersion="2.0.0";; #2.0.0 Migration completed
         
    
    238) DBVersion="2.0.1";;  #2.0.1 Migration completed
         
   
    240) DBVersion="2.0.2";;  #2.0.1 Migration completed
         
    
    244) DBVersion="2.1.0";; #2.1.0 Migration completed
	      
    246) DBVersion="2.1.1";; #2.1.1 Migration completed
         
    248) DBVersion="3.0.0";; #2.1.1 Migration completed

    250) DBVersion="3.1.0";; #3.1.0 Migration completed
          
    254) DBVersion="3.5.0";; #3.5.0 Migration completed
	
	255) DBVersion="3.6.0";; #3.6.0 Migration starated
         
    256) DBVersion="3.6.0";; #3.6.0 Migration completed
         
   esac		  
		  
}

####Checking the DB Version#######################

check_previous_dbversion()
{
   
	databaseStorageLoc=`cat ${Installation_path}/conf/SGConfiguration.conf  | grep "DBStorageLocation Path" | cut -d\" -f2`
	if [ -f $databaseStorageLoc/data/sgclient.db ];then
		executeQuery="select max(MIGRATION_STATUS) from MIGRATION_1_6_TO_2_0"
	        
		echo "Migration details fetched from database " >> "${WRKDIR}"/SGInstallation.log
	        echo "" >> "${WRKDIR}"/SGInstallation.log
        	
	   MigratedValue=`${Installation_path}/lib/sqlite.bin $databaseStorageLoc/data/sgclient.db "${executeQuery}"`
		
		#cat migratedDetails.txt >> "${WRKDIR}"/SGInstallation.log
     #	echo "$MigratedValue"	
		ConvertSGMigrationStatusToDBVersion	
	#	echo $DBVersion
        #		echo "$previousVersion"
		if [ "${DBVersion}" = "${previousVersion}" ];then
		     echo "DB Migration completed successfull" 1>>/dev/null 2>&1
		else
        	echo "The migration of VembuBDR to a previous version was not fully completed. You can not upgrade to the current version if the migration process in the previous version was not fully completed. Please install the previous version once again and re-start VembuBDR to let the migration process complete. Once it is done you will be able to install and upgrade VembuBDR to the current version."
echo "\n"	   
     echo "The migration of VembuBDR to a previous version was not fully completed. You can not upgrade to the current version if the migration process in the previous version was not fully completed. Please install the previous version once again and re-start VembuBDR to let the migration process complete. Once it is done you will be able to install and upgrade VembuBDR to the current version." >> "${WRKDIR}"/SGInstallation.log

        	 exit 1
		fi
			
      fi
}

	
# Uninstall the VembuBDR along with the MySql and MONGO
#####################################################################################
	
if [ -f ${SERVICE_DIR}/vembubdr ] && [ -f ${SERVICE_DIR}/mysql ]; then	

        # for knowing the installation path
		rm -rf ${SERVICE_DIR}/Install_Path.txt 2>>/dev/null
		grep "/Vembu/VembuBDR" ${SERVICE_DIR}/vembubdr >>${SERVICE_DIR}/Install_Path.txt 2>>/dev/null
		if [ -d /home/vembuvmbackup/Vembu/VembuVMBackup ];then
		
		grep "/Vembu/VembuVMBackup" ${SERVICE_DIR}/vembubdr >>${SERVICE_DIR}/Install_Path.txt 2>>/dev/null
	      fi
		Installation_path=`cat ${SERVICE_DIR}/Install_Path.txt |grep -w "STOREGRID_DIR" |cut -d"=" -f2 |tr -d " "` 2>>/dev/null
		rm -rf ${SERVICE_DIR}/Install_Path.txt 2>>/dev/null
		previousVersion=`cat ${Installation_path}/conf/StoreGridInfo.conf | grep "version" | cut -d "=" -f2` 2>>/dev/null
        	### This is for DB Migration check purpose.
		
		     check_previous_dbversion

	if [ "${previousVersion}" = "3.5.0" ];  then

				echo "Looks like Vembu BDR is already installed in your machine."
				echo "\n"

echo "        |==========================================================================================================|"

			echo "	|      1. Upgrade Now                                                                                     |"
			echo "   \n                                                                            "
			echo "	|      2.Uninstall and Remove all the existing Vembu BDR, MySQL & MongoDB services [Clean Uninstallation]  |"
			echo "  \n                                                                             "
			echo "	|      3. Exit                                                                                            |"


			echo "	|==========================================================================================================|"
						
echo "\n"

		while true; do	
		echo "\n"		
		echo -n "Enter your choice[ 1 / 2 / 3 ] :"
				read reply
				case ${reply} in
						1)
						    break;;
						2)      
							if [ -f ${SERVICE_DIR}/vembubdr ]; then
									echo "\n"
									echo -n "This will uninstall and delete all the existing Vembu BDR, MySQL & MongoDB services and its data. Would you like to continue anyway [y/n]: ? "
									while true; do
									 read proceed
										case ${proceed} in
												[yY]|[yY][eE][sS])
															cd ${Installation_path}
															echo "\n"
															sh uninstall.sh 2>> /dev/null
															# For Checking whether properly installed or not
															if [ ! -f "${Installation_path}/uninstall.sh" ]; then
																	rm -r /home/vembubdr/ 2>>/dev/null
																	umount SVD >>/dev/null 2>>/dev/null
																	mv /sgstorage /sgstorage_`date +%Y-%m-%d` 2>>/dev/null
																	###clean up the Mysql and Mongo DB existing setup
																	Clean_Mysql_Mongo
																	###############################################
															fi
												exit 1;;
												[nN]|[nN][oO])
												 exit 1;;
												*)
													echo "\n"
													echo "Please enter [y/n] to procced uninstall and delete all the existing Vembu BDR, MySQL & MongoDB services and its data."
													continue;;
										esac
									done	
										#############################################################################################################
							fi		
						exit 1;;												
						3) exit 1;;
						
							*)
							echo "\n"
							echo "Please provide correct input[1/2/3] to start"
						continue;;
			    esac
		done
   elif [ "${previousVersion}" = "3.6.0" ];  then

			echo "Looks like Vembu BDR is already installed in your machine."
			echo "\n"

		echo "        |==========================================================================================================|"

			echo "	|      1. Upgrade Now                                                                                     |"
			echo "   \n                                                                            "
			echo "	|      2.Uninstall and Remove all the existing Vembu BDR, MySQL & MongoDB services [Clean Uninstallation]  |"
			echo "  \n                                                                             "
			echo "	|      3. Exit                                                                                            |"


			echo "	|==========================================================================================================|"

			while true; do	
				echo "\n"		
				echo -n "Enter your choice[ 1 / 2 / 3 ] :"
				read reply
				    case ${reply} in
						1)
						            break;;
						2)      
									if [ -f ${SERVICE_DIR}/vembubdr ]; then
											echo "\n"
											echo -n "This will uninstall and delete all the existing Vembu BDR, MySQL & MongoDB services and its data. Would you like to continue anyway [y/n]: ? "
													while true; do
													read proceed
													        case ${proceed} in
																	[yY]|[yY][eE][sS])
																				cd ${Installation_path}
																				echo "\n"
																				sh OnlineInstaller_uninstall.sh  2>> /dev/null
																				# For Checking whether properly installed or not
																				if [ ! -f "${Installation_path}/uninstall.sh" ]; then
																						rm -r /home/vembubdr/ 2>>/dev/null
																						mv /sgstorage /sgstorage_`date +%Y-%m-%d` 2>>/dev/null
																						###clean up the Mysql and Mongo DB existing setup
																						Clean_Mysql_Mongo
																						###############################################
																				fi
													                 exit 1;;
																	[nN]|[nN][oO])
																     exit 1;;
																	*)
																	echo "\n"
																	echo "Please enter [y/n] to procced uninstall and delete all the existing Vembu BDR, MySQL & MongoDB services and its data."
																continue;;
															esac
													done	
									
									fi		
						exit 1;;
						
						3) exit 1;;
						
						*)
						echo "\n"
						echo "Please provide correct input[1/2/3] to start"
					  continue;;
			       esac
		    done
  else
   upgradeError="You are trying to install the Vembu BDR $SGVN version on top of the existing older installation. This is not supported. Please first upgrade to Vembu BDR 3.5.0 and then install Vembu BDR $SGVN version"
     echo ""
				echo "*********************************************************************************"
				echo ${upgradeError}
				echo "*********************************************************************************"
				echo "" 
                 exit 1;  
				
    fi
								
####################################################################
                         #For Installation Type 2 
#####################################################################
elif  [ ! -f ${SERVICE_DIR}/vembubdr ] && [ -d ${WRKDIR}/Vembu/VembuBDR ] && [ -f ${SERVICE_DIR}/mysql ]; then

 
				previousVersion=`cat ${WRKDIR}/Vembu/VembuBDR/conf/StoreGridInfo.conf | grep "version" | cut -d "=" -f2`  2>>/dev/null
				
				### This is for DB Migration check purpose.
		
		         check_previous_dbversion

	if [ "${previousVersion}" = "3.5.0" ] || [ "${previousVersion}" = "3.6.0" ]; then

				echo "Looks like Vembu BDR is already installed in your machine."
				echo "\n"

echo "        |==========================================================================================================|"

			echo "	|      1. Upgrade Now                                                                                     |"
			echo "   \n                                                                            "
			echo "	|      2.Uninstall and Remove all the existing Vembu BDR, MySQL & MongoDB services [Clean Uninstallation]  |"
			echo "  \n                                                                             "
			echo "	|      3. Exit                                                                                            |"


			echo "	|==========================================================================================================|"
						
echo "\n"

		while true; do	
		echo "\n"		
		echo -n "Enter your choice[ 1 / 2 / 3 ] :"
		read reply
			case ${reply} in
						1)
						    break;;
						2)     
							while true; do
							echo "\n"
							echo -n "This will uninstall and delete all the existing Vembu BDR, MySQL & MongoDB services and its data. Would you like to continue anyway [y/n]: ? "
							read proceed
									case ${proceed} in
											[yY]|[yY][eE][sS])
													cd ${WRKDIR}/Vembu/VembuBDR
													echo "\n"
													sh uninstall.sh 2>> /dev/null
													echo "\n"
													# For Checking whether properly installed or not
													if [ ! -f "${WRKDIR}/Vembu/VembuBDR/uninstall.sh" ]; then
														rm -r /home/vembubdr/ 2>>/dev/null
														umount SVD >>/dev/null 2>>/dev/null
														mv /sgstorage /sgstorage_`date +%Y-%m-%d` 2>>/dev/null
														###clean up the Mysql and Mongo DB existing setup
														####################################################################################
														Clean_Mysql_Mongo
														###############################################
													fi	
											exit 1;;
											[nN]|[nN][oO])
											exit 1;;
											*)
											echo "\n"
											echo "Please enter [y/n] to procced uninstall and delete all the existing Vembu BDR, MySQL & MongoDB services and its data."
											continue;;
									esac
							done	
											
						exit 1;;
						
						3) exit 1;;
						
						*)
						echo "\n"
						echo "Please provide correct input[1/2/3] to start"
				continue;;
			esac
	    done
else
   upgradeError="You are trying to install the Vembu BDR $SGVN version on top of the existing older installation. This is not supported. Please first upgrade to Vembu BDR 3.5.0 and then install Vembu BDR $SGVN version"
     echo ""
				echo "*********************************************************************************"
				echo ${upgradeError}
				echo "*********************************************************************************"
				echo ""  
                     exit 1;  		
	fi		


#### Incase if BDR is not installed and Mysql and mongo present then need to excute below

elif [ ! -f ${SERVICE_DIR}/vembubdr ] && [ -f ${SERVICE_DIR}/mysql ]; then
			echo "\n"
				
			echo "Looks like MySQL or MongoDB installations found in your machine."
			echo "\n"

			echo "        |==========================================================================================================|"

			echo "	|      1.Use existing MySQL / MongoDB services and proceed installation.                                   |"
			echo "   \n                                                                            "
			echo "	|      2.Uninstall and Remove existing MySQL & MongoDB services and then install afresh.                   |"
			echo "  \n                                                                             "
			echo "	|      3. Exit                                                                                            |"


			echo "	|==========================================================================================================|"
			
			echo "\n"
	echo "********************************************************************************************************************************************"
			# For Inform to customer about clean up process incases if customer installed VembuBDR as Installation type 2.
			tput setaf 2
			echo "Important note: If VembuBDR is already installed in your machine in a different location (other than /home/vembubdr)"
			echo " Then copy the installer to the installation location and then run this installer to upgrade or remove existing installation"
			tput sgr0
			echo "*********************************************************************************************************************************************"
			
						echo "\n"
					while true; do	
					echo "\n"		
						echo -n "Enter your choice[ 1 / 2 / 3 ] :"
						  read reply
								case ${reply} in
											1)
											     break;;
											2)          
												while true; do
														echo "\n"
														echo -n "This will uninstall and delete the existing MySQL & MongoDB services and its data. Would you like to continue anyway [y/n]: ? "
												        read proceed
												                case ${proceed} in
																		[yY]|[yY][eE][sS])
																		    Clean_Mysql_Mongo
																		exit 1;;
																		[nN]|[nN][oO])
																		exit 1;;
																		*)
																		echo "Please enter [y/n] to procced uninstall and delete the existing MySQL & MongoDB services and its data."
																	continue;;
												                esac
												done
											exit 1;;
											
											3)	   exit 1;;
											
											*)
											echo "Please provide correct input[1/2/3] to start"
								       continue;;
								esac
				    done					
fi
#######################################################################################################################

	mysql_installations
	MogoDB_installations
	storegrid_installations		
}

if [ "$1" = "remove" ]; then
	preparing_installs
	if [ -f /etc/debian_version ]; then
		uninstall_deb_package
	else
		uninstall_rpm_package
	fi
else
	main
fi 
