#!/usr/bin/env bash

## Download DB from assets server.
##
## Usage: fin download/db

. ${PROJECT_ROOT}/.docksal/scripts/common

__fail_message="
Failed to download the database dump.

Either:
- your username is wrong or
- you do not have the required permissions.

If you need any assistance, please contact Chris. If Chris is not available contact Franz or Muhammad.
"

# Make sure assets username is configured.
if [ -z "$(fin assets/username get)" ]; then
  fin assets/username
fi

# Get the username.
readonly ASSETS_USERNAME=$(fin assets/username get);

if [ -n "${ASSETS_PREFIX}" ] && [ "${ASSETS_DB}" == "yes" ] ; then
	mkdir -p ${ASSETS_DIR};
	if rsync -azLP "$ASSETS_USERNAME"@assets.tree.ewdev.ca:${ASSETS_PREFIX}/${ASSETS_DB_FILE_NAME} ${ASSETS_DIR}/drupal.sql.gz;
	then
		print_info_message "Downloaded the file to ${ASSETS_DIR}/drupal.sql.gz";
	else
		print_error_message "$__fail_message";
		exit 0;
	fi;
else
	if [ -z "${ASSETS_PREFIX}" ]; then
		print_info_message "Skipping database download as ASSETS_PREFIX is not set.";
	else
		print_info_message "Skipping database download as ASSETS_DB is not set to yes.";
	fi;
fi;
