# -------------------------------------------------------------------------------
# Filename:    database_replace_ip.sh
# Revision:    1.0
# Date:        2016/09
# Author:      Xiaolin.zhu
# Description: 该脚本根据备份sql称特定环境使用的sql，例如：xyqb_192.168.4.3。
# Usage：used by ../deploy/database_sync.sh
# -------------------------------------------------------------------------------
database_name=$1
mysql_host=$2
server_host=$2
db_backup=$3
use_public_ip=$4
public_ip="www.q-gp.com"

echo $server_host
cd $db_backup
cp ${database_name}_latest.sql ${database_name}_${mysql_host}.sql
# mac
# sed -i '' "s/http\:\/\/m.xyqb.com/http\:\/\/${server_host}:7001/g" ${database_name}_${mysql_host}.sql
# sed -i ''  "s/http\:\/\/bt.xyqb.com/http\:\/\/${server_host}:7002/g" ${database_name}_${mysql_host}.sql
# sed -i '' "s/http\:\/\/api.xyqb.com/http\:\/\/${server_host}:7003/g" ${database_name}_${mysql_host}.sql
# sed -i '' "s/http\:\/\/r.xyqb.com/http\:\/\/${server_host}:7004/g" ${database_name}_${mysql_host}.sql
# sed -i '' "s/http\:\/\/rapi.xyqb.com/http\:\/\/${server_host}:7005/g" ${database_name}_${mysql_host}.sql
# sed -i '' "s/http\:\/\/payapi.xyqb.com/http\:\/\/${server_host}:7006/g" ${database_name}_${mysql_host}.sql
# sed -i '' "s/http\:\/\/pay.xyqb.com/http\:\/\/${server_host}:7007/g" ${database_name}_${mysql_host}.sql
# sed -i '' "s/http\:\/\/off.xyqb.com/http\:\/\/${server_host}:7008/g" ${database_name}_${mysql_host}.sql
# sed -i '' "s/http\:\/\/offapi.xyqb.com/http\:\/\/${server_host}:7010/g" ${database_name}_${mysql_host}.sql
# sed -i '' "s/http\:\/\/financial.xyqb.com/http\:\/\/${server_host}:7011/g" ${database_name}_${mysql_host}.sql
# sed -i '' "s/http\:\/\/fsapi.xyqb.com/http\:\/\/${server_host}:7012/g" ${database_name}_${mysql_host}.sql
# sed -i '' "s/http\:\/\/auth.xyqb.com/http\:\/\/${server_host}:7013/g" ${database_name}_${mysql_host}.sql
# sed -i '' "s/http\:\/\/qapi.xyqb.com/http\:\/\/${server_host}:7014/g" ${database_name}_${mysql_host}.sql
# sed -i '' "s/http\:\/\/mallapi.xyqb.com/http\:\/\/${server_host}:7019/g" ${database_name}_${mysql_host}.sql
# linux
sed -i "s/http\:\/\/m.xyqb.com/http\:\/\/${server_host}:7001/g" ${database_name}_${mysql_host}.sql
sed -i "s/https\:\/\/m.xyqb.com/http\:\/\/${server_host}:7001/g" ${database_name}_${mysql_host}.sql
sed -i  "s/http\:\/\/bt.xyqb.com/http\:\/\/${server_host}:7002/g" ${database_name}_${mysql_host}.sql
sed -i  "s/https\:\/\/bt.xyqb.com/http\:\/\/${server_host}:7002/g" ${database_name}_${mysql_host}.sql
sed -i "s/http\:\/\/api.xyqb.com/http\:\/\/${server_host}:7003/g" ${database_name}_${mysql_host}.sql
sed -i "s/https\:\/\/api.xyqb.com/http\:\/\/${server_host}:7003/g" ${database_name}_${mysql_host}.sql
sed -i "s/http\:\/\/r.xyqb.com/http\:\/\/${server_host}:7004/g" ${database_name}_${mysql_host}.sql
sed -i "s/https\:\/\/r.xyqb.com/http\:\/\/${server_host}:7004/g" ${database_name}_${mysql_host}.sql
sed -i "s/http\:\/\/rapi.xyqb.com/http\:\/\/${server_host}:7005/g" ${database_name}_${mysql_host}.sql
sed -i "s/https\:\/\/rapi.xyqb.com/http\:\/\/${server_host}:7005/g" ${database_name}_${mysql_host}.sql
sed -i "s/http\:\/\/payapi.xyqb.com/http\:\/\/${server_host}:7006/g" ${database_name}_${mysql_host}.sql
sed -i "s/https\:\/\/payapi.xyqb.com/http\:\/\/${server_host}:7006/g" ${database_name}_${mysql_host}.sql
sed -i "s/http\:\/\/pay.xyqb.com/http\:\/\/${server_host}:7007/g" ${database_name}_${mysql_host}.sql
sed -i "s/https\:\/\/pay.xyqb.com/http\:\/\/${server_host}:7007/g" ${database_name}_${mysql_host}.sql
sed -i "s/http\:\/\/off.xyqb.com/http\:\/\/${server_host}:7008/g" ${database_name}_${mysql_host}.sql
sed -i "s/https\:\/\/off.xyqb.com/http\:\/\/${server_host}:7008/g" ${database_name}_${mysql_host}.sql
sed -i "s/http\:\/\/offapi.xyqb.com/http\:\/\/${server_host}:7010/g" ${database_name}_${mysql_host}.sql
sed -i "s/https\:\/\/offapi.xyqb.com/http\:\/\/${server_host}:7010/g" ${database_name}_${mysql_host}.sql
sed -i "s/http\:\/\/financial.xyqb.com/http\:\/\/${server_host}:7011/g" ${database_name}_${mysql_host}.sql
sed -i "s/https\:\/\/financial.xyqb.com/http\:\/\/${server_host}:7011/g" ${database_name}_${mysql_host}.sql
sed -i "s/http\:\/\/fsapi.xyqb.com/http\:\/\/${server_host}:7012/g" ${database_name}_${mysql_host}.sql
sed -i "s/https\:\/\/fsapi.xyqb.com/http\:\/\/${server_host}:7012/g" ${database_name}_${mysql_host}.sql
sed -i "s/http\:\/\/auth.xyqb.com/http\:\/\/${server_host}:7013/g" ${database_name}_${mysql_host}.sql
sed -i "s/https\:\/\/auth.xyqb.com/http\:\/\/${server_host}:7013/g" ${database_name}_${mysql_host}.sql
sed -i "s/http\:\/\/qapi.xyqb.com/http\:\/\/${server_host}:7014/g" ${database_name}_${mysql_host}.sql
sed -i "s/https\:\/\/qapi.xyqb.com/http\:\/\/${server_host}:7014/g" ${database_name}_${mysql_host}.sql
sed -i "s/http\:\/\/mallapi.xyqb.com/http\:\/\/${server_host}:7019/g" ${database_name}_${mysql_host}.sql
sed -i "s/https\:\/\/mallapi.xyqb.com/http\:\/\/${server_host}:7019/g" ${database_name}_${mysql_host}.sql
sed -i "s/http\:\/\/scapi.xyqb.com/http\:\/\/${server_host}:7021/g" ${database_name}_${mysql_host}.sql
sed -i "s/https\:\/\/scapi.xyqb.com/http\:\/\/${server_host}:7021/g" ${database_name}_${mysql_host}.sql
sed -i "s/http\:\/\/gyxdapi.xyqb.com/http\:\/\/${server_host}:7029/g" ${database_name}_${mysql_host}.sql
sed -i "s/https\:\/\/gyxdapi.xyqb.com/http\:\/\/${server_host}:7029/g" ${database_name}_${mysql_host}.sql

sed -i "s/http\:\/\/btapi.xyqb.com/http\:\/\/${server_host}:7025/g" ${database_name}_${mysql_host}.sql
sed -i "s/https\:\/\/btapi.xyqb.com/http\:\/\/${server_host}:7025/g" ${database_name}_${mysql_host}.sql


if [[ "${use_public_ip}" = true ]]; then
	echo "replace private ip to public ip"
	if [ $prefix -lt 100 ]; then
		echo "prefix < 100, ${prefix}"
		sed -i "s/${server_host}:7/${public_ip}:${prefix}/g" ${database_name}_${mysql_host}.sql
	else
		echo "prefix >= 100, ${prefix}"
		sed -i "s/${server_host}:70/${public_ip}:${prefix}/g" ${database_name}_${mysql_host}.sql
	fi
else
	echo "use_public_ip is false, skip replace ip to public ip."
fi



