#!/bin/bash
source ~/.gamerc

for ip in $(cat /etc/hosts | sed -n '/\# PLEASE/,/# DO/p' | egrep 'GAMEDB' | grep -v 'GAMEDB23' | awk -F" " '{print $1}'); do
	DATE=$(date +"%Y%m%d")
	print_servername ${ip} | colorize yellow black
	echo "GAMEDB IP: ${ip}" | colorize blue black
	set=$(print_servername ${ip} | awk -F"_" '{print $1}' | sed 's/GAMEDB//')
	echo "SET: ${set}"
	wz_ip=$(cat /etc/hosts | grep WZ${set}01 | awk -F" " '{print $1}')
	echo "WZ IP: ${wz_ip}"

	# BAN CHAR
	CHAR_ID_LIST=$(ssh ${ip} "psql -U postgres M12DB${set} -c \"COPY (SELECT receiver_id FROM player_mail GROUP BY receiver_id HAVING COUNT(*) >= 20 ORDER BY COUNT(*) DESC) TO STDOUT CSV\"")
	for char_id in ${CHAR_ID_LIST}; do
		echo "curl http://127.0.0.1/cgi-bin/CGIAgent.fcgi?${wz_ip},20060,0KjaM85BjfqjA,ban+${char_id}+1+remove_mail_before"
		curl http://127.0.0.1/cgi-bin/CGIAgent.fcgi?${wz_ip},20060,0KjaM85BjfqjA,ban+${char_id}+1+remove_mail_before
	done

	# 海外需求 #
	ssh ${ip} "psql -U postgres M12DB${set} -c \"SELECT receiver_id, COUNT(*) FROM player_mail GROUP BY receiver_id HAVING COUNT(*) >= 20 ORDER BY COUNT(*) DESC\""
	ssh ${ip} "psql -U postgres M12DB${set} -c \"COPY (SELECT receiver_id, COUNT(*) FROM player_mail GROUP BY receiver_id HAVING COUNT(*) >= 20 ORDER BY COUNT(*) DESC) TO '/tmp/s${set}_player_mail_stat_${DATE}.csv' HEADER CSV\""

	ssh ${ip} "psql -U postgres M12DB${set} -c \"SELECT player_id, item_id, durability, TO_TIMESTAMP(create_time) AS create_time FROM mailitem WHERE player_id IN (SELECT receiver_id FROM player_mail GROUP BY receiver_id HAVING COUNT(*) >= 20 ORDER BY COUNT(*) DESC) ORDER BY player_id, item_id\""
	ssh ${ip} "psql -U postgres M12DB${set} -c \"COPY (SELECT player_id, item_id, durability, TO_TIMESTAMP(create_time) AS create_time FROM mailitem WHERE player_id IN (SELECT receiver_id FROM player_mail GROUP BY receiver_id HAVING COUNT(*) >= 20 ORDER BY COUNT(*) DESC) ORDER BY player_id, item_id) TO '/tmp/s${set}_mailitem_detailed_${DATE}.csv' HEADER CSV\""

	# 完整備份 #
	ssh ${ip} "pg_dump -U postgres M12DB${set} -t player_mail > /tmp/s${set}_player_mail_${DATE}.sql"
	ssh ${ip} "pg_dump -U postgres M12DB${set} -t mailitem > /tmp/s${set}_mailitem_${DATE}.sql"
	ssh ${ip} "psql -U postgres M12DB${set} -c \"COPY (SELECT * FROM mailitem WHERE player_id IN (SELECT receiver_id FROM player_mail GROUP BY receiver_id HAVING COUNT(*) >= 20 ORDER BY COUNT(*) DESC) ORDER BY player_id, item_id) TO '/tmp/s${set}_mailitem_full_${DATE}.csv' HEADER CSV\""
	ssh ${ip} "psql -U postgres M12DB${set} -c \"COPY (SELECT * FROM player_mail WHERE receiver_id IN (SELECT receiver_id FROM player_mail GROUP BY receiver_id HAVING COUNT(*) >= 20 ORDER BY COUNT(*) DESC) ORDER BY receiver_id) TO '/tmp/s${set}_player_mail_full_${DATE}.csv' HEADER CSV\""

	# 資料刪除 #
	ssh ${ip} "psql -U postgres M12DB${set} -c \"DELETE FROM mailitem WHERE player_id IN (SELECT receiver_id FROM player_mail GROUP BY receiver_id HAVING COUNT(*) >= 20 ORDER BY COUNT(*) DESC)\""
	ssh ${ip} "psql -U postgres M12DB${set} -c \"DELETE FROM player_mail WHERE receiver_id IN (SELECT receiver_id FROM player_mail GROUP BY receiver_id HAVING COUNT(*) >= 20 ORDER BY COUNT(*) DESC)\""

	for char_id in ${CHAR_ID_LIST}; do
		echo "curl http://127.0.0.1/cgi-bin/CGIAgent.fcgi?${wz_ip},20060,0KjaM85BjfqjA,ban+${char_id}+1+remove_mail_after"
		curl http://127.0.0.1/cgi-bin/CGIAgent.fcgi?${wz_ip},20060,0KjaM85BjfqjA,ban+${char_id}+1+remove_mail_after
	done

done 
