#!/bin/bash
[ "$1" == "" ] && exec echo "Usage: $0 <TYPE: 1 => LIVE, 2 => TEST>"
source $HOME/.gamerc

SCRIPT_NAME=$(basename "$0")
LOCK_FILE="/tmp/${SCRIPT_NAME}.lock"

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

TYPE=$1

# LIVE
if [ "${TYPE}" == "1" ]; then
    ENVIRONMENT="正式機"
    ATKDEF_CHEAT_COUNT=30
    OUTPUT="/tmp/atkdef_auto_cheat_live_monitor.log"
    [[ -f "${OUTPUT}" ]] && rm -rf ${OUTPUT}

    while IFS=',' read -r set_id world_id tag_name game_db_name game_db_ip db_account db_pass ms_fcgi_ip ms_fcgi_port ms_fcgi_pass; do
        echo "set_id=$set_id world_id=$world_id tag_name=$tag_name game_db_name=$game_db_name game_db_ip=$game_db_ip db_account=$db_account db_pass=$db_pass ms_fcgi_ip=$ms_fcgi_ip ms_fcgi_port=$ms_fcgi_port ms_fcgi_pass=$ms_fcgi_pass"

        SET_ID=$(echo ${set_id})
        MACHINE="GAMEDB${SET_ID}"
        DBNAME="${GAME_DB_NAME}${SET_ID}"
        echo "${SET_ID},${MACHINE},${DBNAME}"
        CHAR_ID_LISTS=$(ssh ${MACHINE} "psql -U postgres ${DBNAME} -t -A -F',' -c \"SELECT pc.world_id, pc.id FROM player_cheat pch JOIN player_characters pc ON pc.id = pch.char_id WHERE pch.atkdef_cheat_count >= ${ATKDEF_CHEAT_COUNT} AND (pc.flag & 64) = 0 AND NOT EXISTS (SELECT 1 FROM player_count pct WHERE pct.id = pch.char_id AND type = 54);\"" < /dev/null)
        echo "${CHAR_ID_LISTS}"
        if [ ! -z "${CHAR_ID_LISTS}" ]; then
            while IFS=',' read -r world_id_real char_id; do
                echo "world_id=$world_id_real, id=$char_id"
                echo "curl http://127.0.0.1/cgi-bin/CGIAgent.fcgi?$ms_fcgi_ip,$ms_fcgi_port,$ms_fcgi_pass,cheat_char+$char_id"
                curl http://127.0.0.1/cgi-bin/CGIAgent.fcgi?$ms_fcgi_ip,$ms_fcgi_port,$ms_fcgi_pass,cheat_char+$char_id
                echo "curl http://127.0.0.1/cgi-bin/CGIAgent.fcgi?$ms_fcgi_ip,$ms_fcgi_port,$ms_fcgi_pass,ban+$char_id+9999999+atkdef_cc_autoban"
                curl http://127.0.0.1/cgi-bin/CGIAgent.fcgi?$ms_fcgi_ip,$ms_fcgi_port,$ms_fcgi_pass,ban+$char_id+9999999+atkdef_cc_autoban
                echo "$set_id,$world_id_real,$char_id" >> ${OUTPUT}
            done <<< "${CHAR_ID_LISTS}"
        fi

    done < <(psql -U postgres -d WebTool -t -A -c "COPY (SELECT DISTINCT ON (set_id, game_db_name) set_id, world_id, tag_name, game_db_name, game_db_ip, db_account, db_pass, ms_fcgi_ip, ms_fcgi_port, ms_fcgi_pass FROM db_setup WHERE is_commercial = 1 AND is_public = 1 AND world_id NOT IN (SELECT id FROM fake_worlds) ORDER BY set_id, game_db_name, world_id DESC) TO STDOUT WITH CSV;")

    if [ -f "${OUTPUT}" ]; then
        LOG_SIZE=$(stat --printf="%s" ${OUTPUT})
        if [ "${LOG_SIZE}" -gt 0 ]; then
            #send_messages AUTOCHEAT atkdef_auto_cheat_live "${ENVIRONMENT} ATKDEF AutoCheat \/ AutoBan Cheat Count >= ${ATKDEF_CHEAT_COUNT} ( Log 打點 only )"
            send_messages AUTOCHEAT atkdef_auto_cheat_live "${ENVIRONMENT} ATKDEF AutoCheat \/ AutoBan Cheat Count >= ${ATKDEF_CHEAT_COUNT}"
        fi
    fi

# TEST
elif [ "${TYPE}" == "2" ]; then
    ENVIRONMENT="測試環境"
    ATKDEF_CHEAT_COUNT=30
    OUTPUT="/tmp/atkdef_auto_cheat_test_monitor.log"
    [[ -f "${OUTPUT}" ]] && rm -rf ${OUTPUT}

    while IFS=',' read -r set_id world_id tag_name game_db_name game_db_ip db_account db_pass ms_fcgi_ip ms_fcgi_port ms_fcgi_pass; do
        echo "set_id=$set_id world_id=$world_id tag_name=$tag_name game_db_name=$game_db_name game_db_ip=$game_db_ip db_account=$db_account db_pass=$db_pass ms_fcgi_ip=$ms_fcgi_ip ms_fcgi_port=$ms_fcgi_port ms_fcgi_pass=$ms_fcgi_pass"

        SET_ID=$((world_id / 100))
        MACHINE_PRE="${tag_name#\[}"
        MACHINE="${MACHINE_PRE%%]*}"
        DBNAME="${GAME_DB_NAME}${SET_ID}"
        echo "${SET_ID},${MACHINE},${DBNAME}"
        CHAR_ID_LISTS=$(ssh ${MACHINE} "psql -U postgres ${DBNAME} -t -A -F',' -c \"SELECT pc.world_id, pc.id FROM player_cheat pch JOIN player_characters pc ON pc.id = pch.char_id WHERE pch.atkdef_cheat_count >= ${ATKDEF_CHEAT_COUNT} AND (pc.flag & 64) = 0 AND NOT EXISTS (SELECT 1 FROM player_count pct WHERE pct.id = pch.char_id AND type = 54);\"" < /dev/null)
        echo "${CHAR_ID_LISTS}"
        if [ ! -z "${CHAR_ID_LISTS}" ]; then
            while IFS=',' read -r world_id_real char_id; do
                echo "world_id=$world_id_real, id=$char_id"
                echo "curl http://127.0.0.1/cgi-bin/CGIAgent.fcgi?$ms_fcgi_ip,$ms_fcgi_port,$ms_fcgi_pass,cheat_char+$char_id"
                curl http://127.0.0.1/cgi-bin/CGIAgent.fcgi?$ms_fcgi_ip,$ms_fcgi_port,$ms_fcgi_pass,cheat_char+$char_id
                echo "curl http://127.0.0.1/cgi-bin/CGIAgent.fcgi?$ms_fcgi_ip,$ms_fcgi_port,$ms_fcgi_pass,ban+$char_id+9999999+atkdef_cc_autoban"
                curl http://127.0.0.1/cgi-bin/CGIAgent.fcgi?$ms_fcgi_ip,$ms_fcgi_port,$ms_fcgi_pass,ban+$char_id+9999999+atkdef_cc_autoban
                echo "$set_id,$world_id_real,$char_id" >> ${OUTPUT}
            done <<< "${CHAR_ID_LISTS}"
        fi

    done < <(psql -U postgres -d WebTool -t -A -c "COPY (SELECT DISTINCT ON (set_id, game_db_name) set_id, world_id, tag_name, game_db_name, game_db_ip, db_account, db_pass, ms_fcgi_ip, ms_fcgi_port, ms_fcgi_pass FROM db_setup WHERE is_commercial = 0 AND is_public = 0 AND set_id NOT IN (985,986,987) ORDER BY set_id, game_db_name, world_id DESC) TO STDOUT WITH CSV;")

    if [ -f "${OUTPUT}" ]; then
        LOG_SIZE=$(stat --printf="%s" ${OUTPUT})
        if [ "${LOG_SIZE}" -gt 0 ]; then
            send_messages AUTOCHEAT atkdef_auto_cheat_test "${ENVIRONMENT} ATKDEF AutoCheat \/ AutoBan Cheat Count >= ${ATKDEF_CHEAT_COUNT}"
        fi
    fi
fi

