#!/bin/bash
[ "$2" == "" ] && exec echo "Usage: $0 <XignCode (PatchRoot 7zip / Topaz zip / Ruby zip) Tarball> <XignCode ID>"

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

XIGNCODE_TARBALL=$1
XIGNCODE_ID=$2
ENVIRONMENT="TEST"

### Check Tarball Exist or not ###
[ ! -f "${XIGNCODE_PATCH_ROOT_7ZIP_DIRECTORY}${XIGNCODE_TARBALL}" ] && exec echo "Tarball is not exist, please check again."

### Check XignCode TEST Directory Exist or not ###
if [ ! -d "${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT}" ]; then mkdir -p ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT}; fi

### Classify Tarball & Uncompress Tarball to TEST Server ###
cd ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}

case "${XIGNCODE_TARBALL}" in
	*PatchRoot*)
		rm -rf List List64
		7za x ${XIGNCODE_PATCH_ROOT_7ZIP_DIRECTORY}${XIGNCODE_TARBALL} -p${XIGNCODE_PASSWORLD} -o${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT}
		filetype="PatchRoot"
		;;
	*Topaz*)
		rm -rf Topaz
		7za x ${XIGNCODE_PATCH_ROOT_7ZIP_DIRECTORY}${XIGNCODE_TARBALL} -o${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT}
		filetype="Topaz"
		;;
	*Ruby*)
		rm -rf Ruby
		7za x ${XIGNCODE_PATCH_ROOT_7ZIP_DIRECTORY}${XIGNCODE_TARBALL} -o${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT}
		filetype="Ruby"
		;;
	*)
		filetype="Unknown"
		exec echo "Wrong Tarball filename, please check again."
		;;
esac

chmod -R 777 ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}

### Sync Patch Root Contents to XignCode CDN TEST ###
echo "[REMOTE] Sync Environment => ${XIGNCODE_PATCH_ROOT_CDN}${ENVIRONMENT}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/."

rsync -av --delete --progress ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME} ${XIGNCODE_PATCH_ROOT_CDN_MACHINE}:${XIGNCODE_PATCH_ROOT_CDN}${ENVIRONMENT}/.

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

### XignCode Update Info to ChatBot ###
output_filename="/tmp/xigncode_test_cdn_monitor.log"
case "${XIGNCODE_TARBALL}" in
	*PatchRoot*)
		XIGNCODE_VERSION=$(basename ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/List64/[0-9]*)
		echo "XignCode PatchRoot ( Rev. ${XIGNCODE_VERSION} ) 已上測試 CDN，可以測試囉!" > $output_filename
		send_messages_with_pic INFO xigncode_test_cdn "XignCode 版本更新"
		VERSION=${XIGNCODE_VERSION}
		;;
	*Topaz*)
		TOPAZ_VERSION=$(basename ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/Topaz/[0-9]*)
		echo "XignCode Pattern > Topaz ( Rev. ${TOPAZ_VERSION} ) 已上測試 CDN，可以測試囉!" > $output_filename
		send_messages_with_pic INFO xigncode_test_cdn "XignCode Topaz 更新"
		VERSION=${TOPAZ_VERSION}
		;;
	*Ruby*)
		RUBY_VERSION=$(basename ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/Ruby/[0-9]*)
		echo "XignCode Pattern > Ruby ( Rev. ${RUBY_VERSION} ) 已上測試 CDN，可以測試囉!" > $output_filename
		send_messages_with_pic INFO xigncode_test_cdn "XignCode Ruby 更新"
		VERSION=${RUBY_VERSION}
		;;
esac

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