#!/bin/bash

SCRIPT_NAME=$(basename "$0")
LOCK_FILE="/tmp/${SCRIPT_NAME}.lock"

exec 200>"${LOCK_FILE}"
flock -n 200 || {
	echo "Another instance is running. Exiting."
	exit 1
}

countdown_seconds () {
	seconds=$1
	for ((i=seconds; i>0; i--)); do
		echo "$i"
		sleep 1
	done
}


SET_ID_LISTS=$(psql -U postgres WebTool -qAt -c "SELECT world_id FROM db_setup WHERE is_commercial = 1 AND is_purchased = 1 AND is_public = 0 AND schedule_time IS NOT NULL AND EXTRACT(EPOCH FROM now())::INTEGER >= EXTRACT(EPOCH FROM schedule_time)::INTEGER AND is_locked = 1 ORDER BY schedule_time, world_id;")

for new_world_server_id in ${SET_ID_LISTS}; do
	echo "### ${new_world_server_id} ###"
	STATE=$(ssh -o StrictHostKeyChecking=no ACCOUNTDB "psql -U postgres N1Account -qAt -c \"SELECT state FROM worlds WHERE id = ${new_world_server_id};\"")
	if [ ${STATE} -eq 1 ]; then
		echo "$HOME/bin/reset_server_world_id_silent_mode_rs ${new_world_server_id}"
		STATUS=$($HOME/bin/reset_server_world_id_silent_mode_rs ${new_world_server_id})
		if [ ${STATUS} -eq 0 ]; then
			$HOME/bin/send_chatbot_text_only "系統腳本" "[滾服流程] s${new_world_server_id} 重置伺服器天數回到第一天"
			#sleep 10
			countdown_seconds 30
			echo "ssh -o StrictHostKeyChecking=no ACCOUNTDB \"psql -U postgres N1Account -c \"UPDATE worlds SET maxnum_user = 1200 WHERE id = ${new_world_server_id};\"\""
			ssh -o StrictHostKeyChecking=no ACCOUNTDB "psql -U postgres N1Account -c \"UPDATE worlds SET maxnum_user = 1200 WHERE id = ${new_world_server_id};\""
			$HOME/bin/send_chatbot_text_only "系統腳本" "[滾服流程] s${new_world_server_id} 開放對外入口"
			if [ $((${new_world_server_id} % 100)) -ne 1 ]; then
				#echo ${new_world_server_id} $((${new_world_server_id} % 100))
				old_world_server_id=$((${new_world_server_id} - 1))
				
				BPW=$(psql -U postgres WebTool -qAt -c "SELECT block_previous_world FROM db_setup WHERE is_commercial = 1 AND world_id = ${new_world_server_id};")

				if [ ${BPW} -eq 1 ]; then
					echo "ssh -o StrictHostKeyChecking=no ACCOUNTDB \"psql -U postgres N1Account -c \"UPDATE worlds SET nochar_block_login = 1 WHERE id = ${old_world_server_id};\"\""
					ssh -o StrictHostKeyChecking=no ACCOUNTDB "psql -U postgres N1Account -c \"UPDATE worlds SET nochar_block_login = 1 WHERE id = ${old_world_server_id};\""
					$HOME/bin/send_chatbot_text_only "系統腳本" "[滾服流程] s${old_world_server_id} 入口已關閉"
				fi
			fi
			echo "psql -U postgres WebTool -c \"UPDATE db_setup SET is_public = 1, public_time = NOW() WHERE is_commercial = 1 AND world_id = ${new_world_server_id};\""
			psql -U postgres WebTool -c "UPDATE db_setup SET is_public = 1, public_time = NOW() WHERE is_commercial = 1 AND world_id = ${new_world_server_id};"
			$HOME/bin/send_chatbot_text_only "系統腳本" "[滾服流程] s${new_world_server_id} 已完成對外所有流程"
		elif [ ${STATUS} -eq 1 ]; then
			echo "Call CGI reset_server+${new_world_server_id} Failed, please wait for the next run!"
			continue
		fi
	elif [ ${STATE} -eq 0 ]; then
		echo "${new_world_server_id} 關閉中..." 
		continue
	fi
	echo
done
