#!/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
LIBSTD_VERSION_SIX=6
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"
PostgreSQL_DOWNLOADS="postgresql-downloads"
LOGFILE="$MYSQL_SCRIPT_LOCATION/SQLInstall.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
DB_PATH_FOR_README=""
##### Changes for Silent Installation #####
IS_SILENT_INSTALLATION=0
noofargs=$#

if [ ${noofargs} -gt 5 ] && [ ${noofargs} -lt 15 ]; then
	echo "For Silent Installation no. of arguements must be 15.."
	exit 1
fi

IS_STRING()
{
	var=$1
	if [ $var -eq $var ] 2>/dev/null; then
		return 0
	else
		return 1
	fi
}

if [ ${noofargs} -eq 15 ]; then
	IS_SILENT_INSTALLATION=1
	LICENSE_AGREEMENT=$1
    SOREGRID_INSTALLATION_TYPE=$2
    STOREGRID_USER_PASSWORD=$3
    INSTALLATION_PATH=$4
    STOREGRID_INSTALLATION_MODE=$5
    WEBCONSOLE_USER_NAME=$6
    WEBCONSOLE_PASSWORD=$7
    WEBCONSOLE_RETYPE_PASSWORD=$8
    CLIENT_IDENTITY=${9}
    STOREGRID_STORAGE_LOCATION=${10}
    BACKUP_SERVER_PORT=${11}
    GUI_PORT=${12}
    STOREGRID_GROUP_NAME=${13}
    useODBC=${14}
	ODBC_PASSWORD=${15}
	IS_STRING $SOREGRID_INSTALLATION_TYPE
	res1=$?;
    IS_STRING $STOREGRID_INSTALLATION_MODE
	res2=$?;
	IS_STRING $BACKUP_SERVER_PORT
	res3=$?;
	IS_STRING $GUI_PORT
	res4=$?;
	IS_STRING $useODBC
	res5=$?;
	################## Validation ###############
	if [ -z $LICENSE_AGREEMENT ]; then
		echo "Licence agreement not accepted"
		exit 1
	elif [ $LICENSE_AGREEMENT != 'y' ] && [ $LICENSE_AGREEMENT != 'Y' ]; then
		echo "Licence agreement not accepted"
		exit 1
	elif [ $res1 -eq 1 ]; then
		echo "Installation type value should be 1 or 2"
		exit 1
	elif [ $SOREGRID_INSTALLATION_TYPE -ne 1 ] && [ $SOREGRID_INSTALLATION_TYPE -ne 2 ]; then
		echo "Installation type value should be 1 or 2"
		exit 1
	elif [ -z $STOREGRID_USER_PASSWORD ]; then
		echo "vembubdr user password must not be empty"
		exit 1
	elif [ -z $INSTALLATION_PATH ]; then
		echo "Installation Path must not be empty"
		exit 1
	elif [ $res2 -eq 1 ]; then
		echo "Installation mode value should be 1 or 2"
		exit 1
	elif [ $STOREGRID_INSTALLATION_MODE -ne 1 ] && [ $STOREGRID_INSTALLATION_MODE -ne 2 ]; then
		echo "Installation mode value should be 1 or 2"
		exit 1
	elif [ -z $WEBCONSOLE_USER_NAME ]; then
		echo "Web Console User Name must not be empty"
		exit 1
	elif [ -z $WEBCONSOLE_PASSWORD ]; then
		echo "Web Console Password must not be empty"
		exit 1
	elif [ -z $WEBCONSOLE_RETYPE_PASSWORD ]; then
		echo "Web Console Retype Password must not be empty"
		exit 1
	elif [ $WEBCONSOLE_PASSWORD != $WEBCONSOLE_RETYPE_PASSWORD ]; then
		echo "Web Console Passwords do not match "
		exit 1
	elif [ -z $STOREGRID_STORAGE_LOCATION ]; then
		echo "VembuBDR Storage Location must not be empty"
		exit 1
	elif [ $res3 -eq 1 ]; then
		echo "BACKUP_SERVER_PORT must be a number greater than 0"
		exit 1
	elif [ $BACKUP_SERVER_PORT -lt 1 ]; then
		echo "BACKUP_SERVER_PORT must be a number greater than 0"
		exit 1
	elif [ $res4 -eq 1 ]; then
		echo "BACKUP_SERVER_PORT must be a number greater than 0"
		exit 1
	elif [ $GUI_PORT -lt 1 ]; then
		echo "BACKUP_SERVER_PORT must be a number greater than 0"
		exit 1
	elif [ -z $STOREGRID_GROUP_NAME ]; then
		echo "VembuBDR Group Name must not be empty"
		exit 1
	elif [ $res5 -eq 1 ]; then
		echo "Use ODBC value should be 0, 1 or 2"
		exit 1
	elif [ $useODBC -ne 0 ] && [ $useODBC -ne 1 ] && [ $useODBC -ne 2 ]; then
		echo "Use ODBC value should be 0, 1 or 2"
		exit 1
	elif [ -z $ODBC_PASSWORD ]; then
		echo "ODBC Password must not be empty"
		exit 1
	fi
	if ! [ -d $INSTALLATION_PATH ]; then
		mkdir -p $INSTALLATION_PATH
	fi
	if ! [ -d $STOREGRID_STORAGE_LOCATION ]; then
		mkdir -p $STOREGRID_STORAGE_LOCATION
	fi
	if getent passwd vembubdr > /dev/null 2>&1; then
		userdel vembubdr
	fi
fi

STOREGRID_DOWNLOADS="vembubdr-downloads"
export storegrid
SGVN="3.9.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="https://sg-build-release.s3.amazonaws.com/products/vembu/vembuBDR/pro/3902018011600/Linux/OnlineInstaller/Server/${BUILD_INFO_FILE}"
#HEADER_URL="http://192.168.100.125/builds/VembuProducts/BDRSuite/3-9-0/10-03-2018-16-26/BDR/${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
        if [ ${USE_DATABASE} -eq 2 ];then
		 cd ${PostgreSQL_DOWNLOADS} >> /dev/null 2>> /dev/null 
		else
		mkdir ${MYSQL_DOWNLOADS} 2>> /dev/null 
		cd ${MYSQL_DOWNLOADS} >> /dev/null 2>> /dev/null 
		fi
		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
########cecking the GLIBCXX_3.4.19
            
	   ########checking the strings
	   
	   osarch=`uname -m`
	   
if [ "${osarch}" = "x86_64" ] && [ -f /etc/debian_version ]; then

	PKG_OK=$(dpkg-query -W --showformat='${Status}\n' binutils |grep "not-installed")  >> "${WRKDIR}"/SGInstallation.log
	echo Checking for somelib: $PKG_OK 1> /dev/null 2> /dev/null  >> "${WRKDIR}"/SGInstallation.log
	
	if [ "${PKG_OK}" != "" ];then
		apt-get install binutils 1> /dev/null 2> /dev/null
		echo "Dependency package:binutils  installed successfully" >> "${WRKDIR}"/SGInstallation.log
	else
		echo "Dependency package:binutils already installed" 1> /dev/null 2> /dev/null  >> "${WRKDIR}"/SGInstallation.log
	fi


         if [ -f /usr/lib/x86_64-linux-gnu/libstdc++.so.${LIBSTD_VERSION_SIX} ];then
Check_GLIBCXX=`strings  /usr/lib/x86_64-linux-gnu/libstdc++.so.${LIBSTD_VERSION_SIX} | grep GLIBCXX_3.4.19`
		 elif [ -f /lib64/libstdc++.so.${LIBSTD_VERSION_SIX} ];then
                Check_GLIBCXX=`strings   /lib64/libstdc++.so.${LIBSTD_VERSION_SIX} | grep GLIBCXX_3.4.19`

         elif [ -f /usr/lib64/libstdc++.so.${LIBSTD_VERSION_SIX} ];then
                Check_GLIBCXX=`strings  /usr/lib64/libstdc++.so.${LIBSTD_VERSION_SIX} | grep GLIBCXX_3.4.19`

         elif [ -f /lib/libstdc++.so.${LIBSTD_VERSION_SIX} ];then
                Check_GLIBCXX=`strings  /lib/libstdc++.so.${LIBSTD_VERSION_SIX} | grep GLIBCXX_3.4.19`

         elif [ -f /usr/lib/libstdc++.so.${LIBSTD_VERSION_SIX} ];then
                Check_GLIBCXX=`strings  /usr/lib/libstdc++.so.${LIBSTD_VERSION_SIX} | grep GLIBCXX_3.4.19`
         fi


    if [ "${Check_GLIBCXX}" != "GLIBCXX_3.4.19" ];then
	
  locate libstdc++.so.6 > /home/libversion.txt 2>/dev/null
        chmod 755 /home/libversion.txt
       while read line;
			do
				strings $line  | grep GLIBCXX > /home/versiongc.txt
				chmod 755 /home/versiongc.txt
				check=""
				check=`cat /home/versiongc.txt | grep "GLIBCXX_3.4.19"`
				if [ "${check}" = "GLIBCXX_3.4.19" ];then
				   echo ""
				   break
				fi
			done <  /home/libversion.txt
      rm -rf /home/versiongc.txt
	  rm -rf /home/libversion.txt
   	fi		 
          
        	if [ "${check}" = "GLIBCXX_3.4.19" ] || [ "${Check_GLIBCXX}" = "GLIBCXX_3.4.19" ]; then
                	echo ""
					 rm -rf /home/libversion.txt >> "${WRKDIR}"/SGInstallation.log
					 echo "GLIBCXX_3.4.19 package installed successfully"  >> "${WRKDIR}"/SGInstallation.log
					 
        	elif [ `cat /etc/lsb-release | grep DISTRIB_RELEASE | cut -d "=" -f2` = "12.04" ]; then
			        echo "Seems GLIBCXX_3.4.19 package is not installed in that system. Please refer this URL "
					echo "https://www.vembu.com/support/knowledge-base/question/115015/glibcxx_3419-package-is-not-installed-in-ubuntu-1204-lts/";

					echo "Seems GLIBCXX_3.4.19 package is not installed in that system. Please refer this URL " >> "${WRKDIR}"/SGInstallation.log
					echo "https://www.vembu.com/support/knowledge-base/question/115015/glibcxx_3419-package-is-not-installed-in-ubuntu-1204-lts/" >> "${WRKDIR}"/SGInstallation.log

                	exit;
		fi
 fi	
}
checklic()
{

clear
	if [ "$1" = "" ]; then
        echo
	echo
        tput setaf 3
	echo "	================================================================================"
        echo "	|									       |"
	echo "	|  Welcome to Vembu BDR Suite installation setup wizard.	               |"	
	echo "	|									       |"
	echo "	|  We will now take you through the Vembu BDR installation process.            |"
	echo "	|  Vembu BDR is proprietary software of Vembu Technologies and is licensed under|"
	echo "	|  its own terms which you are required to accept for this installation.       |"				
	echo "	|                                                                              |"	
	echo "	|  If you would like to install Vembu BDR,Please enter yes to proceed.         |"
	echo "	|  	                                                                       |"
	echo "	================================================================================"
        tput sgr0		
	while true;do
		if [ ${IS_SILENT_INSTALLATION} -eq 0 ] 
		then
			echo -n "  	Do you want to proceed [yes/no]: "
			read proceed
		else
			proceed="Y"
		fi

		case ${proceed} in
		[yY]|[yY][eE][sS])
						break;;
			[nN]|[nN][oO])
						Exit;;
						*)
						echo " Please enter [y/n] to procced installation."
						continue;;
		esac
	done
	if [ ${IS_SILENT_INSTALLATION} -eq 0 ]; then
	echo

more << EOF
						VEMBU END USER LICENSE AGREEMENT
								     

CAREFULLY READ THE FOLLOWING END USER LICENSE AGREEMENT ('EULA')!  IT CONTAINS VERY IMPORTANT INFORMATION ABOUT YOUR (END-USER) RIGHTS, RESTRICTIONS AND OBLIGATIONS, AS WELL AS LIMITATIONS AND EXCLUSIONS THAT MAY APPLY TO YOU REGARDING YOUR RIGHTS TO DOWNLOAD, INSTALL, USE OR COPY VEMBU SOFTWARE (AS DEFINED BELOW).  BY DOWNLOADING, INSTALLING AND/OR CREATING AN ACCOUNT AT THE VEMBU PORTAL (AS DEFINED BELOW), YOU AGREE TO THE TERMS OF THIS AGREEMENT THAT WILL GOVERN YOUR PURCHASE OF LICENSES FOR PURPOSES OF INSTALLING, USING, OR COPYING VEMBU SOFTWARE.

BY CLICKING ON THE 'ACCEPT' BUTTON AT THE END OF THIS AGREEMENT, YOU ARE CONSENTING TO BE BOUND BY THE TERMS OF THIS AGREEMENT AND ARE BECOMING A PARTY TO THIS AGREEMENT.  IF YOU DO NOT AGREE TO ALL OF THE TERMS OF THIS AGREEMENT, CLICK THE 'DECLINE' BUTTON AT THE END OF THIS AGREEMENT OR LEAVE THE WEBSITE.


1.DEFINITIONS

1.1 'Confidential Information' means all information, material or data concerning the business, accounts, finance, technology, personnel, customers, suppliers, trade secrets, know-how or intellectual property rights of Vembu and all information which may reasonably be regarded as confidential information which are supplied or disclosed by Vembu to End-User or obtained by End-User in pursuance of or in connection with this Agreement or which is generated pursuant to or in connection with this Agreement.  Without limiting the foregoing, Confidential Information shall include Vembu�s software, source code, procedures, business processes, business activities and operations, related documentation, customer lists and the terms of this Agreement.  The list of Licensees and their contact information shall be deemed to be the Confidential Information of Vembu.  Provided that Confidential Information shall not include any information, material or data that:
(a) was, or becomes, generally known to the public without the End-User's breach of any obligation owed to Vembu;
(b) was, or subsequently is, independently developed by End-User without reference to Confidential Information of Vembu as can be proved by documentary evidence;
(c) was, or subsequently is, received from a third party who obtained and disclosed such Confidential Information without breach of any obligation owed to Vembu; or
(d) is required by law to be disclosed (in which case the End-User shall give Vembu reasonable prior notice of such compelled disclosure and reasonable assistance, at End-User's expense, should End-User wish to contest the disclosure or seek a protective order).

1.2 'End-User' means any corporation or other person that has obtained a license from the Owner of Vembu Software and has been authorized to install and/or use Vembu Software for its personal or internal business purposes, pursuant to this Agreement.
1.3 'Electronic Delivery' means the delivery using the Internet using servers and public or private access networks.
1.4 'Intellectual Property Rights' means any and all rights existing from time to time anywhere in the world under copyright law, patent law, trade secret law, trademark law, unfair competition law or other similar rights.
1.5 'Marks' means any trademarks, service marks or trade names associated with the Vembu and/or Vembu Software including the trade marks �Vembu�.
1.6 'Owner of Vembu Software' means M/s. Vembu Technologies Private Limited ('Vembu'), a company incorporated in India.
1.7 'Proprietary Rights' mean all rights, title, interest and intellectual property rights including but not limited to copyright, patent, design rights, trademarks etc., present or future, in perpetuity through-out the world and shall include Confidential Information.
1.8 'Vembu Software' mean the following computer software programs which are part of the Vembu BDR Suite: (a) Vembu VMBackup; (b) Vembu ImageBackup (c) Vembu NetworkBackup; (d) Vembu OffsiteDR; (e) Vembu Universal Explorer; and (f) Vembu RecoveryCD; together with accompanying items including, but not limited to, the executable programs, drivers, libraries and data files associated with such programs and all related documentation, which are owned by the Owner of Vembu Software.  Vembu Software comprises of a server component ('Vembu Server Software') which is to be installed on a backup server and a client component ('Vembu Client Agent Software').
1.9 'Source Code' means a series of human readable instructions or statements in high-level or low-level computer language and which is normally transformed by an interpreter or compiler into machine-readable executable code for actual use on a computer. 
1.10 'Vembu' means Vembu Technologies Private Limited, a company incorporated in India and having it's registered office at Office A, II floor, New No. 209, Old No. 13A, Velachery Main Road, Velachery, Chennai - 600042, India and its subsidiaries.
1.11 'Vembu Portal' means the website www.vembu.com that offers a broad array of resources and services including, but not limited to, a web-based interface for users to create account information.

2.SCOPE OF AGREEMENT

2.1 This Agreement sets forth the terms upon which End-User may (i) license the evaluation version of Vembu Software in order to evaluate it by installing and using the evaluation version; and (ii) obtain the right to install, use or copy Vembu Software.
2.2 The Vembu Software is not sold to End-User, and instead is only granted a license to use Vembu Software in accordance with the terms of this Agreement.  Vembu reserves all rights (including all title and intellectual property rights) not specifically granted to you by this Agreement.  Vembu Software is protected by copyright laws and international copyright treatises, as well as other intellectual property laws and treatises.

3. GRANT OF RIGHTS AND RESTRICTIONS

3.1 Vembu grants to the End-User a non-exclusive, non-transferable and limited right to install and/or use the evaluation version of the Vembu Software in binary object code form, for purposes of trial and evaluation of the performance of the Vembu Software.  This license shall be valid only for a period of thirty (30) days from the installation of the evaluation version of the Vembu Software, ('Evaluation Period').  Should the End-User decide to use the evaluation version beyond the Evaluation Period or if End-User wants to use the Vembu Software to backup its data, the End-User shall purchase licenses for the same in accordance with the terms of this Agreement.  Should the End-User decide not to purchase licenses for use of the Software Products and Services (any version) beyond the Evaluation Period, the End-User shall immediately cease using the Vembu Software and/or its functionality and delete all copies of the Vembu Software including the evaluation version installed in its computer(s) forthwith on the expiry of the Evaluation Period.  Subsequent downloads of the evaluation version of the Vembu Software Products by or for the End-User does not extend, renew or otherwise restart the term of the license for the evaluation version granted herein.
3.2 Should the End-User decide to install the Vembu Software, subject to the terms of this Agreement, Vembu hereby grants the End-User a limited, non-exclusive, non-transferable license to download, install and use the Vembu Software, as the case may be, for the sole purpose of using the data back-up applications and any other applications expressly provided by Vembu from time to time solely for the End-User�s internal business purposes.
3.3 Following restrictions apply:
(a) End-User shall install and use only one copy of the Vembu Software on one server or computer system, as the case may be.
(b) End-User shall not use the Vembu Software beyond the period for which it has obtained a license. End-User shall install or use the Vembu Software only on such number of machines for which it has obtained licenses.
(c) End-User shall not or permit any other to transmit all or any portion of the Vembu Software through any network or communication line.
(d) The rights under this Agreement are personal to the End-User and the functionality of the Vembu Software may be used only by its employees, agents or others specifically authorized by the End-User and the End-User shall not sub-license or transfer in any manner whatsoever any of its rights under this Agreement.
(e) End-User shall not modify any portion of the Vembu Software or merge or integrate any portion of the Vembu Software into/with any other program.
(f) End-User shall not translate, decompile, disassemble or reverse engineer the Vembu Software or do anything to obtain underlying information that is not visible to the user in connection with normal use of the Vembu Software.
(g) End-User shall not display the Vembu Software's object code on any computer screen or make any hardcopy memory dump of the Vembu Software's object code.
(h) End-User shall not remove, alter or conceal any copyright or other intellectual property notices from any copy of the Vembu Software or any of written materials, if any, accompanying it.
(i) End-User acknowledges that it has the license to the Vembu Software as a single product and therefore, shall not separate the Vembu Software into software component parts for use on more than one computer.

3.4 End-User acknowledges that it is merely granted a license to install and/or use the Vembu Software in accordance with the terms of this Agreement.  Owner of Vembu Software reserves all rights (including all title and Intellectual Property Rights) not specifically granted to the End-User under this Agreement. 
3.5 Vembu shall make provision for the Electronic Delivery of Vembu Software to the End-User.
3.6 End-User shall take reasonable security precautions, protocols, and protective measures to prevent the unauthorized use, copying, distribution, transmission, or dissemination of the Vembu Software.

4.PRICING

4.1 End-User hereby agrees to pay Vembu applicable license fee, data storage fee and technical support fee, as described in Vembu�s website www.vembu.com, for Vembu Software.  Vembu reserves the right to revise such fees from time to time and such revised fees will be applicable to any new licenses applied by End-User after the date on which such revised fees are published on Vembu�s website.
4.2 All fees described herein are exclusive of applicable taxes and such taxes will be borne by the End-User.
4.3 All payments made pursuant to this Agreement shall be in U.S. dollars.  Payments shall be made by credit card, check or wire transfer at the time of purchase of licenses for use.

5.INTELLECTUAL PROPERTY RIGHTS

5.1 End-User acknowledges and agrees that Vembu is the exclusive owner of the Software Products and Services and retains and owns Intellectual Property Rights in any derivatives, suggestions, ideas, enhancement requests, feedback, recommendations or other information provided by the End-User relating to Vembu Software. End-User acquires neither any Intellectual Property Rights nor other rights in Vembu Software, other than the right to install and use the Vembu Software as expressly set forth in this Agreement.
5.2 End-User shall not obtain or attempt to obtain any right, title or interest in Vembu Software, any versions or updates of the same, or in any value added services in combination with Vembu Software.

6.WARRANTIES AND DISCLAIMER

(a) VEMBU WARRANTS THAT IT HOLDS ALL RIGHT, TITLE AND INTEREST IN AND TO THE VEMBU SOFTWARE OR THAT IT IS OTHERWISE ENTITLED TO PROVIDE INCLUDING THE RIGHT TO LICENSE THE VEMBU SOFTWARE TO END-USER ON THE TERMS CONTAINED HEREIN.

(b) VEMBU WARRANTS THAT THE VEMBU SOFTWARE, WHEN IT IS DELIVERED TO THE END-USER, IS FREE OF VIRUSES, TROJAN HORSES, WORMS, AND OTHER SIMILAR DESTRUCTIVE OR DISABLING CODE.

(c) THE ABOVE IS A LIMITED WARRANTY AND IS THE ONLY WARRANTY MADE BY VEMBU.  THE VEMBU SOFTWARE IS PROVIDED ON AN 'AS IS' BASIS AND THE END-USER�S USE OF THE SOFTWARE IS AT END-USER�S SOLE RISK.

(d) VEMBU EXPRESSLY DISCLAIMS AND END-USER HEREBY EXPRESSLY WAIVES ALL OTHER CONDITIONS, WARRANTIES AND REPRESENTATIONS, EXPRESS OR IMPLIED, INCLUDING WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, OR NONINFRINGEMENT; THAT THE VEMBU SOFTWARE WILL MEET END-USER REQUIREMENTS OR THAT THE OPERATION OF THE VEMBU SOFTWARE WILL BE UNINTERRUPTED OR ERROR-FREE OR THAT ERRORS IN THE VEMBU SOFTWARE, IF ANY, WILL BE CORRECTED.  ALSO, THERE IS NO WARRANTY OR CONDITION OF TITLE, QUIET ENJOYMENT, OR QUIET POSSESSION WITH REGARD TO VEMBU SOFTWARE.

(e) VEMBU'S ABOVE LIMITED WARRANTY IS IN LIEU OF ALL LIABILITIES OR OBLIGATIONS OF VEMBU FOR DAMAGES ARISING OUT OF OR IN CONNECTION WITH THE INSTALLATION, USE OR PERFORMANCE OF THE VEMBU SOFTWARE. THE VEMBU SOFTWARE'S FAILURE TO PERFORM IN ACCORDANCE WITH THE SPECIFICATIONS SHALL NOT BE CONSIDERED A FAILURE OF THE ESSENTIAL PURPOSE OF THE WARRANTIES CONTAINED HEREIN.

(f) IN NO EVENT SHALL VEMBU BE LIABLE FOR ANY LOST PROFITS, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE, INDIRECT, OR OTHER CONSEQUENTIAL OR INCIDENTAL DAMAGES EVEN IF VEMBU HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES AND REGARDLESS OF WHETHER SUCH LIABILITY IS BY REASON OF BREACH OF WARRANTY OR BASED ON CONTRACT, TORT, NEGLIGENCE, STRICT LIABILITY, PRODUCTS LIABILITY OR OTHERWISE.

7.LIMITATION ON LIABILITY

(a) NOTWITHSTANDING ANYTHING CONTAINED IN THIS AGREEMENT, UNDER NO CIRCUMSTANCES SHALL VEMBU BE LIABLE TO END-USER FOR ANY SPECIAL, INDIRECT, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL DAMAGES (INCLUDING BUT NOT LIMITED TO DAMAGES FOR LOSS OF BUSINESS PROFITS OR SAVINGS, BUSINESS INTERRUPTION, LOSS OF INFORMATION OR DATA, OR ANY OTHER PECUNIARY LOSS) WHETHER IN CONTRACT, NEGLIGENCE, TORT, STRICT LIABILITY OR OTHERWISE ARISING OUT OF YOUR USE OR INABILITY TO USE THE VEMBU SOFTWARE OR THE BREACH OF THIS AGREEMENT, EVEN IF FORESEEABLE OR IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES; OR FOR ANY CLAIM THAT THE VEMBU SOFTWARE INFRINGE UPON A COPYRIGHT OR VIOLATE THE TRADE SECRET RIGHTS OF ANY THIRD PARTY ARISING OR RESULTING FROM ANY BREACH OF THIS AGREEMENT BY THE END-USER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES..

(b) NOTWITHSTANDING ANYTHING CONTAINED IN THIS AGREEMENT, VEMBU'S AGGREGATE LIABILITY OR DAMAGES TO END-USER OR TO ANY OTHER PERSON SHALL NOT EXCEED THE AMOUNT PAID BY END-USER TO VEMBU TO USE THE VEMBU SOFTWARE UNDER THIS AGREEMENT REGARDLESS OF THE FORM OF THE CLAIMS.

8.INDEMNITY

End-User hereby agrees to indemnify, hold harmless and defend Vembu from and against any and all claims or lawsuits, including attorney's fees and costs that arise, result from or are connected with the said use or distribution of the Vembu Software in violation of this Agreement.

9.TERM AND TERMINATION

9.1 The term of the Agreement ('Term') will commence when End-User agrees to the terms and conditions of this Agreement by clicking the 'Accept' button below and completes the registration process for the Software Products and Services.  This Agreement will remain in effect until terminated by the End-User or Vembu. 

9.2 In the event End-User has failed to pay any amount for the Vembu Software when due or is in breach or default of any provision of this Agreement, Vembu may forthwith terminate this Agreement without notice.  Upon termination of the Agreement, all of End-User�s rights under this Agreement shall immediately terminate and End-User shall be required to delete all copies of the Vembu Software from its computer(s).  Vembu reserves the right to disable any Vembu Software installed on End-User�s computer systems on termination of the Agreement.  End-User will remain liable for payments and any other obligations incurred till the date of termination. 

9.3 End-User acknowledges and agrees that Vembu reserves the right to terminate this Agreement without notice, and thereby terminate the rights granted herein to the End-User at any time for any reason.

9.4 If End-User commences or participates in any legal proceeding against Vembu, then Vembu may, in its sole discretion, suspend or terminate all license grants and any other rights provided under this Agreement.

9.5 In the event of multiple license purchases by the End-User for the Vembu Software, if there are any differences in the terms between or among the EULAs issued in connection with the purchases, the terms in the most recent EULA will be deemed adopted by the End-User to govern its use of all Vembu Software supplied to the End-User under all such EULAs.

10.RIGHT TO COLLECT INFORMATION

Vembu Software contains programs that collect data relating to the End-User�s use of the Vembu Software.  End-User agrees that Vembu may collect such data about its use of the Vembu Software and that Vembu may use such data collected through such programs to, among other things, monitor End-User�s compliance with the terms of this Agreement, and/or to improve the Vembu Software.  End-User agrees that it will not prevent or in any manner restrict the right of Vembu to collect such data on End-User�s use of the Vembu Software.

11.SUPPORT SERVICES

Notwithstanding anything contained anywhere in this Agreement, Vembu shall not be responsible to the End-User for the maintenance or for the provision of support services for the Vembu Software under this Agreement, or to provide any updates of the Vembu Software other than the support services and software updates provided to End-Users that pay for such services.

12.THIRD PARTY SOFTWARE

Portions of the Vembu Software include software with open source licenses from third parties that govern the use of those portions.  Nothing contained herein limits the rights and obligations you may have under such open source licenses.  However, the disclaimer of warranty and limitation of liability provisions in this Agreement will apply to the Vembu Software as a whole.  For details of the open source software included in the Software and the terms of the license for the same, please refer to www.vembu.com/license-agreements/open-source-list. 

13.GOVERNING LAW AND JURISDICTION

This Agreement shall be governed by and construed under by the laws of the Republic of India. End-User agrees that any dispute between the parties arising out of or in connection with this Agreement (including any question regarding its existence, validity or termination) shall be submitted exclusively to the competent courts located at Chennai, India and hereby expressly consents to the jurisdiction and venue of the courts within Chennai, India to the exclusion of any other court that may have jurisdiction in the matter.

14.GENERAL

This Agreement is binding on End-User as well as its employees, contractors, agents and any person who uses the computer/device on which the Vembu Software is installed.  This Agreement is also binding on your successors and assignees. This Agreement is the entire agreement between us and supersedes all previous agreements, arrangements, understandings or representations (including advertisement) relating to the subject matter hereof.  If any provision of this Agreement is deemed invalid or unenforceable that particular provision will be deemed modified to the extent necessary to make the provision valid and enforceable and the remaining provisions will remain in full force and effect. Any failure or delay by Vembu to exercise any right or remedy under this Agreement shall not operate as a waiver of that right or remedy.
EOF
fi
fi

agreed=""
while [ x$agreed = x ]; do

        if [ ${IS_SILENT_INSTALLATION} -eq 0 ]; then
			echo 
			echo -n " Do you agree to the above license terms? [y / n]   "
			read reply
		else
			reply="Y"
		fi

    case $reply in
       y | Y | yes | YES | Yes)
           agreed=1;;
       n | N | no | NO | No)
     
        echo " ******************************************************************" 
        echo " If you don't agree to the license you can't install this software";
        echo " ******************************************************************"
      
    exit 1;;
    esac
done 

}
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
		
		if [ "$USE_DATABASE" -eq 2 ]; then 
		echo " 1. PostGreSQL RDBMS (9.6)"
		echo " 2. PostGreSQL Connector (9.5.02)"
		echo " 3. ODBC driver (2.3.4)"
		if [ ${SIMPLE_ONLINE_INSTALLER} -eq 1 ]; then
			echo " 4. VembuBDR Server ($SGVN)"
		fi
		else
		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
		fi
		

		#if [ -f /etc/debian_version ] || [ -f /etc/mandrake-release ]; then
		#	echo ""
		#else
		if [ "$USE_DATABASE" -ne 2 ]; then 
			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	
		#fi
		echo
		echo "####################################################################################################################"
        	echo
	fi

	
	while true; do
		if [ ${IS_SILENT_INSTALLATION} -eq 0 ]; then
			echo -n "Do you want to proceed [y/n]: "
			read proceed
		else
			proceed="Y"
		fi
		
		case ${proceed} in
			[yY]|[yY][eE][sS])
					break;;
			[nN]|[nN][oO])
			
			           if [ ${USE_DATABASE} -ne 2 ];then
						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
						else
						   if [ -d ${PostgreSQL_DOWNLOADS} ]; then
							cd ${PostgreSQL_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
					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
	if [ ${IS_SILENT_INSTALLATION} -eq 0 ]; then
		echo -n "Do you want to proceed [y/n]: "
		read proceed
	else
		proceed="Y"
	fi
	
		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		 

}
Postgresql_disclaimer(){

					if [ ! -d "${PostgreSQL_DOWNLOADS}" ]; then
						mkdir -p ${PostgreSQL_DOWNLOADS}
					fi
					preparing_installs
	
                      
}
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
		if [ ${IS_SILENT_INSTALLATION} -eq 0 ]; then
				read mysqlpwd
		else
			mysqlpwd="admin"
		fi
				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."
			if [ ${IS_SILENT_INSTALLATION} -eq 0 ]; then
				echo -n " Do you want to create a new database for the server? [y/n]: "
				read proceed
			else
				proceed="N"
			fi
			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
		if [ ${IS_SILENT_INSTALLATION} -eq 0 ]; then
			echo -n " Are you sure want to drop \"$dbName\" database? [y/n] : "
			read choice
		else
			choice="Y"
		fi
		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
}

odbc_configuration()
{
	echo
	echo "****************************************************************"
	echo " Test Connectivity between \"ODBC-driver\" and \"MySQL-Server\" "
	echo "****************************************************************"
	
	  if [ ${USE_DATABASE} -eq 2 ];then
		 cd ${WRKDIR}/${PostgreSQL_DOWNLOADS}
	  else
		cd ${WRKDIR}/${MYSQL_DOWNLOADS}
	 fi
	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
				if [ ${IS_SILENT_INSTALLATION} -eq 0 ]; then
					echo -n 1>&2 " Enter MySQL password : "
					stty -echo
					read mysql_passwd
					stty echo
				else
					mysql_passwd="admin"
				fi
				
				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
				
				if [ ${IS_SILENT_INSTALLATION} -eq 0 ]; then
					echo -n "Do you want to install MongoDB Now ? Enter [y/n] : "
					read urChoice
				else
					urChoice="Y"
				fi
				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
				if [ ${IS_SILENT_INSTALLATION} -eq 0 ]; then
					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
				else
					urChoice=2
				fi
				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}
}
Installing_unixodbc_Postgresql()
{

echo "installation of unix odbc started ">> "${WRKDIR}"/SGInstallation.log
cd ${WRKDIR}/${PostgreSQL_DOWNLOADS};

if [ -f "${BUILD_INFO_FILE}" ]; then
                unixodbc_postgres=`grep -rni "unixodbc_postgres" ${BUILD_INFO_FILE} |cut -d"=" -f2`
                if [ "${unixodbc_postgres}" = "" ]; then
                        echo " ERROR: Failed to retrieve odbc header information. Please try again..."
						echo "unix odbc files not downloaded properly" >>"${WRKDIR}"/SGInstallation.log
                        Exit
                fi
                wget -c ${unixodbc_postgres} -a ${LOGFILE}
                "${UNZIP}" -o unixodbc_postgres.zip >> ${LOGFILE}
                if [ "$?" -gt 1 ]; then
                        echo
                        echo " Unable to extract odbc files. Please verify the downloaded file."
                        echo
                        Exit
                fi
        fi
tar -xvf unixODBC-2.3.4.tar.gz 1> /dev/null 2>> "${WRKDIR}"/SGInstallation.log
echo "Untaring of unix odbc completed successfully.">>"${WRKDIR}"/SGInstallation.log
cd unixODBC-2.3.4 1> /dev/null 2>> "${WRKDIR}"/SGInstallation.log
./configure 1> /dev/null 2>> "${WRKDIR}"/SGInstallation.log
echo "configure command for unixodbc executed successfully.">>"${WRKDIR}"/SGInstallation.log
make 1> /dev/null 2>> "${WRKDIR}"/SGInstallation.log
make install 1> /dev/null 2>> "${WRKDIR}"/SGInstallation.log
echo "Unix odbc configured and installed successfully.">>"${WRKDIR}"/SGInstallation.log

cd ${WRKDIR}
}

odbc_configuration_postgresql()
{
echo "odbc configurations for postgresql is in progress."
odbcportnum=$1
echo
	echo "****************************************************************"
	echo " Test Connectivity between \"ODBC-driver\" and \PostGreSQL-Server\" "
	echo "****************************************************************"

	
		cd ${WRKDIR}/${PostgreSQL_DOWNLOADS}
	if [ ! -f "unixodbc_postgres.zip" ]; then
	      if [ -f "${BUILD_INFO_FILE}" ]; then
                unixodbc_postgres=`grep -rni "unixodbc_postgres" ${BUILD_INFO_FILE} |cut -d"=" -f2`
                if [ "${unixodbc_postgres}" = "" ]; then
				echo "odbc configuration files are not downloaded properly.">>"${WRKDIR}"/SGInstallation.log
                        echo " ERROR: Failed to retrieve odbc header information. Please try again..."
                        Exit
                fi
                wget -c ${unixodbc_postgres} -a ${LOGFILE}
                "${UNZIP}" -o unixodbc_postgres.zip >> ${LOGFILE}
                if [ "$?" -gt 1 ]; then
                        echo
                        echo " Unable to extract odbc files. Please verify the downloaded file."
                        echo
                        Exit
                fi
        fi
	fi
echo "copying contents of odbcinst files started">>"${WRKDIR}"/SGInstallation.log
	# copying odbcinst.ini information
	if [ ! -s /usr/local/etc/odbcinst.ini ]; then	
		rm -f /usr/local/etc/odbcinst.ini >> /dev/null
		cp -f odbc_postgres/odbcinst.ini /usr/local/etc/odbcinst.ini 1>/dev/null 2>/dev/null
	else
		if [ -f /usr/local/etc/odbcinst.ini ] && [ ! -f /usr/local/etc/odbcinst.ini.sg ]; then

			check_odbcinst=`cat /usr/local/etc/odbcinst.ini | grep  PostgreSQL` 2>/dev/null
			if [ "$check_odbcinst" = "" ];then
			cat odbc_postgres/odbcinst.ini >> /usr/local/etc/odbcinst.ini  2>/dev/null
			fi
		else
			check_odbcinst=`cat /usr/local/etc/odbcinst.ini | grep  PostgreSQL` 2>/dev/null
			if [ "$check_odbcinst" = "" ];then
			cat odbc_postgres/odbcinst.ini >> /usr/local/etc/odbcinst.ini 2>/dev/null
			fi
		fi
	fi
echo "copying odbc.ini file contents started">>"${WRKDIR}"/SGInstallation.log
	# copying odbc.ini information

		if [ ! -s /usr/local/etc/odbc.ini ]; then	
		rm -f /usr/local/etc/odbci.ini >> /dev/null
    	cp -f odbc_postgres/odbc.ini /usr/local/etc/odbc.ini 1>/dev/null 2>/dev/null
	else
		if [ -f /usr/local/etc/odbc.ini ] && [ ! -f /usr/local/etc/odbc.ini.sg ]; then
				check_odbc=`cat /usr/local/etc/odbc.ini | grep  PostGreDBConnection` 2>/dev/null
				if [ "$check_odbc" = "" ];then
				cat odbc_postgres/odbc.ini >> /usr/local/etc/odbc.ini 2>/dev/null
				fi
		else
			check_odbc=`cat /usr/local/etc/odbc.ini | grep  PostGreDBConnection`  2>/dev/null
			if [ "$check_odbc" = "" ];then
			cat odbc_postgres/odbc.ini >> /usr/local/etc/odbc.ini  2>/dev/null
			fi
		fi
	fi
	
	if [ "$postgreExits" != "1" ];then 
		if [ "$postgreExits" != "1" ];then
		odbc_port_num=`cat /usr/local/etc/odbc.ini | grep "Port" | cut -d ":" -f2 `
		perl -w -e "s:$odbc_port_num:Port = $odbcportnum:g" -p -i /usr/local/etc/odbc.ini
		fi
	else
		if [ "$postgreExits" = "1" ];then
		port_num=`cat /etc/postgresql/9.6/main/postgresql.conf | grep -w "port =" | awk '{print $3}'`
		odbc_port_num=`cat /usr/local/etc/odbc.ini | grep "Port" | cut -d ":" -f2 `
		perl -w -e "s:$odbc_port_num:Port = $port_num:g" -p -i /usr/local/etc/odbc.ini
		echo "***********************************"
		fi
	fi
		
 echo "Odbc configurations saved successfully ">>"${WRKDIR}"/SGInstallation.log
}
Install_Psql_Driver(){
echo "Starting installation of psql_driver ">> "${WRKDIR}"/SGInstallation.log
cd ${WRKDIR}/${PostgreSQL_DOWNLOADS};
apt-get --yes --force-yes install libpq-dev 1> /dev/null 2>> "${WRKDIR}"/SGInstallation.log
echo "Dependency library libpq-dev installed successfully.">> "${WRKDIR}"/SGInstallation.log
if [ -f "${BUILD_INFO_FILE}" ]; then
                psqlodbc_driver=`grep -rni "psqlodbc_driver" ${BUILD_INFO_FILE} |cut -d"=" -f2`
                if [ "${psqlodbc_driver}" = "" ]; then
                        echo " ERROR: Failed to retrieve odbc header information. Please try again..."
                        Exit
                fi
                wget -c ${psqlodbc_driver} -a ${LOGFILE}
fi
tar -zxvf psqlodbc-09.05.0400.tar.gz 1> /dev/null 2>> "${WRKDIR}"/SGInstallation.log
echo "psql driver downloaded and extracted successfully.">> "${WRKDIR}"/SGInstallation.log
cd psqlodbc-09.05.0400
./configure  --with-libpq=/usr/lib 1> /dev/null 2>> "${WRKDIR}"/SGInstallation.log
echo "configuration of psql driver completed">> "${WRKDIR}"/SGInstallation.log
make 1> /dev/null 2>> "${WRKDIR}"/SGInstallation.log
make install 1> /dev/null 2>> "${WRKDIR}"/SGInstallation.log
cp /usr/local/lib/libodbcinst.so.2 /usr/lib 2>> "${WRKDIR}"/SGInstallation.log
cp /usr/local/lib/libodbc.so.2  /usr/lib 2>> "${WRKDIR}"/SGInstallation.log
echo "psql driver installed successfully.">> "${WRKDIR}"/SGInstallation.log

}

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
			if [ ${IS_SILENT_INSTALLATION} -eq 0 ]; then
				echo
				echo " Warning : This will uninstall mysql-connector-odbc."
				echo -n " Proceed? [y/n] : "
				read proceed
			else
				proceed="Y"
			fi
				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
			if [ ${IS_SILENT_INSTALLATION} -eq 0 ]; then
				echo -n " choice : "
				read package_count
			else
				package_count="q";
			fi
			case ${package_count} in 
				$server_count)
					while true; do
						if [ ${IS_SILENT_INSTALLATION} -eq 0 ]; then
							echo
							echo " Warning : This will uninstall MySQL-Server rpm package. "
							echo -n " Proceed? [y/n] : "
							read proceed
						else
							proceed="Y"
						fi
						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
						if [ ${IS_SILENT_INSTALLATION} -eq 0 ]; then
							echo
							echo " Warning : This will uninstall MySQL-Client rpm package. "
							echo -n " Proceed? [y/n] : "
							read proceed
						else
							proceed="Y"
						fi
						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
						if [ ${IS_SILENT_INSTALLATION} -eq 0 ]; then
							echo
							echo " Warning : This will uninstall UnixODBC rpm package."
							echo -n " Proceed? [y/n] : "
							read proceed
						else
							proceed="Y"
						fi
						
						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
			if [ ${IS_SILENT_INSTALLATION} -eq 0 ]; then
				echo -n " choice : "
				read package_count
			else
				package_count="q"
			fi
			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
						if [ ${IS_SILENT_INSTALLATION} -eq 0 ]; then
							echo
							echo " Warning : This will uninstall MySQL-Client debian package. "
							echo -n " Proceed? [y/n] : "
							read proceed
						else
							proceed="Y"
						fi
						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
						if [ ${IS_SILENT_INSTALLATION} -eq 0 ]; then
							echo
							echo " Warning : This will uninstall UnixODBC debain package."
							echo -n " Proceed? [y/n] : "
							read proceed
						else
							proceed="Y"
						fi
						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
				if [ ${IS_SILENT_INSTALLATION} -eq 0 ]; then
					echo -n " Do you want to start MySQL server now? [y/n]: "
					read proceed
				else
					proceed="Y"
				fi
				
					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
				if [ ${IS_SILENT_INSTALLATION} -eq 0 ]; then
					echo -n " Do you want to start MySQL server now? [y/n]: "
					read proceed
				else
					proceed="Y"
				fi
					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
	if ! [ "${previousVersion}" = "3.8.0" ] && ! [ "${previousVersion}" = "3.9.0" ] && ! [ -f ${SERVICE_DIR}/postgresql ]; then
		echo "Postgres Installation Failed. VembuBDR will not be installed.."
		echo "Postgres Installation Failed. VembuBDR will not be installed..">> "${WRKDIR}"/SGInstallation.log
		Exit
	fi
	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 software 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 [ ${USE_DATABASE} -eq 2 ];then
	if [ ! -f "../${PostgreSQL_DOWNLOADS}/${BUILD_INFO_FILE}" ]; then
		echo " ${BUILD_INFO_FILE} file is missing in ${PostgreSQL_DOWNLOADS} folder. Please verify the downloaded file size."
		Exit
	fi
else	
	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	
   fi		
fi
		#VembuBDR Downloads here
		echo "$1"
		
		if [ ${USE_DATABASE} -eq 2 ];then
		 storegrid_package_link=`cat ../${PostgreSQL_DOWNLOADS}/${BUILD_INFO_FILE} |grep -w "$1" |cut -d"=" -f2`
		else
		storegrid_package_link=`cat ../${MYSQL_DOWNLOADS}/${BUILD_INFO_FILE} |grep -w "$1" |cut -d"=" -f2`
		fi
		#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
			if [ ${IS_SILENT_INSTALLATION} -eq 0 ]; then 
				wget -c ${storegrid_package_link}
			else
				wget -c -q ${storegrid_package_link}
			fi
			"$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 [ ${IS_SILENT_INSTALLATION} -eq 1 ]; then 
				#${RUN} ${WRKDIR}/${STOREGRID_DOWNLOADS}/${storegrid_bin_file} "y" 1 "admin" "/home/vembubdr" 1 "admin" "admin" "admin" 1 "/home/vembubdr/" 32001 32005 "Avinash" "2" "admin"
				
				${RUN} ${WRKDIR}/${STOREGRID_DOWNLOADS}/${storegrid_bin_file} $LICENSE_AGREEMENT $SOREGRID_INSTALLATION_TYPE $STOREGRID_USER_PASSWORD $INSTALLATION_PATH $STOREGRID_INSTALLATION_MODE $WEBCONSOLE_USER_NAME $WEBCONSOLE_PASSWORD $WEBCONSOLE_RETYPE_PASSWORD $CLIENT_IDENTITY $STOREGRID_STORAGE_LOCATION $BACKUP_SERVER_PORT $GUI_PORT $STOREGRID_GROUP_NAME $useODBC $ODBC_PASSWORD $DB_PATH_FOR_README
			else
			   if [ "$odbc_database" = "" ] && [ "$odbc_hostname" = "" ] && [ "$odbc_username" = "" ] && [ "$odbc_password" = "" ]; then
					${RUN} ${WRKDIR}/${STOREGRID_DOWNLOADS}/${storegrid_bin_file}
               elif [ "$USE_DATABASE" -eq 2 ]; then 
				${RUN} ${WRKDIR}/${STOREGRID_DOWNLOADS}/${storegrid_bin_file} dsname=PostGreDBConnection hostname=$odbc_hostname databasename=$odbc_database username=$odbc_username password=$odbc_password $DB_PATH_FOR_README
               else
                ${RUN} ${WRKDIR}/${STOREGRID_DOWNLOADS}/${storegrid_bin_file} dsname=sgserver hostname=$odbc_hostname databasename=$odbc_database username=$odbc_username password=$odbc_password $DB_PATH_FOR_README
               fi

			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	
}

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_Postgresql_Database(){

if [ "$postgreExits" != "1" ];then
	#to install latest version of postgresql database
	sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" >> /etc/apt/sources.list.d/pgdg.list' 1> /dev/null 2> /dev/null
	if [ `cat /etc/lsb-release | grep DISTRIB_RELEASE | cut -d "=" -f2` = "16.04" ]; then
		sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main" >> /etc/apt/sources.list.d/pgdg.list' 1> /dev/null 2> /dev/null
	fi
	wget -q https://www.postgresql.org/media/keys/ACCC4CF8.asc -O - | sudo apt-key add - 1> /dev/null 2> /dev/null
	# apt install --reinstall libappstream3 1> /dev/null 2>> "${WRKDIR}"/SGInstallation.log
  
	#installs postgresql database
	if [ -f /etc/debian_version ]; then	
		sudo apt-get update 1> /dev/null 2> /dev/null
		sudo apt-get --yes --force-yes install postgresql-9.6 postgresql-contrib-9.6 1> /dev/null 2>> "${WRKDIR}"/SGInstallation.log
	else 
		yum install postgresql postgresql-contrib 
	fi
fi

if ! [ -f ${SERVICE_DIR}/postgresql ]; then
	echo "Postgres Installation Failed. VembuBDR will not be installed.."
	echo "Postgres Installation Failed. VembuBDR will not be installed..">> "${WRKDIR}"/SGInstallation.log
	Exit
fi

}

Postgresql_server_password_setting()
{
uname="postgres";
pswd="admin"
dbpath=$1
dbportnum=$2

	#if [ "$postgreExits" != "1" ];then
#echo "\n"	
#echo "********************************************************************************"
#echo "Default credentials for PostgreSQL:"
#echo "User : 'postgres' "
#echo "Password : 'vembu'"
#echo "********************************************************************************"
#echo "\n"	
			#echo
		#while true; do
		#	echo -n "Do you want to change the default password for the Postgre user 'postgres' [y/n] "
			#read response
		#	case ${response} in
		#	[yY]|[yY][eE][sS])
		#	     
				#	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
				#	else
					#   break;
					#fi
				#done
				# break;;
			#[nN]|[nN][oO])
			          #pswd="vembu"
                 #  break;;	
             # *)
			#	echo "Please provide correct input [y/n] to proceed."
			#	echo
				#continue;;
			#esac
		#done				   
	#fi	
			sed -i -e 's/peer/trust /g' /etc/postgresql/9.6/main/pg_hba.conf
			echo "\n"
		if [ "$postgreExits" != "1" ] && [ "${dbPath}" = "/var/lib/postgresql/9.6/main" ] && [ ${#dbportnum} -eq 32010 ];then 
			     echo "***********************************"
		else
			 if [ "$postgreExits" != "1" ];then
			data_directory=`cat /etc/postgresql/9.6/main/postgresql.conf | grep "data_directory" | cut -d ":" -f2 `
			perl -w -e "s:$data_directory:data_directory = '$dbPath':g" -p -i /etc/postgresql/9.6/main/postgresql.conf
			port_num=`cat /etc/postgresql/9.6/main/postgresql.conf | grep -w "port =" | awk '{print $3}'`
			perl -w -e "s:port = $port_num:port = ${dbportnum}:g" -p -i /etc/postgresql/9.6/main/postgresql.conf
			#service postgresql stop
			cp -rf /var/lib/postgresql/9.6/main/*  $dbpath 1> /dev/null 2>> "${WRKDIR}"/SGInstallation.log

			 sudo chown postgres:postgres $dbpath 
	         chmod -R 777 $dbpath/*
			
		    fi
		fi
			service postgresql restart
			echo 
     echo "*:*:*:*:admin" >> /root/.pgpass
        chmod 600 /root/.pgpass			
  if [ "$postgreExits" != "1" ];then
		#altering user role
		sudo -u postgres psql -c "ALTER USER \"postgres\" WITH PASSWORD '$pswd'";
  else
	#	echo -n 1>&2 " Enter user name for connecting to PostgreSQL : Default Username($uname):";
	 #  echo  "\n"
	 #	read uname
	#	echo -n 1>&2 " Enter password for user $uname : ";
	#	echo  "\n"
	#	stty -echo
	#	read pswd
	#	stty echo
		psql -U $uname -w  -c "drop database if exists \"SGDatabase\"";
fi
      psql -U $uname -w  -c "Create database \"SGDatabase\""; 
sed -i -e 's/trust/md5 /g' /etc/postgresql/9.6/main/pg_hba.conf
max_connections=`cat /etc/postgresql/9.6/main/postgresql.conf | grep "max_connections" | cut -d ":" -f2 |  sed 's/[^0-9]//g'`

 if [ "$max_connections" != "2000" ];then
    sed -i -e 's/max_connections = 100/max_connections = 2000/g'  /etc/postgresql/9.6/main/postgresql.conf
 fi

echo  "\n"
#oldpwd=`cat /usr/local/etc/odbc.ini | grep "Password" | cut -d "=" -f2`
#/bin/sed -i -e "s/$oldpwd/${pswd}/g" /usr/local/etc/odbc.ini
rm -rf /root/.pgpass 1>>/dev/null 2>>/dev/null
service postgresql restart

 }
 
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 " >> "${WRKDIR}"/SGInstallation.log
		echo "********************************************"
		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
}
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
                echo "odbcurl==============";
                echo $odbcURL
                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

}

mysql_installations()
{
	if [ "`uname -s`" = "Linux" ]; then
                checklic
		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
}
postgres_installations()
{
  if [ "`uname -s`" = "Linux" ]; then
                checklic
		Welcome
		Postgresql_disclaimer
		
		if [ "$postgreExits" = "1" ];then
              sleep 5
		      echo "\n"
			echo "****************************************************************************"
			echo "*************PostgreSQL resumed successfully ***************************"
			echo "****************************************************************************"
			  echo "\n"
		fi
if [ "$postgreExits" != "1" ];then 

if [ -f /etc/lsb-release ] && [ "`egrep -i "SUSE" /proc/version`"  = "" ]; then
				echo "\033[35m\nVembu BDR 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\nVembu BDR 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 "********************************************************************************"
echo "Default storage location for PostgreSQL : '/var/lib/postgresql/9.6/main' "
echo "********************************************************************************"
    
		while true; do
			if [ ${IS_SILENT_INSTALLATION} -eq 0 ]; then
				echo -n "Do you want to change the default storage location for the PostgreSQL [y/n] : "
				read response
			else
				response="N"
			fi
			case ${response} in
			[yY]|[yY][eE][sS])
			echo "\n"
			echo "Please specify the path :"
				while true; do
				read dbPath
				DB_PATH_FOR_README="${dbPath}/postgres"
				case ${dbPath} in 
				[/]*) dbPath="${dbPath}/postgres"
                      SG_METADBS_DIRPATH=$dbPath
				#creating the parent directory directory.	
				[ ! -d "${SG_METADBS_DIRPATH}" ] && { 
				mkdir -p "${SG_METADBS_DIRPATH}"
				chmod 700 ${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
			 break;;	
			[nN]|[nN][oO])
			
                  dbPath="/var/lib/postgresql/9.6/main"
				  DB_PATH_FOR_README="${dbPath}/postgres"
                   break;;	
              *)
				echo "Please provide correct input [y/n] to proceed."
				echo
				continue;;
			esac
		done
		
		echo "********************************************************************************"
		echo "Default port for PostgreSQL : '32010' "
		echo "********************************************************************************"
		while true
		do
			if [ ${IS_SILENT_INSTALLATION} -eq 0 ]; then
				echo -n "Do you want to change the default port for the PostgreSQL [y/n] : "
				read response
			else
				response="N"
			fi
			case ${response} in
			[yY]|[yY][eE][sS])
				echo "\n"
				echo "Please specify the port :"
					while true; do
					read dbPortNum
					case ${dbPortNum} in 
					[0-9]*) dbPortNum=${dbPortNum}
					isAvailPort=$(is_port_free $dbPortNum)
					if [ "${isAvailPort}" = 1 ]; then
					echo "********************************"
					elif [ "${isAvailPort}" = 2 ]; then
					echo "Please specify the valid port number between 1024 and 65535 to proceed"
					continue
					else
					echo "Given Postgresql server port is currently occupied by another process. Enter another valid port number:"
					continue
					fi
					break;;
					*)
						echo "Please specify the valid port number between 1024 and 65535 to proceed."
						continue
					esac
					done
				break;;
			 
			[nN]|[nN][oO])
			
				dbPortNum=32010
				isAvailPort=$(is_port_free $dbPortNum)
				if [ "${isAvailPort}" = 1 ]; then
				echo "********************************"
				else
				echo "Pre-defined Postgresql server port '32010' is currently occupied by another process. Enter another valid port number:"
				while true; do
				read newPortNum
				case ${newPortNum} in
				[0-9]*) newPortNum=${newPortNum}
				isNewPortAvail=$(is_port_free $newPortNum)
				if [ "${isNewPortAvail}" = 1 ]; then
				echo "********************************"
				elif [ "${isAvailPort}" = 2 ]; then
				echo "Please specify the valid port number between 1024 and 65535 to proceed"
				continue
				else
				echo "Given Postgresql server port is currently occupied by another process. Enter another valid port number:"
				continue
				fi
				break;;
				*)
					   echo "Please specify the valid port number between 1024 and 65535 to proceed."
					   continue
				esac
				done
				fi
				break;;	
	            	*)
				echo "Please provide correct input [y/n] to proceed."
				echo
				continue;;
			esac
		done	
fi
	

echo "*********************************************************************************************************"
        echo "Installing Unix ODBC and Postgresql driver please wait##############################"
		
sudo apt-get --assume-yes install unzip 1> /dev/null 2>> "${WRKDIR}"/SGInstallation.log
sudo apt-get --assume-yes install build-essential 1> /dev/null 2>> "${WRKDIR}"/SGInstallation.log

Installing_unixodbc_Postgresql
odbc_configuration_postgresql $dbPortNum
echo "*****************************************************************************************************"
echo "###########################Installing_unixodbc  finished##############################"
echo "***********************************************************"
echo "#####################Installing postgresql database please wait##############################"
echo "*****************************************************************************************************"
		Install_Postgresql_Database
		Install_Psql_Driver
	
		Postgresql_server_password_setting $dbPath $dbPortNum
	echo
			echo " SUCCESS : ODBC driver is successfully connected to PostgreSQL server"
			echo
			echo "*************************************************************"
			

   odbc_database=`cat /usr/local/etc/odbc.ini |grep -w "Database" |cut -d"=" -f2 |tr -d " "`
			odbc_hostname=`cat /usr/local/etc/odbc.ini |grep -w "Servername" |cut -d"=" -f2 |tr -d " "`
			odbc_username=`cat /usr/local/etc/odbc.ini |grep -w "UserName" |cut -d"=" -f2 |tr -d " "`
			odbc_password=`cat /usr/local/etc/odbc.ini |grep -w "Password" |cut -d"=" -f2 |tr -d " "`
			odbc_port_num=`cat /usr/local/etc/odbc.ini |grep -w "Port" |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 /usr/local/etc/odbc.ini
			else
				echo "DatasourceName    : PostGreDBConnection"
				echo "DatabaseName      : $odbc_database"
				echo "HostName  	  : $odbc_hostname"
				echo "UserID            : $odbc_username"
				echo "Password  	  : $odbc_password"
				echo "Port			: $odbc_port_num"
			fi
			
			 sleep 5	
			 
	else
		echo " This Online installer will support only on Linux operating systems Like RHEL, Suse, centOS, fedora, mandrake, Debian and Ubuntu."
		Exit
	fi
		
}

is_port_free ()
{
    argPort=$1
	if [ $argPort -lt 1024 ] || [ $argPort -gt 65535 ];
    then
        echo "2"
        return
    fi
    portRes=$(netstat -anpl | grep $argPort -w | wc -l); 
    if [ $portRes -ne 0 ];
    then 
	echo "0"
    else 
	echo "1"
    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
			if [ ${IS_SILENT_INSTALLATION} -eq 0 ]; then
				echo "\nDo you want to install VembuBDR's backup server Now ? say [y/n] : \c"
				read urChoice
			else
				urChoice="Y"
			fi
				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
		if [ ${IS_SILENT_INSTALLATION} -eq 0 ]; then
			echo -n "Do you want to proceed to install MongoDB ? say [y/n] : "
			read urChoice
		else
			urChoice="Y"
		fi
			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 & 16.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 & 16.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 & 16.04 LTS versions only."
			exit
		elif [ ! `cat /etc/lsb-release | grep DISTRIB_RELEASE | cut -d "=" -f2` = "16.04" ] && 
				[ ! `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 "Server Installation is Supported on Ubuntu 12.04, 14.04 & 16.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  
								  
}

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

# Clean the PostgreSQL and Databases

Clean_PostgreSQL()
{

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

						cd ${SERVICE_DIR}
						

							apt-get -y --purge remove postgresql 1>/dev/null 2>/dev/null
							apt-get -y --purge remove postgresql-client 1>/dev/null 2>/dev/null
							apt-get -y --purge remove postgresql-client-9.6 1>/dev/null 2>/dev/null
							apt-get -y --purge remove postgresql-client-common 1>/dev/null 2>/dev/null
							apt-get -y --purge remove postgresql-contrib 1>/dev/null 2>/dev/null
							apt-get -y --purge remove postgresql-common 1>/dev/null 2>/dev/null
							apt-get --purge remove postgresql\* 1>/dev/null 2>/dev/null
							apt-get -y --purge remove libpq-dev 1>/dev/null 2>/dev/null
							rm /var/lib/apt/lists/apt.postgresql.org_pub_repos_apt_dists_xenial-pgdg_main_binary-amd64_Packages
							rm /var/lib/apt/lists/apt.postgresql.org_pub_repos_apt_dists_xenial-pgdg_main_binary-i386_Packages
						  echo "\n"
								echo "************************************************************************"
								echo " Giving PostgreSQL a couple of seconds to exit cleanly before uninstalling."
								sleep 3
								echo "\n"
								echo " PostgreSQL is successfully uninstalled."
								echo "\n"
								echo "  Exiting ..."
								echo "\n"
								echo "************************************************************************"
								rm -rf /etc/init.d/postgresql 2>/dev/null
								rm -rf /etc/postgresql
								rm -rf /var/lib/postgresql/
								rm -rf /var/log/postgresql/
								rm -rf /usr/share/postgresql/
								rm -rf /usr/include/postgresql   
								rm -rf /usr/share/postgresql-common
								rm -rf /usr/share/doc/postgresql
								rm -rf /var/log/postgresql/
								rm -rf /etc/postgresql-common/
								rm -rf /usr/local/etc/odbcinst.ini 
								 rm -rf /usr/local/etc/odbc.ini
								
							sleep 10
							
							

								
								
						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
	
	257) DBVersion="3.7.0";; #3.7.0 Migration starated
         
    258) DBVersion="3.7.0";; #3.7.0 Migration completed
	
	259) DBVersion="3.8.0";; #3.8.0 Migration starated
         
    260) DBVersion="3.8.0";; #3.8.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 successfully" 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
}


###Checking the PostgreSQL Version#######################
if [ -f /etc/init.d/postgresql ] && [ ! -f /etc/init.d/vembubdr ];then
version=`psql  -V | sed 's/[^0-9]//g'`
fi

if [ "$version" != "" ];then
    if [ "$version" -le 960 ] ||  [ "$version" -ge 970 ];then
	echo "Looks like an Incompatible PostgreSQl is already installed in your machine."
	echo 
	tput setaf 3
	echo "   |====================================================================|"
	echo "   |                                                                    |"
	echo "   |      1. Uninstall and Remove all the existing PostgreSQL services  |"
	echo "   |      2. Exit                                                       |"
	echo "   |                                                                    |"
	echo "   |====================================================================|"
	tput sgr0
	echo 
        echo 
	
	

			if [ ${IS_SILENT_INSTALLATION} -eq 0 ]; then
				echo -n "Enter your choice[ 1 / 2 ] :"
				read reply
			else
				reply=2;
			fi
				case ${reply} in
				
				    1)
						    apt-get -y --purge remove postgresql 1>/dev/null 2>/dev/null
							apt-get -y --purge remove postgresql-client 1>/dev/null 2>/dev/null
							apt-get -y --purge remove postgresql-client-common 1>/dev/null 2>/dev/null
							apt-get -y --purge remove postgresql-contrib 1>/dev/null 2>/dev/null
							apt-get -y --purge remove postgresql-common 1>/dev/null 2>/dev/null
							apt-get --purge remove postgresql\* 1>/dev/null 2>/dev/null
							rm /var/lib/apt/lists/apt.postgresql.org_pub_repos_apt_dists_xenial-pgdg_main_binary-amd64_Packages
							rm /var/lib/apt/lists/apt.postgresql.org_pub_repos_apt_dists_xenial-pgdg_main_binary-i386_Packages
						  echo "\n"
								echo "************************************************************************"
								echo " Giving PostgreSQL a couple of seconds to exit cleanly before uninstalling."
								sleep 3
								echo "\n"
								echo " PostgreSQL is successfully uninstalled."
								echo "\n"
								echo "  Exiting ..."
								echo "\n"
								echo "************************************************************************"
									rm -rf /etc/init.d/postgresql 2>/dev/null
									rm -rf /etc/postgresql
									rm -rf /var/lib/postgresql/
									rm -rf /usr/share/postgresql/
									rm -rf /usr/include/postgresql   
									rm -rf /usr/share/postgresql-common
									rm -rf /usr/share/doc/postgresql
									rm -rf /var/log/postgresql/
									rm -rf /etc/postgresql-common/
                                                 exit 1;;
						2)   exit 1;;
						
						*)
													echo "\n"
													echo "Please enter [1/2] to procced further."
													continue;;
						
						esac

             fi
fi			 
	
	
# Uninstall the VembuBDR along with the MySql and MONGO
#####################################################################################
	
if [ -f ${SERVICE_DIR}/vembubdr ] && [ -f ${SERVICE_DIR}/mysql ] && [ ! -f ${SERVICE_DIR}/postgresql ]; then
USE_DATABASE=1
        # 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.
	if [ ! "${previousVersion}" -eq "($SGVN)"];then
	
		     check_previous_dbversion
fi

	if [ "${previousVersion}" = "3.8.0" ] || [ "${previousVersion}" = "3.9.0" ];  then

		echo	"	Looks like Vembu BDR is already installed in your machine."
		echo 
		tput setaf 3
		echo	"	============================================================================================================"
		echo	"	|                                                						           |"
		echo	"	|      1. Upgrade Now                                                                                      |"
		echo	"	|      2. Uninstall and Remove all the existing Vembu BDR, MySQL & MongoDB services [Clean Uninstallation] |"
		echo	"	|      3. Exit                                                                                             |"
		echo	"	|                                                						           |"
		echo	"	============================================================================================================"
		echo
		tput sgr0
		while true; do	
		echo "\n"		
			if [ ${IS_SILENT_INSTALLATION} -eq 0 ]; then
				echo -n "Enter your choice[ 1 / 2 / 3 ] :"
				read reply
			else
				reply=1
			fi
				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}/OnlineInstaller_uninstall.sh" ]; then
																	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
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.8.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 ] && [ ! -f ${SERVICE_DIR}/postgresql ]; then

 USE_DATABASE=1 
				previousVersion=`cat ${WRKDIR}/Vembu/VembuBDR/conf/StoreGridInfo.conf | grep "version" | cut -d "=" -f2`  2>>/dev/null
				
				### This is for DB Migration check purpose.
		if [ ! "${previousVersion}" -eq "($SGVN)"];then
		         check_previous_dbversion
				 fi

	if [ "${previousVersion}" = "3.8.0" ] || [ "${previousVersion}" = "3.9.0" ]; then

		echo	"	Looks like Vembu BDR is already installed in your machine."
		echo 
		tput setaf 3
		echo	"	============================================================================================================"
		echo	"	|                                                						           |"
		echo	"	|      1. Upgrade Now                                                                                      |"
		echo	"	|      2. Uninstall and Remove all the existing Vembu BDR, MySQL & MongoDB services [Clean Uninstallation] |"
		echo	"	|      3. Exit                                                                                             |"
		echo	"	|                                                						           |"
		echo	"	============================================================================================================"
		echo
		tput sgr0

		while true; do	
		if [ ${IS_SILENT_INSTALLATION} -eq 0 ]; then
			echo "\n"		
			echo -n "Enter your choice[ 1 / 2 / 3 ] :"
			read reply
		else
			reply=1
		fi
			case ${reply} in
						1)
						    break;;
						2)     
							while true; do
							if [ ${IS_SILENT_INSTALLATION} -eq 0 ]; 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]: ? "
								read proceed
							else
								proceed="Y"
							fi
									case ${proceed} in
											[yY]|[yY][eE][sS])
													cd ${WRKDIR}/Vembu/VembuBDR
													echo "\n"
													sh OnlineInstaller_uninstall.sh 2>> /dev/null
													echo "\n"
													# For Checking whether properly installed or not
													if [ ! -f "${WRKDIR}/Vembu/VembuBDR/OnlineInstaller_uninstall.sh" ]; then
														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.8.0 and then install Vembu BDR $SGVN version"
     echo ""
				echo "*********************************************************************************"
				echo ${upgradeError}
				echo "*********************************************************************************"
				echo ""  
                     exit 1;  		
	fi		

### If VembuBDR is installed with the PostgreSQL
##================================================

elif [ -f ${SERVICE_DIR}/vembubdr ] && [ -f ${SERVICE_DIR}/postgresql ]; then
USE_DATABASE=2
        # 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
		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.
		if [ ! "${previousVersion}" -eq "($SGVN)"];then
		     check_previous_dbversion
			 fi

	if [ "${previousVersion}" = "3.8.0" ] || [ "${previousVersion}" = "3.9.0" ]; then
	
		echo	"	Looks like Vembu BDR is already installed in your machine."
		echo 
		tput setaf 3
		echo	"	======================================================================================================="
		echo	"	|                                                						      |"
		echo	"	|      1. Upgrade Now                                                                                 |"
		echo	"	|      2. Uninstall and Remove all the existing Vembu BDR, PostgreSQl services [Clean Uninstallation] |"
		echo	"	|      3. Exit                                                                                        |"
		echo	"	|                                                						      |"
		echo	"	======================================================================================================="
		echo
		tput sgr0

		while true; do	
		if [ ${IS_SILENT_INSTALLATION} -eq 0 ]; then
			echo "\n"		
			echo -n "Enter your choice[ 1 / 2 / 3 ] :"
			read reply
		else
			reply=1
		fi
				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, PostgreSQl services and its data. Would you like to continue anyway [y/n]: ? "
									while true; do
									if [ ${IS_SILENT_INSTALLATION} -eq 0 ]; then
										read proceed
									else
										proceed="Y"
									fi
										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}/OnlineInstaller_uninstall.sh" ]; then
																	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_PostgreSQL
																	###############################################
															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, PostgreSQL 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.8.0 and then install Vembu BDR $SGVN version"
     echo ""
				echo "*********************************************************************************"
				echo ${upgradeError}
				echo "*********************************************************************************"
				echo "" 
                 exit 1;  
				
    fi
								


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

elif [ ! -f ${SERVICE_DIR}/vembubdr ] && [ -f ${SERVICE_DIR}/postgresql ]; then
			echo "\n"
	USE_DATABASE=2							
	echo "	Looks like PostgreSQL installations found in your machine."
	echo	
	tput setaf 3
	echo "	========================================================================================"
	echo "	|                                                                                      |"
	echo "	|      1. Use existing PostgreSQL services and proceed installation.                   |"
	echo "	|      2. Uninstall and Remove existing PostgreSQL services and then install afresh.   |"
	echo "	|      3. Exit                                                                         |"
	echo "	|                                                                                      |"
	echo "	========================================================================================"
	echo
	tput sgr0
	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            "
	echo "	    location (other than /home/vembubdr). Then copy the installer to the installation          "
	echo "	    location and then run this installer to upgrade or remove existing installation            "
	tput sgr0
	echo "    *********************************************************************************************"
			
						echo "\n"
					while true; do	
					if [ ${IS_SILENT_INSTALLATION} -eq 0 ]; then
						echo "\n"		
						echo -n "Enter your choice[ 1 / 2 / 3 ] :"
						read reply
					else
						reply=1
					fi
								case ${reply} in
											1)
											postgreExits=1
											     break;;
											2)          
												while true; do
												if [ ${IS_SILENT_INSTALLATION} -eq 0 ]; then
														echo "\n"
														echo -n "This will uninstall and delete the existing PostgreSQL services and its data. Would you like to continue anyway [y/n]: ? "
												        read proceed
												else
													proceed="Y"
												fi
												                case ${proceed} in
																		[yY]|[yY][eE][sS])
																		    Clean_PostgreSQL
																		exit 1;;
																		[nN]|[nN][oO])
																		exit 1;;
																		*)
																		echo "Please enter [y/n] to procced uninstall and delete the existing PostgreSQL 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					

					
else
    ##Note : This is a Fresh installation so we have to proceed PostgreSQL with the VembuBDR
	ODBC_Version=`odbcinst -j | grep unixODBC | awk '{print $2}'` 1> /dev/null 2> /dev/null
	if [ "$ODBC_Version" != "2.3.4" ] && [ "$ODBC_Version" != "" ] ;then
	   echo "Seems like incompatible UnixODBC version already installed in your machine. KIndly uninstall it and try again or please contact Vembu Support"
	   exit
	 fi 
	   USE_DATABASE=2
	   
fi	   
#######################################################################################################################
 if [ "$USE_DATABASE" -eq 2 ]; then
        if [ ! -f ${SERVICE_DIR}/vembubdr ];then
		postgres_installations
		else
		    checklic
		    Welcome
			Postgresql_disclaimer
		   sleep 5
		        echo "\n"
				echo "**********************************************************************************************"
				echo "*************PostgreSQL resumed successfully ***************************"
				echo "**********************************************************************************************"
				  echo "\n"
		fi		
	else
	     if [ -f ${SERVICE_DIR}/vembubdr ];then
                        checklic
		        Welcome
				preparing_installs
	        	echo "**********************************************************************************************"
				echo "*************Mysql and MongoDB resumed successfully ***************************"
				echo "**********************************************************************************************"
				  echo "\n"
				  sleep 5
		else
		mysql_installations
		MogoDB_installations
	   fi	
	fi
	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 
