#!/bin/bash

for db_info in $(psql -U postgres WebTool -c "COPY (SELECT set_id, game_db_account, game_db_pass, game_db_ip, game_db_name, account_db_account, account_db_pass, account_db_ip, account_db_name FROM db_setup WHERE (set_id < 990 AND is_commercial = 1) OR (set_id >= 990 AND is_commercial = 0) ORDER BY set_id) TO STDOUT CSV;"); do
	echo ${db_info}

	SET_ID=$(echo ${db_info} | awk -F"," '{print $1}')
	GAME_DB_ACCOUNT=$(echo ${db_info} | awk -F"," '{print $2}')
	GAME_DB_PASS=$(echo ${db_info} | awk -F"," '{print $3}')
	GAME_DB_IP=$(echo ${db_info} | awk -F"," '{print $4}')
	GAME_DB_NAME=$(echo ${db_info} | awk -F"," '{print $5}')
	ACCOUNT_DB_ACCOUNT=$(echo ${db_info} | awk -F"," '{print $6}')
	ACCOUNT_DB_PASS=$(echo ${db_info} | awk -F"," '{print $7}')
	ACCOUNT_DB_IP=$(echo ${db_info} | awk -F"," '{print $8}')
	ACCOUNT_DB_NAME=$(echo ${db_info} | awk -F"," '{print $9}')

	echo "======"
	echo "SET_ID: $SET_ID"
	echo "GAME_DB: $GAME_DB_ACCOUNT $GAME_DB_PASS $GAME_DB_IP $GAME_DB_NAME"
	PGPASSWORD="$GAME_DB_PASS" psql -U $GAME_DB_ACCOUNT -h $GAME_DB_IP $GAME_DB_NAME -c "SELECT NOW();"
	PGVERSION=$(ssh $GAME_DB_IP "psql -U postgres -c \"COPY (SELECT version()) TO STDOUT;\"" | awk -F"," '{print $1}' | awk -F" " '{print $2}' | awk -F"." '{print $1}')
	ssh $GAME_DB_IP "sudo cat /var/lib/pgsql/${PGVERSION}/data/pg_hba.conf | grep -v '#'"
	echo "ACCOUNT_DB: $ACCOUNT_DB_ACCOUNT $ACCOUNT_DB_PASS $ACCOUNT_DB_IP $ACCOUNT_DB_NAME"
	PGPASSWORD="$ACCOUNT_DB_PASS" psql -U $ACCOUNT_DB_ACCOUNT -h $ACCOUNT_DB_IP $ACCOUNT_DB_NAME -c "SELECT NOW();"
	PGVERSION=$(ssh $ACCOUNT_DB_IP "psql -U postgres -c \"COPY (SELECT version()) TO STDOUT;\"" | awk -F"," '{print $1}' | awk -F" " '{print $2}' | awk -F"." '{print $1}')
	ssh $ACCOUNT_DB_IP "sudo cat /var/lib/pgsql/${PGVERSION}/data/pg_hba.conf | grep -v '#'"
	echo "======"
done
