#!/bin/bash
[ "$1" == "" ] && exec echo "Usage: $0 <PATCH_VERSION>"
source $HOME/.gamerc

PATCH_ROOT_DIRECTORY="$HOME/patch"
PATCH_VERSION_DIRECTORY="$1"
PATCH_DIRECTORY="$HOME/patch/$1"
LOWER_GAME_TYPE="${GAME_TYPE,,}"
CLIENT_CHECK_WARNING_FILE="$HOME/bin/client_check_file_warning_${LOWER_GAME_TYPE}"
CLIENT_CHECK_ERROR_FILE="$HOME/bin/client_check_file_error_${LOWER_GAME_TYPE}"
CLIENT_CHECK_WARNING_OUTPUT="/tmp/client_check_file_warning_monitor.log"
CLIENT_CHECK_ERROR_OUTPUT="/tmp/client_check_file_error_monitor.log"
CLIENT_PATCH_FILE_LIST_NFS="/tmp/client_patch_file_list_nfs"
CLIENT_PATCH_FILE_LIST="/tmp/client_patch_file_list"

if [ "$GAME_TYPE" == "DJ" ]; then
        EXTENSION="TAR"
        CLIENT_PATCH_DIRECTORY=$(basename ${PATCH_DIRECTORY}/*.${EXTENSION} | sed "s/.${EXTENSION}/bz2/")
        CLIENT_PATCH_TARBALL=$(basename ${PATCH_DIRECTORY}/*.${EXTENSION})
elif [ "$GAME_TYPE" == "FF" -o "$GAME_TYPE" == "FN" -o "$GAME_TYPE" == "AR" ]; then
        EXTENSION="tar"
        CLIENT_PATCH_DIRECTORY=$(basename ${PATCH_DIRECTORY}/*.${EXTENSION} | sed "s/.${EXTENSION}//")
        CLIENT_PATCH_TARBALL=$(basename ${PATCH_DIRECTORY}/*.${EXTENSION})
fi

if [ ! -d "${PATCH_DIRECTORY}" ]; then
        exec echo "ERROR: Wrong patch version name for directory, please check it again."
fi
if [ ! -f ${PATCH_DIRECTORY}/${CLIENT_PATCH_TARBALL} ]; then
        exec echo "ERROR: No client patch tarball, please check it again."
fi

SEARCH_STR_WARNING=$(cat $CLIENT_CHECK_WARNING_FILE | tr '\n' '|' | sed 's/|$//' | sed 's/^/| egrep "/;s/$/"/')
SEARCH_STR_ERROR=$(cat $CLIENT_CHECK_ERROR_FILE | tr '\n' '|' | sed 's/|$//' | sed 's/^/| egrep "/;s/$/"/')

[ -f $CLIENT_CHECK_WARNING_OUTPUT ] && rm -rf $CLIENT_CHECK_WARNING_OUTPUT
[ -f $CLIENT_CHECK_ERROR_OUTPUT ] && rm -rf $CLIENT_CHECK_ERROR_OUTPUT

SEARCH_STR_WARNING_CMD=$(echo "tar tvf ${PATCH_DIRECTORY}/${CLIENT_PATCH_TARBALL} $SEARCH_STR_WARNING")
SEARCH_STR_ERROR_CMD=$(echo "tar tvf ${PATCH_DIRECTORY}/${CLIENT_PATCH_TARBALL} $SEARCH_STR_ERROR")

if [ "$GAME_TYPE" == "DJ" -o "$GAME_TYPE" == "FF" ]; then
	eval "$SEARCH_STR_WARNING_CMD" | tee $CLIENT_CHECK_WARNING_OUTPUT
	eval "$SEARCH_STR_ERROR_CMD" | tee $CLIENT_CHECK_ERROR_OUTPUT
elif [ "$GAME_TYPE" == "FN" -o "$GAME_TYPE" == "AR" ]; then
	[ -f $CLIENT_PATCH_FILE_LIST_NFS ] && rm -rf $CLIENT_PATCH_FILE_LIST_NFS
	[ -f $CLIENT_PATCH_FILE_LIST ] && rm -rf $CLIENT_PATCH_FILE_LIST
	awk -F"," '{ print $1","$2","$3$9 }' ${PATCH_DIRECTORY}/FullFileList* | sed '1d' > $HOME/bin/FullFileList_${GAME_TYPE}${COUNTRY_CODE_DISPLAY}.txt
	tar tvf ${PATCH_DIRECTORY}/${CLIENT_PATCH_TARBALL} | grep nfs | awk -F" " '{print $NF}' > $CLIENT_PATCH_FILE_LIST_NFS
while IFS= read -r line; do 
	cat $HOME/bin/FullFileList_${GAME_TYPE}${COUNTRY_CODE_DISPLAY}.txt | egrep $(basename "${line}") | awk -F"," '{print $1}' >> $CLIENT_PATCH_FILE_LIST
done < $CLIENT_PATCH_FILE_LIST_NFS	
	SEARCH_STR_WARNING_CMD=$(echo "cat $CLIENT_PATCH_FILE_LIST $SEARCH_STR_WARNING")
	eval "$SEARCH_STR_WARNING_CMD" | tee $CLIENT_CHECK_WARNING_OUTPUT

	SEARCH_STR_ERROR_CMD=$(echo "cat $CLIENT_PATCH_FILE_LIST $SEARCH_STR_ERROR")
	eval "$SEARCH_STR_ERROR_CMD" | tee $CLIENT_CHECK_ERROR_OUTPUT
fi

LOG_SIZE=$(stat --printf="%s" ${CLIENT_CHECK_WARNING_OUTPUT})
if [ "${LOG_SIZE}" -gt 0 ]; then
        #echo "send_messages WARNING client_check_file_warning \"Client Patch $PATCH_VERSION_DIRECTORY 檔案變更請確認\""
        send_messages WARNING client_check_file_warning "Client Patch $PATCH_VERSION_DIRECTORY 檔案變更請確認"
fi
LOG_SIZE=$(stat --printf="%s" ${CLIENT_CHECK_ERROR_OUTPUT})
if [ "${LOG_SIZE}" -gt 0 ]; then
        #echo "send_messages ERROR client_check_file_error \"Client Patch $PATCH_VERSION_DIRECTORY 檔案不該被包進版本\""
        send_messages ERROR client_check_file_error "Client Patch $PATCH_VERSION_DIRECTORY 檔案不該被包進版本"
fi
