#!/bin/bash

[ "$1" == "" ] && exec echo "Usage: $0 <Set number|Account ID> [Character ID/Name]"

source ~/.gamerc

if [ "$2" == "" ] ; then
	PGPASSWORD="$LIVE_SERVER_DB_PASSWORD" psql -h accountdb $ACCOUNT_DB_NAME <<< "copy (select username from accounts where id=$1) to STDOUT csv;"
else
	if grep -q "^[0-9]\+$" <<< $2 ; then
	        PGPASSWORD="$LIVE_SERVER_DB_PASSWORD" psql -h gamedb$1 $GAME_DB_NAME$1 <<< "copy (select account_name from player_characters where id=$2) to stdout csv;"
	else
	        PGPASSWORD="$LIVE_SERVER_DB_PASSWORD" psql -h gamedb$1 $GAME_DB_NAME$1 <<< "copy (select account_name from player_characters where given_name='$2') to stdout csv;"
	fi
fi
