#!/bin/bash
# 如果 gamerc 存在, 則載入它
[ -f ~/.gamerc ] && source ~/.gamerc

# 如果是非 Ctrl 架構請自行設定遊戲名稱跟國家名稱
[ -z "${GAME_TYPE}" ] && GAME_TYPE=""
[ -z "${COUNTRY_CODE}" ] && COUNTRY_CODE=""

# 如果有需要自訂或是修改顯示的遊戲與國家名稱, 請自行修正
[ "${GAME_TYPE}" == "AK" ] && GAME_TYPE_DISPLAY="M7" || GAME_TYPE_DISPLAY=${GAME_TYPE}
GAME_TYPE="M12"
COUNTRY_CODE_DISPLAY="TW"
LOCAL_PATCH_DIR="20190903_2"

MESSAGES_OUTPUT="/tmp/check_server_debug_version_messages.log"

# 舊的發送訊息 LOG 檔案如果存在, 則進行刪除
[ -f "${MESSAGES_OUTPUT}" ] && rm -rf ${MESSAGES_OUTPUT}

# API ROUTE 設定, 如果需要可自行修改
#ROUTE=${GAME_TYPE}-${COUNTRY_CODE}
ROUTE="INTERNAL-TEST"

# 發送當下時間
DATETIME=$(date +"%Y-%m-%d %T %Z")

# 發送訊息
#MESSAGE=$(printf "[TEST MESSAGES]\n${DATETIME}\nHello Guys! (${GAME_TYPE_DISPLAY}-${COUNTRY_CODE_DISPLAY})")

GAME_TYPE_LOWER=$(echo ${GAME_TYPE} | tr '[:upper:]' '[:lower:]')
for server in $(ls ${GAME_TYPE_LOWER}*Server); do 
	ldd_check=$(ldd ${server} | grep libasan) 

	if [ ! -z "${ldd_check}" ]; then 
		echo "${server} is the Debug version" >> ${MESSAGES_OUTPUT}
	fi
done

sed -i "1s/^/\n[WARNING: Server Binary CHECK during Patching]\n${DATETIME}\n(${GAME_TYPE_DISPLAY}-${COUNTRY_CODE_DISPLAY})\n\nVersion: ${LOCAL_PATCH_DIR}\n/" ${MESSAGES_OUTPUT}
MESSAGE=$(cat ${MESSAGES_OUTPUT}) 
echo ${MESSAGE}

# 送出資訊給 API
curl -sS -X POST --data-urlencod "message=${MESSAGE}" "https://web-dev.x-legend.com/api/${ROUTE}/"
