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

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

XIGNCODE_TARBALL=$1
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

PGPASSWORD=$dbpass psql -U ${dbuser} ${dbname} -h CTRL -c "CREATE SEQUENCE IF NOT EXISTS ${XIGNCODE_LOG_TABLE_NAME}_id START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1; CREATE TABLE IF NOT EXISTS ${XIGNCODE_LOG_TABLE_NAME} (id integer DEFAULT nextval('public.${XIGNCODE_LOG_TABLE_NAME}_id_seq'::regclass) NOT NULL, name text primary key, type text, version text, md5 text, created_time timestamp with time zone, updated_time timestamp with time zone, finished_time timestamp with time zone, status integer, live_sync integer);"

currentDateTime=$(date +"%Y-%m-%d %H:%M:%S")
fileMD5=$(md5sum ${XIGNCODE_PATCH_ROOT_7ZIP_DIRECTORY}${XIGNCODE_TARBALL} | awk -F' ' '{print $1}')
XIGNCODE_ID=$(PGPASSWORD=$dbpass psql -U ${dbuser} ${dbname} -h CTRL -c "COPY (INSERT INTO ${XIGNCODE_LOG_TABLE_NAME} (name, type, md5, created_time, status, live_sync) VALUES ('${XIGNCODE_TARBALL}', '${filetype}', '${fileMD5}', '${currentDateTime}', 0, 0) RETURNING id) TO STDOUT;")

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}/."

ssh ${XIGNCODE_PATCH_ROOT_CDN_MACHINE} "if [ ! -d "${XIGNCODE_PATCH_ROOT_CDN}${ENVIRONMENT}" ]; then mkdir -p ${XIGNCODE_PATCH_ROOT_CDN}${ENVIRONMENT}; fi"
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;"
