#!/bin/bash
[ "$1" == "" ] && exec echo "Usage: $0 <XignCode Patch Root 7zip Tarball>"

source $HOME/.gamerc

XIGNCODE_TARBALL=$1
ENVIRONMENT="TEST"

### Uncompress Tarball to TEST Server ###
if [ ! -d "${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT}" ]; then mkdir -p ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT}; fi

cd ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT}; rm -rf ${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}

7za x ${XIGNCODE_PATCH_ROOT_7ZIP_DIRECTORY}${XIGNCODE_TARBALL} -p${XIGNCODE_PASSWORLD} -o${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT}

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

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 ###
XIGNCODE_VERSION=$(basename ${XIGNCODE_PATCH_ROOT_SOURCE}${ENVIRONMENT}/${XIGNCODE_PATCH_ROOT_DIRECTORY_NAME}/List64/[0-9]*)
output_filename="/tmp/xigncode_test_cdn_monitor.log"
echo "XignCode PatchRoot ( Rev. ${XIGNCODE_VERSION} ) 已上測試 CDN，可以測試囉!" > $output_filename
send_messages_with_pic INFO xigncode_test_cdn "XignCode 版本更新"
