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

PORT="17777"
PASS="0KjaM85BjfqjA"
TODAY=$(date +%Y%m%d)
OUTPUT_LOG="/tmp/ban_online_all_charids_n1_live_internal_${TODAY}"

for machine in $(print_hosts GAMEDB); do
    SET_ID=$(echo ${machine}| sed 's/GAMEDB//')
    IP=$(grep ${machine} /etc/hosts | awk -F" " '{print $1}')
    PORT="17777"
    GAMEDB_NAME="${GAME_DB_NAME}${SET_ID}"
    echo "Machine: ${machine}, GameDB: ${GAMEDB_NAME}" | colorize orange blue
    CHAR_IDS=$(ssh -o StrictHostKeyChecking=no ${machine} "psql -U postgres ${GAMEDB_NAME} -qAt -c \"SELECT id FROM player_characters WHERE ((last_login_time > 0 AND logout_time IS NULL) OR (last_login_time > 0 AND logout_time < last_login_time)) AND (id IN (1005000000057,1002000000651,1020000000886,1004000001001,1007000001557));\"")
    if [ -n "${CHAR_IDS}" ]; then
        for char_id in ${CHAR_IDS}; do
            #echo "curl http://127.0.0.1/cgi-bin/CGIAgent.fcgi?$IP,$PORT,$PASS,ban+$char_id+3+n1tw_first_maintenance_internal"
            echo "curl http://127.0.0.1/cgi-bin/CGIAgent.fcgi?$IP,$PORT,$PASS,ban+$char_id+3+n1tw_first_maintenance_internal" | tee -a ${OUTPUT_LOG}
            curl http://127.0.0.1/cgi-bin/CGIAgent.fcgi?$IP,$PORT,$PASS,ban+$char_id+3+n1tw_first_maintenance_internal
            sleep 0.3
        done
    fi
done
