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

REMOTE_MACHINE_NAME="$1"
LOCAL_DIRECTORY_NAME="$2"
[ -z "${LOCAL_DIRECTORY_NAME}" ] && DIRECTORY_NAME=$(date +"%Y%m%dT%H%M%S") || DIRECTORY_NAME=$(basename ${LOCAL_DIRECTORY_NAME})

mkdir -p /tmp/itemmall_${DIRECTORY_NAME}; cd /tmp/itemmall_${DIRECTORY_NAME};

num=0
echo "######"
echo "Dump all promotion tables"
echo
for table in $PROMOTION_TABLES_APPLY_TO_LIVE; do
    ((num++))
    echo "Dump ${table} [${num}]..."
    pg_dump -U postgres $ACCOUNT_DB_NAME -t ${table} > ${table}.sql
done
echo "######"
echo

echo
[ -z "${REMOTE_MACHINE_NAME}" ] && echo "Rsync to Remote Machine"; cd /tmp; tar cvzf - itemmall_${DIRECTORY_NAME} | ssh ${REMOTE_MACHINE_NAME} '(cd /tmp && tar xvzf -)'
echo

echo "Total Dump Tables: ${num}"
echo
[ -z "${REMOTE_MACHINE_NAME}" ] && echo "ssh -o StrictHostKeyChecking=no ${REMOTE_MACHINE_NAME} \"cd /tmp/itemmall_${DIRECTORY_NAME}; $HOME/bin/restore_all_promotion_tables /tmp/itemmall_${DIRECTORY_NAME}\""; ssh -o StrictHostKeyChecking=no ${REMOTE_MACHINE_NAME} "cd /tmp/itemmall_${DIRECTORY_NAME}; $HOME/bin/restore_all_promotion_tables /tmp/itemmall_${DIRECTORY_NAME}"
