#!/usr/bin/env bash

# A bash script to replace a local database with one on a server
# It relies on wp being installed on both the server and the local machine
# - Note, if using xampp, mamp .. you should make sure that mysql and php are
#         both in your $PATH
# On your local machine, it assumes that WP is in a folder called public_html

# SSH entry from .ssh/config
sshKey="$1"
# The folder so search for on the remote server
# the home folder is grepped for this so public should get public/ and public_html/
sshFolder="${2:-public}"

# If the ssh entry isn't given, exit with a warning
if [ -z "$sshKey" ]; then
	echo "Add an SSH entry"
	exit 1
fi

# This command exits with a non-zero exit code if not a multisite
wp site list 2>/dev/null >/dev/null
if [ $? -eq 0 ]; then
	echo "Appears to be a multisite"
	multisite="true"
else
	echo "Appears not to be a multisite"
	multisite="false"
fi

# Finds the local public_html folder
public_html="${PWD%/public_html*}/public_html"
# Gets the folder that we should Copy from
echo "Getting site variables from the server"
sshFolder="$(ssh "$sshKey" ls | grep "$sshFolder")"
# gets the fell path of the folder for the path replace later on
sshFolderPath="$(ssh "$sshKey" "cd $sshFolder; pwd")"
if [ "$multisite" == "true" ]; then
	# Gets an array of domains on the remote server
	remoteDomains=($(ssh "$sshKey" "cd $sshFolder; wp site list 2> /dev/null" | cut -f 2 | tail -n +2 | sed -E "s/https?:\/\///g" | sed -E "s/\/$//g"))
	# Gets an array of domains on the local server
	localDomains=($(wp site list 2> /dev/null | cut -f 2 | tail -n +2 | sed -E "s/https?:\/\///g" | sed -E "s/\/$//g"))
else
	# puts the site url in an array
	remoteDomains=($(ssh "$sshKey" "cd $sshFolder; wp option get siteurl 2> /dev/null" | sed -E "s/https?:\/\///g" | sed -E "s/\/$//g"))
	# Gets an array of domains on the local server
	localDomains=($(wp option get siteurl 2> /dev/null | sed -E "s/https?:\/\///g" | sed -E "s/\/$//g"))
fi

echo "Downloading database"
# Dumps the database into our public_html folder
ssh "$sshKey" "cd $sshFolder; wp db export -" > "$public_html"/db.dump

# Imports the new database
echo "Importing the database"
wp db import "$public_html"/db.dump 2> /dev/null > /dev/null

# Loops through the domains and does a search and replace
echo "Performing find and replace"
for (( i = 0; i < ${#remoteDomains[@]}; i++ )); do
	echo "${remoteDomains[$i]} -> ${localDomains[$i]}"
	wp search-replace --all-tables --url="${remoteDomains[$i]}" "${remoteDomains[$i]}" "${localDomains[$i]}" 2> /dev/null > /dev/null
done

# Searches and replaces the paths
echo "$sshFolderPath -> $public_html"
wp search-replace --all-tables --url="${localDomains[0]}" "$sshFolderPath" "$public_html" 2> /dev/null > /dev/null

# Removes the db.dump that we created
rm "$public_html"/db.dump

#Makes sure everything in the uploads folder on the server is also on the local
rsync -ruh --progress "${sshKey}:${sshFolder}/wp-content/uploads/" "${public_html}/wp-content/uploads"
