#!/bin/bash
pwd_path="/home/qa-deploy-utils/qa_shell_script"
config_shell_path="$pwd_path"
config_server="$pwd_path/config_server"
filename="/home/quant_group/frp_0.12.0_linux_amd64/frpc_custom_new_1.ini"
nginxname="/usr/local/openresty/nginx/conf/conf.d"
 
ips=`cat $config_server/get_env_ip.sh`
rm -rf $filename
function addFrpcHead()
{
	echo "[common]" >> $1
	echo "server_addr = 127.0.0.1" >> $1
	echo "server_port = 7000" >> $1
	echo "privilege_token = 12345678" >> $1
	echo "log_file = ./frpc.log" >> $1
	echo "log_level = info" >> $1
	echo "log_max_days = 3" >> $1
	echo "" >> $1
}
addFrpcHead $filename

function addFrpcSub()
{
	sub_domain=$1
	sub_type=$2
	sub_ip_first=$3
	sub_ip_last=$4
	sub_port=$5
	sub_custom_domain=$6
	sub_file=$7
	
	echo "" >>$sub_file
	echo "[$sub_domain-$sub_custom_domain]" >> $sub_file
	echo "type = $sub_type" >> $sub_file
	echo "local_ip = $sub_ip_first.$sub_ip_last" >> $sub_file
	echo "local_port = $sub_port" >> $sub_file
	echo "subdomain = $sub_custom_domain" >> $sub_file
}
function addFrpcLoc()
{
	sub_domain=$1
	sub_type=$2
	sub_port=$3
	cus_domain=$4
	sub_loc=$5
	sub_file=$6

	echo "" >> $sub_file
	echo "[$sub_domain]" >> $sub_file
	echo "type = $sub_type" >> $sub_file
	echo "local_port = $sub_port" >> $sub_file
	echo "custom_domains = $cus_domain" >> $sub_file
	echo "locations = $sub_loc" >> $sub_file
}

addFrpcLoc local_ http 8000 q-gp.com / $filename


addFrpcSub store http 127.0.0 1 8001 store  $filename
addFrpcSub h5 http 192.168.4 26 7051 h5 $filename
addFrpcSub mock http 172.30.199 2 80 mock $filename
addFrpcSub atm http 192.168.4 18 3000 atm $filename
addFrpcSub auth-center http 192.168.4 193 8099 auth-center-38 $filename
addFrpcSub company http 172.30.199 2 10087 company-241 $filename
addFrpcSub bp http 172.30.199 2 10084 bp $filename
addFrpcSub qahelp http 172.30.199 2 8000 qahelp $filename






function createNginxSSLConf()
{
	cus_domain=$1
	cus_ip=$2
	cus_port=$3
	file_name=$4
	
	rm -rf $file_name
	touch $file_name
	
	echo "server {" >> $file_name
	echo "    listen       443 ssl;" >> $file_name
	echo "    server_name  $cus_domain.q-gp.com;" >> $file_name
	echo "" >> $file_name
	echo "    ssl_certificate      /home/q-gp/ssl/certificate.crt;" >> $file_name
	echo "    ssl_certificate_key  /home/q-gp/ssl/private.key;" >> $file_name
	echo "" >> $file_name
	echo "    ssl_session_cache    shared:SSL:1m;" >> $file_name
	echo "    ssl_session_timeout  5m;" >> $file_name
	echo "" >> $file_name
	echo "    ssl_ciphers  HIGH:!aNULL:!MD5;" >> $file_name
	echo "    ssl_prefer_server_ciphers  on;" >> $file_name
	echo "" >> $file_name
	echo "    location / {" >> $file_name
	echo "        proxy_pass http://$cus_ip:$cus_port;" >> $file_name
	echo "    }" >> $file_name
	echo "}" >> $file_name
	
}

function createNginxHttpAndSSLConf()
{
	cus_domain=$1
	cus_ip=$2
	cus_port=$3
	file_name=$4
	
	rm -rf $file_name
	touch $file_name
	
	echo "server {" >> $file_name
	echo "    listen 8000;" >> $file_name
	echo "    server_name  $cus_domain.q-gp.com;" >> $file_name
	echo "    proxy_set_header Host  \$host;"  >> $file_name
	echo "" >> $file_name
	echo "    location / {" >> $file_name
	echo "        proxy_pass http://$cus_ip;" >> $file_name
	echo "    }" >> $file_name
	echo "}" >> $file_name
	echo "" >> $file_name
	echo "server {" >> $file_name
	echo "    listen       443 ssl;" >> $file_name
	echo "    server_name  $cus_domain.q-gp.com;" >> $file_name
	echo "" >> $file_name
	echo "    ssl_certificate      /home/q-gp/ssl/certificate.crt;" >> $file_name
	echo "    ssl_certificate_key  /home/q-gp/ssl/private.key;" >> $file_name
	echo "" >> $file_name
	echo "    ssl_session_cache    shared:SSL:1m;" >> $file_name
	echo "    ssl_session_timeout  5m;" >> $file_name
	echo "" >> $file_name
	echo "    ssl_ciphers  HIGH:!aNULL:!MD5;" >> $file_name
	echo "    ssl_prefer_server_ciphers  on;" >> $file_name
	echo "    proxy_set_header Host  \$host;"  >> $file_name
	echo "" >> $file_name
	echo "    location / {" >> $file_name
	echo "        proxy_pass http://$cus_ip;" >> $file_name
	echo "    }" >> $file_name
	echo "}" >> $file_name
	
}


# projectAndPort=`node $config_server/get_project_config.js  -type multi -attr port`
createNginxSSLConf store 172.30.199.2 9000 $nginxname/store.conf

for ip_last in ${ips[@]}
do
	projectAndPorts=`node $config_server/get_project_config.js  -type multi -attr port -attr2 true`
	for projectAndPort in ${projectAndPorts[@]}
	do
		var=(${projectAndPort//|/ })
		addFrpcSub ${var[0]} http 192.168.4 $ip_last ${var[1]} ${var[0]}-${ip_last} $filename
		createNginxSSLConf ${var[0]}-${ip_last} 192.168.4.$ip_last ${var[1]} $nginxname/${var[0]}-${ip_last}.conf
	done
done 

for host_name in {0..1}
do 
	projectAndPorts=`node $config_server/get_project_config.js  -type multi -attr port/url -attr2 true`
	for projectAndPort in ${projectAndPorts[@]}
	do
		var=(${projectAndPort//|/ })
		addFrpcSub ${var[0]}-${var[2]} http 127.0.0 1 8000 ${var[2]}-qa${host_name} $filename
		createNginxHttpAndSSLConf ${var[2]}-qa${host_name} 192.168.6.6 ${var[1]} $nginxname/${var[0]}-qa${host_name}.conf
	done	

done


