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

num=0
echo "######"
echo "Check all promotion tables"
echo
for table in $PROMOTION_TABLES_APPLY_TO_LIVE; do
    RESULT=$(psql -U postgres ${ACCOUNT_DB_NAME} -Atqc "SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = '${table}');")
    if [ "${RESULT}" == "t" ]; then
        ((num++))
        RETURN="(O)"
    else
        RETURN="(X)"
    fi
    echo "${table} ${RETURN} [${num}]"
done
echo "######"
echo

# 讀入陣列，以空白分隔
read -r -a expect_tables_arr <<< "$PROMOTION_TABLES_APPLY_TO_LIVE"

echo "Total Expect Tables: ${#expect_tables_arr[@]}"
echo "Total Actual Tables: ${num}"
if [ "${#expect_tables_arr[@]}" == "${num}" ]; then
    echo "All Promotion Tables Exists (O)"
else
    echo "Some Promotion Tables Not Exist (X)" 
fi
echo 
