#!/bin/sh
# Copyright (c) 2002-2006 by Rack-Soft, LLC
# Copyright (c) 2002-2006 by 4PSA (www.4psa.com)
# All rights reserved
# Adds the IP of the remote server to the current server

pre_install()
{
TARGET_OS="`uname -a`"
case "${TARGET_OS}" in
	FreeBSD*i386)
		uudecode="uudecode -p"
		sys="freebsd"
		root_group="wheel"
		;;
	Linux*2.?.*?86*)
   		uudecode="uudecode -o /dev/stdout"
		root_group="root"
   		if [ -f /etc/debian_version ];then
			sys="debian"
		fi
		if [ -f /etc/SuSE-release ];then
			sys="suse"
		fi
   		if [ -f /etc/redhat-release ];then
   			mandrake=`cat /etc/redhat-release | grep -i "mandrake"`
	   		if [ -z "${mandrake}" ];then
	   			sys="redhat"
	   		else 
	   			sys="mandrake"
	   		fi
		fi
		;;
	*)	echo "You do not have a supported OS."
		exit 0
		;;
esac
}

mysql_passwd=`cat /etc/psa/.psa.shadow`
mysql_path=`cat /etc/psa/psa.conf | grep -i "MYSQL_BIN_D" | awk '{print $2}'`

prompt_for_ip=1
while [ "${prompt_for_ip}" -eq "1" ] ; do
	echo -n 'Main server IP address > '
	read ip
	echo ${ip} | egrep -i "^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$" > /dev/null 2>&1
	if [ "$?" -ne "0" ] ; then
		echo 'IP address is invalid.'
		continue
	fi
	ip_a=`echo $ip | cut -d'.' -f1`
	ip_b=`echo $ip | cut -d'.' -f2`
	ip_c=`echo $ip | cut -d'.' -f3`
	ip_d=`echo $ip | cut -d'.' -f4`
	
	if [ "${ip_a}" -gt "255" -o "${ip_a}" -lt "0" ];then
		echo 'IP address is invalid.'
		continue	
	fi
	if [ "${ip_b}" -gt "255" -o "${ip_b}" -lt "0" ];then
		echo 'IP address is invalid.'
		continue	
	fi	
	if [ "${ip_c}" -gt "255" -o "${ip_c}" -lt "0" ];then
		echo 'IP address is invalid.'
		continue	
	fi
	if [ "${ip_d}" -gt "255" -o "${ip_d}" -lt "0" ];then
		echo 'IP address is invalid.'
		continue	
	fi	
	prompt_for_ip=0
done

#check for existing record in the user table
line=`${mysql_path}/mysql -Bs -uadmin -p${mysql_passwd} -Dmysql -e"SELECT CONCAT_WS(\"~\", Host, User, Select_priv) FROM user WHERE Host=\"$ip\" AND User=\"admin\""`
priv=`echo $line | cut -d'~' -f3`
if [ -z "$priv" ] ; then
	${mysql_path}/mysql -Bs -uadmin -p${mysql_passwd} -Dmysql -e"INSERT INTO user (Host, User, user.Password, Select_priv) VALUES ('$ip', 'admin', PASSWORD('$mysql_passwd'), 'Y')"
	if [ "$?" -ne 0 ]; then
		echo 'Error: Could not make the change in the database.'
		exit 1
	fi
	${mysql_path}/mysql -Bs -uadmin -p${mysql_passwd} -e"FLUSH PRIVILEGES"
elif [ "$priv" = "N" ] ; then
	${mysql_path}/mysql -Bs -uadmin -p${mysql_passwd} -Dmysql -e"UPDATE user SET Select_priv='Y' WHERE Host='$ip' AND User='admin'"
	if [ "$?" -ne 0 ]; then
		echo 'Error: Could not make the change in the database.'
		exit 1
	fi
	${mysql_path}/mysql -Bs -uadmin -p${mysql_passwd} -e"FLUSH PRIVILEGES"
fi

#remove the skip-networking directive in the my.cnf file

pre_install

if [ ${sys} != 'freebsd' ];then
	if [ ${sys} = 'debian' ];then
		if [ -f /etc/mysql/my.cnf ];then
			bindadr=`grep -w "^bind-address" /etc/mysql/my.cnf`
			if [ ! -z "${bindadr}" ];then
				${mysql_path}/replace "bind-address" "#bind-address" -- /etc/mysql/my.cnf
				if [ "$?" -ne 0 ] ; then
					echo 'Error: Could not make the change in the my.cnf file.'
					exit 1
				else
					echo 'You might have to restart Mysql daemon in order for changes to become effective'		
				fi
			fi
		fi
	fi

	if [ -f /etc/my.cnf ];then
		${mysql_path}/replace "skip-networking" "" -- /etc/my.cnf
		if [ "$?" -ne 0 ] ; then
			echo 'Error: Could not make the change in the my.cnf file.'
			exit 1
		else
			echo 'You might have to restart Mysql daemon in order for changes to become effective'	
		fi
	fi
fi

echo 'Done.'

