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

PORT="17777"
PASS="0KjaM85BjfqjA"
TODAY=$(date +%Y%m%d)
OUTPUT_LOG="/tmp/ban_online_all_charids_n1_live_${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);\"")
    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+10+n1tw_first_maintenance" | tee -a ${OUTPUT_LOG}
            curl http://127.0.0.1/cgi-bin/CGIAgent.fcgi?$IP,$PORT,$PASS,ban+$char_id+10+n1tw_first_maintenance
            sleep 0.3
        done
    fi
done
