#!/bin/bash
[ "$1" == "--help" -o "$1" == "-h" ] && exec echo "Usage: $0 <All / PatchRoot / Topaz / Ruby> (optional)"

source $HOME/.gamerc
source $HOME/bin/xigncode_settings

[ "$1" == "" ] && UPDATE_P="All" || UPDATE_P="$1"
ENVIRONMENT_TEST="TEST"
ENVIRONMENT_LIVE="LIVE"
ENVIRONMENT_PREVIOUS="PREVIOUS"

case ${UPDATE_P} in
	PatchRoot)
		echo "PatchRoot"
		XIGNCODE_VERSION_TEST=$(basename ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT_TEST}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/List64/[0-9]*)
		XIGNCODE_VERSION_LIVE=$(basename ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT_LIVE}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/List64/[0-9]*)
		if [ "${XIGNCODE_VERSION_TEST}" == "${XIGNCODE_VERSION_LIVE}" ]; then
			exec echo "No PatchRoot file to flush, please check again."
		else
			#### Sync XignCode List* from TEST to LIVE in TEST Server ###
			echo "[LOCAL] Sync Environment (List*) => ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT_LIVE}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/List*"
						
			if [ ! -d "${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT_LIVE}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}" ]; then mkdir -p ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT_LIVE}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}; chmod -R 777 ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT_LIVE}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}; fi

			rsync -av --delete --progress ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT_TEST}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/List ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT_LIVE}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/.
			rsync -av --delete --progress ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT_TEST}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/List64 ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT_LIVE}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/.

			#### Backup Previous XignCode Version List / List64 ###
			ssh CTRL "if [ ! -d \"${XIGNCODE_PATCH_ROOT_CDN}${ENVIRONMENT_PREVIOUS}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}\" ]; then mkdir -p ${XIGNCODE_PATCH_ROOT_CDN}${ENVIRONMENT_PREVIOUS}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}; fi"
			ssh CTRL "rsync -av --delete --progress ${XIGNCODE_PATCH_ROOT_CDN}${ENVIRONMENT_LIVE}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/List ${XIGNCODE_PATCH_ROOT_CDN}${ENVIRONMENT_PREVIOUS}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/."
			ssh CTRL "rsync -av --delete --progress ${XIGNCODE_PATCH_ROOT_CDN}${ENVIRONMENT_LIVE}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/List64 ${XIGNCODE_PATCH_ROOT_CDN}${ENVIRONMENT_PREVIOUS}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/."

			#### Sync Patch Root Contents List* to XignCode CDN LIVE ###
			echo "[REMOTE] Sync Environment (List*) => ${XIGNCODE_PATCH_ROOT_CDN}${ENVIRONMENT_LIVE}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/List*"
		fi
		;;
	Topaz)
		echo "Topaz"
		TOPAZ_VERSION_TEST=$(basename ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT_TEST}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/Topaz/[0-9]*)
		TOPAZ_VERSION_LIVE=$(basename ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT_LIVE}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/Topaz/[0-9]*)
		if [ "${TOPAZ_VERSION_TEST}" == "${TOPAZ_VERSION_LIVE}" ]; then
			exec echo "No Topaz file to flush, please check again."
		else
			#### Sync XignCode Topaz from TEST to LIVE in TEST Server ###
			echo "[LOCAL] Sync Environment (Topaz) => ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT_LIVE}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/Topaz"

			if [ ! -d "${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT_LIVE}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}" ]; then mkdir -p ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT_LIVE}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}; chmod -R 777 ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT_LIVE}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}; fi

			rsync -av --delete --progress ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT_TEST}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/Topaz ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT_LIVE}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/.

			#### Backup Previous XignCode Version Topaz ###
			ssh CTRL "if [ ! -d \"${XIGNCODE_PATCH_ROOT_CDN}${ENVIRONMENT_PREVIOUS}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}\" ]; then mkdir -p ${XIGNCODE_PATCH_ROOT_CDN}${ENVIRONMENT_PREVIOUS}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}; fi"
			ssh CTRL "rsync -av --delete --progress ${XIGNCODE_PATCH_ROOT_CDN}${ENVIRONMENT_LIVE}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/Topaz ${XIGNCODE_PATCH_ROOT_CDN}${ENVIRONMENT_PREVIOUS}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/."

			#### Sync Patch Root Contents Topaz to XignCode CDN LIVE ###
			echo "[REMOTE] Sync Environment (Topaz) => ${XIGNCODE_PATCH_ROOT_CDN}${ENVIRONMENT_LIVE}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/Topaz"
		fi
		;;
	Ruby)
		echo "Ruby"
		RUBY_VERSION_TEST=$(basename ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT_TEST}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/Ruby/[0-9]*)
		RUBY_VERSION_LIVE=$(basename ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT_LIVE}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/Ruby/[0-9]*)
		if [ "${RUBY_VERSION_TEST}" == "${RUBY_VERSION_LIVE}" ]; then
			exec echo "No Ruby file to flush, please check again."
		else
			#### Sync XignCode Ruby from TEST to LIVE in TEST Server ###
			echo "[LOCAL] Sync Environment (Ruby) => ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT_LIVE}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/Ruby"

			if [ ! -d "${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT_LIVE}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}" ]; then mkdir -p ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT_LIVE}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}; chmod -R 777 ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT_LIVE}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}; fi

			rsync -av --delete --progress ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT_TEST}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/Ruby ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT_LIVE}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/.

			#### Backup Previous XignCode Version Ruby ###
			ssh CTRL "if [ ! -d \"${XIGNCODE_PATCH_ROOT_CDN}${ENVIRONMENT_PREVIOUS}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}\" ]; then mkdir -p ${XIGNCODE_PATCH_ROOT_CDN}${ENVIRONMENT_PREVIOUS}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}; fi"
			ssh CTRL "rsync -av --delete --progress ${XIGNCODE_PATCH_ROOT_CDN}${ENVIRONMENT_LIVE}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/Ruby ${XIGNCODE_PATCH_ROOT_CDN}${ENVIRONMENT_PREVIOUS}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/."

			#### Sync Patch Root Contents Ruby to XignCode CDN LIVE ###
			echo "[REMOTE] Sync Environment (Ruby) => ${XIGNCODE_PATCH_ROOT_CDN}${ENVIRONMENT_LIVE}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/Ruby"
		fi
		;;
	All)
		echo "All"
		XIGNCODE_VERSION_TEST=$(basename ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT_TEST}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/List64/[0-9]*)
		XIGNCODE_VERSION_LIVE=$(basename ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT_LIVE}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/List64/[0-9]*)
		TOPAZ_VERSION_TEST=$(basename ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT_TEST}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/Topaz/[0-9]*)
		TOPAZ_VERSION_LIVE=$(basename ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT_LIVE}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/Topaz/[0-9]*)
		#RUBY_VERSION_TEST=$(basename ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT_TEST}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/Ruby/[0-9]*)
		#RUBY_VERSION_LIVE=$(basename ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT_LIVE}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/Ruby/[0-9]*)
		#if [ "${XIGNCODE_VERSION_TEST}" == "${XIGNCODE_VERSION_LIVE}" -a "${TOPAZ_VERSION_TEST}" == "${TOPAZ_VERSION_LIVE}" -a "${RUBY_VERSION_TEST}" == "${RUBY_VERSION_LIVE}" ]; then
		if [ "${XIGNCODE_VERSION_TEST}" == "${XIGNCODE_VERSION_LIVE}" -a "${TOPAZ_VERSION_TEST}" == "${TOPAZ_VERSION_LIVE}" ]; then
			exec echo "No PatchRoot & Topaz & Ruby file to flush, please check again."
		else
			### Sync XignCode All from TEST to LIVE in TEST Server ###
			echo "[LOCAL] Sync Environment All => ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT_LIVE}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/."

			if [ ! -d "${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT_LIVE}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}" ]; then mkdir -p ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT_LIVE}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}; chmod -R 777 ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT_LIVE}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}; fi

			rsync -av --delete --progress ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT_TEST}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/* ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT_LIVE}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/.

			#### Backup Previous XignCode Version All ###
			ssh CTRL "if [ ! -d \"${XIGNCODE_PATCH_ROOT_CDN}${ENVIRONMENT_PREVIOUS}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}\" ]; then mkdir -p ${XIGNCODE_PATCH_ROOT_CDN}${ENVIRONMENT_PREVIOUS}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}; fi"
			ssh CTRL "rsync -av --delete --progress ${XIGNCODE_PATCH_ROOT_CDN}${ENVIRONMENT_LIVE}/* ${XIGNCODE_PATCH_ROOT_CDN}${ENVIRONMENT_PREVIOUS}/."

			#### Sync Patch Root Contents All to XignCode CDN LIVE ###
			echo "[REMOTE] Sync Environment All => ${XIGNCODE_PATCH_ROOT_CDN}${ENVIRONMENT_LIVE}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/*"
		fi
		;;
	*)
		exec echo "Wrong Update Parameter, please check again."
		;;
esac

ssh ${XIGNCODE_PATCH_ROOT_CDN_MACHINE} "if [ ! -d "${XIGNCODE_PATCH_ROOT_CDN}${ENVIRONMENT_LIVE}" ]; then mkdir -p ${XIGNCODE_PATCH_ROOT_CDN}${ENVIRONMENT_LIVE}; fi"
rsync -av --delete --progress ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT_LIVE}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME} ${XIGNCODE_PATCH_ROOT_CDN_MACHINE}:${XIGNCODE_PATCH_ROOT_CDN}${ENVIRONMENT_LIVE}/.

cd ~/patch/; php flush.php flush_all.txt

### XignCode Update Info to ChatBot ###
output_filename="/tmp/xigncode_live_cdn_monitor.log"
case ${UPDATE_P} in
	PatchRoot)
		XIGNCODE_VERSION_LIVE=$(basename ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT_LIVE}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/List64/[0-9]*)
		echo "XignCode PatchRoot ( Rev. ${XIGNCODE_VERSION_LIVE} ) 已上正式 CDN，可以測試囉!" > $output_filename
		send_messages_with_pic INFO xigncode_live_cdn "XignCode 版本更新"
		filetype="PatchRoot"
		VERSION=${XIGNCODE_VERSION_LIVE}
		;;
	Topaz)
		TOPAZ_VERSION_LIVE=$(basename ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT_LIVE}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/Topaz/[0-9]*)
		echo "XignCode Pattern > Topaz ( Rev. ${TOPAZ_VERSION_LIVE} ) 已上正式 CDN，可以測試囉!" > $output_filename
		send_messages_with_pic INFO xigncode_live_cdn "XignCode Topaz 更新"
		filetype="Topaz"
		VERSION=${TOPAZ_VERSION_LIVE}
		;;
	Ruby)
		RUBY_VERSION_LIVE=$(basename ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT_LIVE}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/Ruby/[0-9]*)
		echo "XignCode Pattern > Ruby ( Rev. ${RUBY_VERSION_LIVE} ) 已上正式 CDN，可以測試囉!" > $output_filename
		send_messages_with_pic INFO xigncode_live_cdn "XignCode Ruby 更新"
		filetype="Ruby"
		VERSION="${RUBY_VERSION_LIVE}"
		;;
	All)
		XIGNCODE_VERSION_LIVE=$(basename ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT_LIVE}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/List64/[0-9]*)
		TOPAZ_VERSION_LIVE=$(basename ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT_LIVE}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/Topaz/[0-9]*)
		#RUBY_VERSION_LIVE=$(basename ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT_LIVE}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/Ruby/[0-9]*)
		echo "XignCode PatchRoot ( Rev. ${XIGNCODE_VERSION_LIVE} )" > $output_filename
		echo "XignCode Pattern > Topaz ( Rev. ${TOPAZ_VERSION_LIVE} )" >> $output_filename
		#echo "XignCode Pattern > Ruby ( Rev. ${RUBY_VERSION_LIVE} )" >> $output_filename
		echo "已上正式 CDN，可以測試囉!" >> $output_filename
		send_messages_with_pic INFO xigncode_live_cdn "XignCode 全更新"
		filetype="Unknown"
		VERSION="Unknown"
		;;
esac

XIGNCODE_ID=$(PGPASSWORD=$dbpass psql -U ${dbuser} ${dbname} -h CTRL -c "COPY (SELECT id FROM ${XIGNCODE_LOG_TABLE_NAME} WHERE type = '${filetype}' AND version = '${VERSION}' AND status = 1 ORDER BY id DESC LIMIT 1) TO STDOUT;")

currentDateTime=$(date +"%Y-%m-%d %H:%M:%S")
PGPASSWORD=$dbpass psql -U ${dbuser} ${dbname} -h CTRL -c "UPDATE ${XIGNCODE_LOG_TABLE_NAME} SET finished_time = '${currentDateTime}', status = 2 WHERE type = '${filetype}' AND status = 1 AND version = '${VERSION}' AND id = '${XIGNCODE_ID}';"
