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

LOCAL_DIRECTORY_NAME="$1"
[ -z "${LOCAL_DIRECTORY_NAME}" ] && DIRECTORY_NAME="." || DIRECTORY_NAME="${LOCAL_DIRECTORY_NAME}"

num=0
echo "######"
echo "Drop all promotion tables"
echo
for table in $PROMOTION_TABLES_APPLY_TO_LIVE; do
    ((num++))
    echo "Drop TABLE ${table} [${num}]..."
    psql -U postgres $ACCOUNT_DB_NAME -c "DROP TABLE ${table};"
done
echo "######"

num=0
cd ${DIRECTORY_NAME}
echo "######"
echo "Restore Directory: ${DIRECTORY_NAME}"
echo
for file in $(ls *sql); do
    ((num++))
    table=$(echo ${file}| sed 's/.sql//')
    echo "Restore TABLE ${table} [${num}]..."
    psql -U postgres $ACCOUNT_DB_NAME -c "\i ${file};"
done
echo "######"
echo

echo "Total Restore Tables: ${num}"
echo 
