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

[ "$4" == "" ] && exec echo "Usage: $0 <TIMEZONE> <SET_ID> <WS_ID_START> <WS_ID_END>"

TIMEZONE=$1
SET_ID=$2
WS_ID_START=$3
WS_ID_END=$4

DB_ACCOUNT=$PGUSER
DB_PASS=$LIVE_SERVER_DB_PASSWORD

SET_ID_START=$(( WS_ID_START / 100 ))
SET_ID_END=$(( WS_ID_END / 100 ))

if [ "${SET_ID_START}" -ne "${SET_ID}" ]; then
    exec echo "WS_ID_START ${WS_ID_START} is not equal to SET_ID ${SET_ID}"
fi

if [ "${SET_ID_END}" -ne "${SET_ID}" ]; then
    exec echo "WS_ID_START ${WS_ID_END} is not equal to SET_ID ${SET_ID}"
fi

GAMEDB_INT_IP=$(HOST="GAMEDB${SET_ID}"; awk -v h="${HOST}" '$0 ~ h {print $1}' /etc/hosts)
ACCOUNTDB_INT_IP=$(HOST="ACCOUNTDB"; awk -v h="${HOST}" '$0 ~ h {print $1}' /etc/hosts)
SOCIETYDB_INT_IP=$(HOST="SOCIETYDB${SET_ID}"; awk -v h="${HOST}" '$0 ~ h {print $1}' /etc/hosts)

for ws_id in $(seq ${WS_ID_START} ${WS_ID_END}); do

#ONES=$(( ws_id % 100 ))
#if [ "${ONES}" -eq 1 ]; then
#    BLOCK_PREVIOUS_WORLD=0
#else
#    BLOCK_PREVIOUS_WORLD=1    
#fi
BLOCK_PREVIOUS_WORLD=1

WORLDDB_INT_IP=$(HOST="WORLDDB${ws_id}"; awk -v h="${HOST}" '$0 ~ h {print $1}' /etc/hosts)
MS_CGI_IP=$(KEYWORD="CGIIP"; ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null MS${SET_ID} "awk -F\"=\" -v k=\"${KEYWORD}\" '\$0 ~ k {print \$2}' $HOME/servers${SET_ID}/MissionServer${SET_ID}/setup.ini")
MS_CGI_Port=$(KEYWORD="CGIPort"; ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null MS${SET_ID} "awk -F\"=\" -v k=\"${KEYWORD}\" '\$0 ~ k {print \$2}' $HOME/servers${SET_ID}/MissionServer${SET_ID}/setup.ini")
MS_CGI_Pass=$(KEYWORD="CGIKey"; ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null MS${SET_ID} "awk -F\"=\" -v k=\"${KEYWORD}\" '\$0 ~ k {print \$2}' $HOME/servers${SET_ID}/MissionServer${SET_ID}/setup.ini")
SS_CGI_IP=$(KEYWORD="SocietyCGIIP"; ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null SOCIETYS${SET_ID} "awk -F\"=\" -v k=\"${KEYWORD}\" '\$0 ~ k {print \$2}' $HOME/servers${SET_ID}/SocietyServer${SET_ID}/setup.ini")
SS_CGI_Port=$(KEYWORD="SocietyCGIPort"; ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null SOCIETYS${SET_ID} "awk -F\"=\" -v k=\"${KEYWORD}\" '\$0 ~ k {print \$2}' $HOME/servers${SET_ID}/SocietyServer${SET_ID}/setup.ini")
SS_CGI_Pass=$(KEYWORD="SocietyCGIKey"; ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null SOCIETYS${SET_ID} "awk -F\"=\" -v k=\"${KEYWORD}\" '\$0 ~ k {print \$2}' $HOME/servers${SET_ID}/SocietyServer${SET_ID}/setup.ini")

echo "$(cat <<EOF
DELETE FROM db_setup WHERE is_commercial = 1 AND world_id = ${ws_id} AND set_id = ${SET_ID};
INSERT INTO db_setup VALUES (${ws_id}, ${SET_ID}, '[LIVE]${ws_id}', '${WORLD_DB_NAME}${ws_id}', '${WORLDDB_INT_IP}', '${GAME_DB_NAME}${SET_ID}', '${GAMEDB_INT_IP}', '${ACCOUNT_DB_NAME}', '${ACCOUNTDB_INT_IP}', '${SOCIETY_DB_NAME}${SET_ID}', '${SOCIETYDB_INT_IP}', '${DB_ACCOUNT}', '${DB_PASS}', '${MS_CGI_IP}', '${MS_CGI_Port}', '${MS_CGI_Pass}', '${SS_CGI_IP}', '${SS_CGI_Port}', '${SS_CGI_Pass}', 1, '${TIMEZONE}', 0, 0, NULL, NULL, 0, ${BLOCK_PREVIOUS_WORLD});
EOF
)"
done

echo
