#!/bin/bash
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

. "${SCRIPT_DIR}/../settings.sh" || {
    echo "no settings" && exit 1
}

CONTAINER=$(docker ps | grep mariadb | awk '{print $1}')

if [[ -z "${CONTAINER}" ]]; then
    echo "Unable to find container: db"
    exit 1
fi

MYSQL_CMD="docker exec -i ${CONTAINER} mariadb -u root -p123456"

echo -n "Drop and recreate ${LOCAL_DATABASE_NAME} database... "
bash -c "${MYSQL_CMD} -e 'DROP DATABASE \`${LOCAL_DATABASE_NAME}\`'"
bash -c "${MYSQL_CMD} -e 'CREATE DATABASE \`${LOCAL_DATABASE_NAME}\`'"
echo "OK"

echo -n "Importing prod dump data from $SSH_PROD_SERVER ... "
ssh -C ${SSH_PROD_USERNAME}@${SSH_PROD_SERVER} "mariadb-dump --defaults-extra-file=/home/${SSH_PROD_USERNAME}/.my.cnf ${PROD_DATABASE_NAME} | sed '1d'" | pv | bash -c "${MYSQL_CMD} ${LOCAL_DATABASE_NAME}"
echo "OK"