#!/bin/bash
source $HOME/.gamerc

#LOGIN_USER=$(who -m|awk '{print $1}')

# 確認執行腳本來源
if [ "$EXEC_SOURCE" = "web" ]; then
    DIR_NAME="$HOME/www/tools/files/maintenance_action_log"
else
    DIR_NAME="/tmp"
fi

# 指定輸出的 LOG 檔
ORIGINAL_GENERATE_LOG_OUTPUT="/tmp/check_live_iptables.log"
LOG_OUTPUT="${DIR_NAME}/check_live_iptables.log"
LOG_OUTPUT_CHECK="${DIR_NAME}/check_live_iptables_check.log"
MESSAGES_OUTPUT="${DIR_NAME}/check_live_messages.log"

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

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

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

# API ROUTE 設定, 如果需要可自行修改
[ ! -z "${COUNTRY_CODE_DISPLAY}" ] && ROUTE=$(echo ${GAME_TYPE}-${COUNTRY_CODE_DISPLAY}) || ROUTE=$(echo ${GAME_TYPE}-${COUNTRY_CODE})

# 舊的原始 LOG 檔案如果存在, 則進行備份後刪除
[ -f "${ORIGINAL_GENERATE_LOG_OUTPUT}" ] && rm -rf ${ORIGINAL_GENERATE_LOG_OUTPUT}
[ -f "${LOG_OUTPUT}" ] && rm -rf ${LOG_OUTPUT}
[ -f "${LOG_OUTPUT_CHECK}" ] && rm -rf ${LOG_OUTPUT_CHECK}

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

# 檢查正式機防火牆
ssh ACCOUNTDB "$HOME/bin/check_live_iptables > ${ORIGINAL_GENERATE_LOG_OUTPUT}"
#if [ "$EXEC_SOURCE" = "web" ]; then
#    ssh ACCOUNTDB "rsync -avz ${LOG_OUTPUT} TEST:~/www/tools/files/."
#else
#    ssh ACCOUNTDB "rsync -avz ${LOG_OUTPUT} TEST:/tmp/."
#fi
ssh ACCOUNTDB "rsync -avz ${ORIGINAL_GENERATE_LOG_OUTPUT} TEST:${LOG_OUTPUT}"
#[ -f "debug_web_shell.log" ] && rm -rf "debug_web_shell.log"
#echo "[DEBUG] $(date)" >> debug_web_shell.log
#whoami >> debug_web_shell.log

#if [[ "$*" =~ "--source=web" ]]; then
#  echo "Called from web" >> debug_web_shell.log
#else
#  echo "Called from terminal" >> debug_web_shell.log
#fi

cat ${LOG_OUTPUT} | egrep -v 'Chain|target' | sed '/^$/d' > ${LOG_OUTPUT_CHECK}

# 如果輸出的 LOG 檔案有內容, 則發送訊息到 API, 請機器人送群組
LOG_SIZE=$(stat --printf="%s" ${LOG_OUTPUT_CHECK})

# 發送當下時間
DATETIME=$(date +"%Y-%m-%d %T %Z")
#GOOGLE_CHAT_WEBHOOK_URL="https://chat.googleapis.com/v1/spaces/AAAA7hXAOCM/messages?key=AIzaSyDdI0hCZtE6vySjMm-WEfRq3CPzqKqqsHI&token=YupvEeDhNq2aF9ldCAf8TeJw8kmafVAXpj3xJWIzRGw%3D"

if [ "${LOG_SIZE}" -gt 0 ]; then
	# 發送訊息
	cat ${LOG_OUTPUT} > ${MESSAGES_OUTPUT}
	sed -i "1s/^/[防火牆確認]\n[WARNING]\n${DATETIME}\n${GAME_TYPE_DISPLAY}-${COUNTRY_CODE_DISPLAY} => 有限制IP 阻擋\n/" ${MESSAGES_OUTPUT}
	MESSAGE=$(cat ${MESSAGES_OUTPUT})

	# 送出資訊給 Google Chat Webhook
	#curl -sS -X POST -H 'Content-Type: application/json' $GOOGLE_CHAT_WEBHOOK_URL -d "{\"text\": \"${MESSAGE}\"}"
    #[ ! -z "${LOGIN_USER}" ] && $HOME/bin/send_chatbot_text_only_by_thread_tag "lg_block" "系統腳本" "${MESSAGE}" minute 10 || $HOME/bin/send_chatbot_text_only_by_thread_tag "lg_block" "網頁工具操作" "${MESSAGE}" minute 30
    $HOME/bin/automatic_send_chatbot_text_only_by_thread_tag "lg_block" "${MESSAGE}" minute 10
else
	# 發送訊息
	MESSAGE=$(printf "[防火牆確認]\n${DATETIME}\n${GAME_TYPE_DISPLAY}-${COUNTRY_CODE_DISPLAY} => 狀態為對外\n無任何阻擋")

	# 送出資訊給 Google Chat Webhook
	#curl -sS -X POST -H 'Content-Type: application/json' $GOOGLE_CHAT_WEBHOOK_URL -d "{\"text\": \"${MESSAGE}\"}"
    #[ ! -z "${LOGIN_USER}" ] && $HOME/bin/send_chatbot_text_only_by_thread_tag "lg_allow" "系統腳本" "${MESSAGE}" minute 10 || $HOME/bin/send_chatbot_text_only_by_thread_tag "lg_allow" "網頁工具操作" "${MESSAGE}" minute 10
    $HOME/bin/automatic_send_chatbot_text_only_by_thread_tag "lg_allow" "${MESSAGE}" minute 10
fi
