#!/bin/bash

EXEC_USER="$1"
MANAGE_DIR="$2"
USER=$(whoami)

if [ -z "${MANAGE_DIR}" ]; then
    MANAGE_DIR="/home/${USER}/www/tools/files/config_manage"
fi

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} )"

$HOME/bin/send_chatbot_text_only_by_thread_tag "modify_config" "網頁工具操作" "config.ini / config00.ini on 測試機, 更新開始, 執行人員 by ${EXEC_USER_NICK_NAME}" minute 10

CGI_CMD="/home/${USER}/bin/send_fastcgi_test_n1_ret rfs DONE"

[ -f "${MANAGE_DIR}/to_log.txt" ] && rm -rf "${MANAGE_DIR}/to_log.txt"

MD5_CONFIG_ORIG=$(md5sum /home/${USER}/common-TEST/bin/config.ini | awk -F" " '{print $1}')
MD5_CONFIG_WEB=$(md5sum ${MANAGE_DIR}/config.ini | awk -F" " '{print $1}')
MD5_CONFIG00_ORIG=$(md5sum /home/${USER}/servers1/config00.ini | awk -F" " '{print $1}')
MD5_CONFIG00_WEB=$(md5sum ${MANAGE_DIR}/config00.ini | awk -F" " '{print $1}')

if [ "${MD5_CONFIG_ORIG}" == "${MD5_CONFIG_WEB}" -a "${MD5_CONFIG00_ORIG}" == "${MD5_CONFIG00_WEB}" ]; then
    $HOME/bin/send_chatbot_text_only_by_thread_tag "modify_config" "網頁工具操作" "config.ini / config00.ini 沒有變動, 無須更新. 執行人員 by ${EXEC_USER_NICK_NAME}" minute 10
else
    # backup
    TODATE=$(date +"%Y%m%d")
    NOW=$(date +"%Y%m%dT%H%M%S")
    echo "sudo mkdir -p ${MANAGE_DIR}/${TODATE}"
    sudo mkdir -p ${MANAGE_DIR}/${TODATE}
    sudo cp -arf /home/${USER}/common-TEST/bin/config.ini ${MANAGE_DIR}/${TODATE}/config_${NOW}_${EXEC_USER}.ini
    sudo cp -arf /home/${USER}/servers1/config00.ini ${MANAGE_DIR}/${TODATE}/config00_${NOW}_${EXEC_USER}.ini

    # config.ini
    echo "sudo cp -arf ${MANAGE_DIR}/config.ini /home/${USER}/common-TEST/bin/." >> ${MANAGE_DIR}/to_log.txt
    sudo cp -arf ${MANAGE_DIR}/config.ini /home/${USER}/common-TEST/bin/. 2>> ${MANAGE_DIR}/to_log.txt

    # config00.ini
    echo "sudo cp -arf ${MANAGE_DIR}/config00.ini /home/${USER}/servers1/." >> ${MANAGE_DIR}/to_log.txt
    sudo cp -arf ${MANAGE_DIR}/config00.ini /home/${USER}/servers1/.

    DIRECTORY_COUNT=$(ls -hld /home/${USER}/servers*/MissionServer* | sort | awk -F" " '{print $NF}' | wc -l)
    DIRECTORY=$(ls -hld /home/${USER}/servers*/MissionServer* | sort | awk -F" " '{print $NF}' | head -n 1)

    CORRECT_RET="0"
    STATUS=""
    CHECK_STATUS=""
    STATUS=$(/bin/bash -c "${CGI_CMD}")
    for ((i=0; i<${DIRECTORY_COUNT}; i++)); do
        CHECK_STATUS+="${CORRECT_RET} "
    done
    CHECK_STATUS=${CHECK_STATUS% }
    STATUS_MD5=$(echo ${STATUS} | md5sum | awk -F" " '{print $1}')
    CHECK_STATUS_MD5=$(echo ${CHECK_STATUS} | md5sum | awk -F" " '{print $1}')

    if [ "${STATUS_MD5}" == "${CHECK_STATUS_MD5}" ]; then
        cd ${DIRECTORY}

        FILENAME=$(ls -r Test.log.* | head -n 1)
PARAMS_SEARCH_OUTPUT=$(awk -F',' '
/,Mission Server Reload Function Switch ini/ {
    save_group=1
    group=""
}
/,Mission Server Reload Function Switch ini/ || (save_group && /,Config \[/) {
    group = group $0 "\n"
}
END {
    printf "%s", group
}
' ${FILENAME})

        $HOME/bin/send_chatbot_text_only_by_thread_tag "modify_config" "網頁工具操作" "config.ini / config00.ini 更新與 reload CGI ( rfs ) 完成\n${PARAMS_SEARCH_OUTPUT}" minute 10
    else
        $HOME/bin/send_chatbot_text_only_by_thread_tag "modify_config" "網頁工具操作" "config.ini / config00.ini 更新失敗" minute 10
    fi
fi
$HOME/bin/send_chatbot_text_only_by_thread_tag "modify_config" "網頁工具操作" "config.ini / config00.ini on 測試機, 更新結束, 執行人員 by ${EXEC_USER_NICK_NAME}" minute 10
