#!/bin/bash
[ "$2" == "" ] && exec echo "Usage: $0 <action> <execute by who>"

source ~/.gamerc

SCRIPT_NAME=$(basename "$0")
LOCK_FILE="${HOME}/www/tools/files/.${SCRIPT_NAME}.lock"

exec 200>"${LOCK_FILE}"
flock -n 200 || {
        echo "${SCRIPT_NAME} is running. Exiting."
    exit 1
}

MAIN_COMMAND="Ctrl time1"
ACTION=$1
EXEC_USER=$2
PATCH_VERSION=$(ls -hld ~/patch/20* | awk -F" " '{print $(NF)}' | awk -F"/" '{print $(NF)}' | tail -n 1)

DATE=$(date +"%Y%m%d")
LOG_DIR="${HOME}/www/tools/files/time1_sync_log/${DATE}"
[ -d "${LOG_DIR}" ] || { mkdir -p "${LOG_DIR}"; chmod 777 -R "${LOG_DIR}"; }

NOW=$(date +"%Y%m%dT%H%M%S")
LOG_FILE="${PATCH_VERSION}_${ACTION}_${NOW}_${EXEC_USER}.log"
HTML_OUTPUT_FILE=$(echo ${LOG_FILE} | sed 's/log/html/')

NICK_NAME=$(ssh TEST "psql -U postgres Tools -c \"COPY (SELECT note FROM userinfo WHERE email = '${EXEC_USER}'||'@x-legend.com.tw') TO STDOUT;\"")
EXEC_USER_NICK_NAME="${EXEC_USER} ( ${NICK_NAME} )"

case ${ACTION} in
    status)
        NOW_DATETIME=$(date +"%Y-%m-%d %H:%M:%S")
        CMD="echo \"NOW DATETIME: ${NOW_DATETIME}+08\";
            echo yes | ${MAIN_COMMAND}_status;
        ";;
    stop)
        CMD="echo yes | ${MAIN_COMMAND}_stop;
            ~/bin/send_chatbot_text_only_by_thread_tag \"time1_sync\" \"網頁工具操作\" \"TIME1 操作, Game Server 關閉, 執行人員 by ${EXEC_USER_NICK_NAME}\" minute 10;
        ";;
    start)
        CMD="echo yes | ${MAIN_COMMAND}_start;
            ~/bin/send_chatbot_text_only_by_thread_tag \"time1_sync\" \"網頁工具操作\" \"TIME1 操作, Game Server 啟動, 執行人員 by ${EXEC_USER_NICK_NAME}\" minute 10;
        ";;
    restart)
        CMD="echo yes | ${MAIN_COMMAND}_restart;
            ~/bin/send_chatbot_text_only_by_thread_tag \"time1_sync\" \"網頁工具操作\" \"TIME1 操作, Game Server 重新啟動, 執行人員 by ${EXEC_USER_NICK_NAME}\" minute 10;
        ";;
    dbbackup)
        CMD="echo yes | ${MAIN_COMMAND}_dbbackup;
            ~/bin/send_chatbot_text_only_by_thread_tag \"time1_sync\" \"網頁工具操作\" \"TIME1 操作, 資料庫備分, 執行人員 by ${EXEC_USER_NICK_NAME}\" minute 10;
        ";;
    sync)
        CMD="echo yes | ${MAIN_COMMAND}_sync;
            cd $HOME/servers1/; rsync -avz config00.ini TIME1:~/servers1/.;
            ~/bin/send_chatbot_text_only_by_thread_tag \"time1_sync\" \"網頁工具操作\" \"TIME1 操作, ${PATCH_VERSION} 版本同步 ( Server + Client ), 執行人員 by ${EXEC_USER_NICK_NAME}\" minute 10;
        ";;
    dbpatch)
        LIVE_GAMEDB_NAME=$(ssh TIME1 "grep GameDB= ~/servers20/setup.ini | awk -F'=' '{print \$2}'")
        LIVE_WORLDDB_NAME=$(ssh TIME1 "grep WorldDBName= ~/servers20/WorldServer2001/setup.ini | awk -F'=' '{print \$2}'")
        CMD="
            echo yes | ${MAIN_COMMAND}_dbpatch;
            ssh TIME1 \"echo ${LIVE_GAMEDB_NAME}; cd ~/common-TEST/db/; perl db_game_alter :${LIVE_GAMEDB_NAME}; echo ${LIVE_WORLDDB_NAME}; perl db_world_alter :${LIVE_WORLDDB_NAME};\";
            ~/bin/send_chatbot_text_only_by_thread_tag \"time1_sync\" \"網頁工具操作\" \"TIME1 操作, 資料庫 Schema 更新, 執行人員 by ${EXEC_USER_NICK_NAME}\" minute 10;
        ";;
    itemmall)
        CMD="echo yes | ${MAIN_COMMAND}_itemmall;
            ~/bin/send_chatbot_text_only_by_thread_tag \"time1_sync\" \"網頁工具操作\" \"TIME1 操作, 資料表更新, 執行人員 by ${EXEC_USER_NICK_NAME}\" minute 10;
        ";;
    sync_translate)
        CMD="echo yes | ${MAIN_COMMAND}_sync_translate;
            ~/bin/send_chatbot_text_only_by_thread_tag \"time1_sync\" \"網頁工具操作\" \"TIME1 操作, 翻譯更新 ( Client Translate ), 執行人員 by ${EXEC_USER_NICK_NAME}\" minute 10;
        ";;
    one-stop_services)
        #LIVE_GAMEDB_NAME=$(ssh TIME1 "grep GameDB= ~/servers20/setup.ini | awk -F'=' '{print \$2}'")
        #LIVE_WORLDDB_NAME=$(ssh TIME1 "grep WorldDBName= ~/servers20/WorldServer2001/setup.ini | awk -F'=' '{print \$2}'")
        CMD="
            ~/bin/send_chatbot_text_only_by_thread_tag \"time1_sync\" \"網頁工具操作\" \"TIME1 操作, ${PATCH_VERSION} 版本更新, 全部一條龍開始, 執行人員 by ${EXEC_USER_NICK_NAME}\" minute 10;
            echo yes | ${MAIN_COMMAND}_stop;
            ~/bin/send_chatbot_text_only_by_thread_tag \"time1_sync\" \"網頁工具操作\" \"TIME1 操作, Game Server 關閉, 執行人員 by ${EXEC_USER_NICK_NAME}\" minute 10;
            echo yes | ${MAIN_COMMAND}_dbbackup;
            ~/bin/send_chatbot_text_only_by_thread_tag \"time1_sync\" \"網頁工具操作\" \"TIME1 操作, 資料庫備分, 執行人員 by ${EXEC_USER_NICK_NAME}\" minute 10;
            echo yes | ${MAIN_COMMAND}_sync;
            ~/bin/send_chatbot_text_only_by_thread_tag \"time1_sync\" \"網頁工具操作\" \"TIME1 操作, ${PATCH_VERSION} 版本同步 ( Server + Client ), 執行人員 by ${EXEC_USER_NICK_NAME}\" minute 10;
            cd $HOME/servers1/; rsync -avz config00.ini TIME1:~/servers1/.;
            echo yes | ${MAIN_COMMAND}_dbpatch;
            ssh TIME1 \"$HOME/bin/do_dbpatch_n1_for_current_machine GAMEDB;\";
            ssh TIME1 \"$HOME/bin/do_dbpatch_n1_for_current_machine WORLDDB;\";
            ~/bin/send_chatbot_text_only_by_thread_tag \"time1_sync\" \"網頁工具操作\" \"TIME1 操作, 資料庫 Schema 更新, 執行人員 by ${EXEC_USER_NICK_NAME}\" minute 10;
            echo yes | ${MAIN_COMMAND}_itemmall;
            ~/bin/send_chatbot_text_only_by_thread_tag \"time1_sync\" \"網頁工具操作\" \"TIME1 操作, 資料表更新, 執行人員 by ${EXEC_USER_NICK_NAME}\" minute 10;
            echo yes | ${MAIN_COMMAND}_start;
            ~/bin/send_chatbot_text_only_by_thread_tag \"time1_sync\" \"網頁工具操作\" \"TIME1 操作, Game Server 啟動, 執行人員 by ${EXEC_USER_NICK_NAME}\" minute 10;
            ~/bin/send_chatbot_text_only_by_thread_tag \"time1_sync\" \"網頁工具操作\" \"TIME1 操作, ${PATCH_VERSION} 版本更新, 全部一條龍結束, 可以登入驗證了! 執行人員 by ${EXEC_USER_NICK_NAME}\" minute 10;
        ";;
    *)
        echo "Unknown command for sync";;
esac

if [ "${ACTION}" != "status" ]; then
    $HOME/bin/send_chatbot_text_only_by_thread_tag "time1_sync" "網頁工具操作" "TIME1 操作, 版本 ${PATCH_VERSION}, 同步開始, 執行人員 by ${EXEC_USER_NICK_NAME}" minute 10
fi

#echo ${CMD}
bash -c "${CMD}" >> ${LOG_DIR}/${LOG_FILE}
cat ${LOG_DIR}/${LOG_FILE} | aha | tee -a ${LOG_DIR}/${HTML_OUTPUT_FILE}

if [ "${ACTION}" != "status" ]; then
    $HOME/bin/send_chatbot_text_only_by_thread_tag "time1_sync" "網頁工具操作" "TIME1 操作, 版本 ${PATCH_VERSION}, 同步結束, 執行人員 by ${EXEC_USER_NICK_NAME}" minute 10
fi

[ -f "${LOCK_FILE}" ] && rm -rf ${LOCK_FILE}
