#!/bin/bash

source ~/.gamerc

#PARAMETERS
# Warning deadline when uder this setting.(in Days)
[ -z "${DEADLINE_DAY_NOTICE}" ] && DEADLINE_DAY_NOTICE="30"

# OUTPUT LOGFILE
LOG_OUTPUT_TEST="/tmp/deadline_monitor_test.log"
LOG_OUTPUT_LIVE="/tmp/deadline_monitor_live.log"

# DELETE THE OLD OUTPUT LOG
[ -f "${LOG_OUTPUT_TEST}" ] && rm -rf ${LOG_OUTPUT_TEST}
[ -f "${LOG_OUTPUT_LIVE}" ] && rm -rf ${LOG_OUTPUT_LIVE}

# GAME_TYPE_DISPLAY
[ "${GAME_TYPE}" == "AK" ] && GAME_TYPE_DISPLAY="M7" || GAME_TYPE_DISPLAY=${GAME_TYPE}
[ ! -z "${COUNTRY_CODE_DISPLAY}" ] && ROUTE=$(echo ${GAME_TYPE}-${COUNTRY_CODE_DISPLAY}) || ROUTE=$(echo ${GAME_TYPE}-${COUNTRY_CODE})

# DEADLINE LOG PARSER
if [ "${GAME_TYPE}" == "FF" ] || [ "${GAME_TYPE}" == "AK" ] || [ "${GAME_TYPE}" == "M11" ] || [ "${GAME_TYPE}" == "M12" ] || [ "${GAME_TYPE}" == "G1" ] ; then
	# CHECK LOG EXIST OR NOT
	ls -l $HOME/servers*/MissionServer*/Rank*.log.* > /dev/null 2>&1
	if [ "$?" = "0" ]; then
		DEADLINE="$(xcat $HOME/servers*/MissionServer*/Rank*.log.* | grep ",RankUpdate:" | sort -t , -k 2 | tail -1 \
				| sed 's/^.*: Type=\([0-9]\+\) Group=\([0-9]\+\) Class=\([0-9]\+\),/20\1-\2-\3/g')"
	else
		exit 1
	fi
elif [ "$GAME_TYPE" == "AR" ] ; then
	# CHECK LOG EXIST OR NOT
	ls -l $HOME/servers*/MissionServer*/Family.log.* > /dev/null 2>&1
	if [ "$?" = "0" ]; then
		DEADLINE="$(xcat $HOME/servers*/MissionServer*/Family.log.* | grep "FamilyUpdate," | sort -t , -k 2 | tail -1 \
				| sed 's/^.*, Type=\([0-9]\+\) Group=\([0-9]\+\) Class=\([0-9]\+\)/20\1-\2-\3/g')"
	else
		exit 1
	fi
elif [ "$GAME_TYPE" == "DJ" ] ; then
	# CHECK LOG EXIST OR NOT
	ls -l $HOME/servers*/MissionServer*/Isle.log.* > /dev/null 2>&1
	if [ "$?" = "0" ]; then
		DEADLINE="$(xcat $HOME/servers*/MissionServer*/Isle.log.* | grep ",RankUpdate:" | sort -t , -k 2 | tail -1 \
				| sed 's/^.*:Type=\([0-9]\+\) Group=\([0-9]\+\) Class=\([0-9]\+\),/20\1-\2-\3/g')"
	else
		exit 1
	fi
else
	# CHECK LOG EXIST OR NOT
	ls -l $HOME/servers*/MissionServer*/Test.log.* > /dev/null 2>&1
	if [ "$?" = "0" ]; then
		DEADLINE="$(xcat $HOME/servers*/MissionServer*/Test.log.* | grep "Dead Line" | sort -t , -k 2 | tail -1 \
				| sed 'y/\]\[/::/' | awk -F: '{print $(NF-3)}')"
	else
		exit 1
	fi
fi

# DEADLINE ERROR DATE FIX
DEADLINE_YEAR=$(echo ${DEADLINE} | cut -d - -f1)
DEADLINE_MONTH=$(echo ${DEADLINE} | cut -d - -f2)
DEADLINE_DAY=$(echo ${DEADLINE} | cut -d - -f3)

if [ "${DEADLINE_MONTH}" == "2" ] && ([ "${DEADLINE_DAY}" == "29" ] || [ "${DEADLINE_DAY}" == "30" ] || [ "${DEADLINE_DAY}" == "31" ]); then
	DEADLINE="${DEADLINE_YEAR}-2-28"
elif ([ "${DEADLINE_MONTH}" == "4" ] || [ "${DEADLINE_MONTH}" == "6" ] || [ "${DEADLINE_MONTH}" == "9" ] || [ "${DEADLINE_MONTH}" == "11" ]) && ([ "${DEADLINE_DAY}" == "31" ]); then
	DEADLINE="${DEADLINE_YEAR}-${DEADLINE_MONTH}-30"
fi

# DEADLINE REMAINING CHECK
DEADLINE_DATE="$(date -d ${DEADLINE} +%Y-%m-%d)"
DEADLINE_DAY_REMAINING="$(( ($(date -d ${DEADLINE} +%s)-$(date +%s)) / 86400 ))"
if [ ${DEADLINE_DAY_REMAINING} -lt ${DEADLINE_DAY_NOTICE} ] ; then
	[[ "${HOST_NAME}" == "TEST" ]] && SET_NUM="X" || SET_NUM=$(echo ${HOST_NAME} | awk -F"_" '{ for (i=1;i<=NF;i++) { if ($i ~/GAMEDB/) { printf "%s",$i; exit; } } }' | sed 's/GAMEDB//')
	[[ "${HOST_NAME}" == "TEST" ]] && OUTPUT_FILE=${LOG_OUTPUT_TEST} || OUTPUT_FILE=${LOG_OUTPUT_LIVE}
	echo ${GAME_TYPE_DISPLAY},${COUNTRY_CODE_DISPLAY},${SET_NUM},${DEADLINE},${DEADLINE_DAY_REMAINING}
	echo ${GAME_TYPE_DISPLAY},${COUNTRY_CODE_DISPLAY},${SET_NUM},${DEADLINE},${DEADLINE_DAY_REMAINING} >> ${OUTPUT_FILE}
fi
