#!/bin/bash -e

### Change the values below
path_to_live_site="/home/members/fflic/sites/fflic.org/web"
path_to_dev_site="/home/members/fflic/sites/dev.fflic.org/web"

## optionally file in the following database info if you want to 
## copy the dev database over the live database
live_site_db_name=
dev_site_db_user=
dev_site_db_pass=
dev_site_db_name=

### Optionally change the value below - by default the script will create
### a directory in the live-user's home directory called sync-staging which
### will hold temporary copies of the various sites
path_to_sync_staging=~/sync-staging

### Optionally change the path below if you have a read protected settings 
### file on your dev site. If you dev site does not have a read protected
### settings file, then comment out this line 
#settings_file="sites/default/settings.php"

### No need to change any other values

if [ ! -f ~/.my.cnf ]; then
	echo "I didn't find a .my.cnf file - which means "
	echo "I won't be able to automatically login to your "
	echo "database. Please create a .my.cnf file with your "
	echo "database details or request help from support.mayfirst.org"
	exit
fi

if [ ! -d "$path_to_live_site" ]; then
	echo "Path to live site ($path_to_live_site) doesn't exist"
	exit
fi

if [ ! -d "$path_to_dev_site" ]; then
	echo "Path to dev site ($path_to_dev_site) doesn't exist"
	exit
fi

# remove trailing slash from web variables
path_to_live_site=$(echo $path_to_live_site|sed "s/\\/$//")
path_to_dev_site=$(echo $path_to_dev_site|sed "s/\\/$//")

live_backup="$path_to_sync_staging/live-backup"
dev_staging="$path_to_sync_staging/dev-staging"

if [ ! -d "$path_to_sync_staging" ]; then
	echo "Creating $path_to_sync_staging"
	mkdir "$path_to_sync_staging"
fi

if [ ! -d "$live_backup" ]; then
	echo "Creating $live_backup"
	mkdir "$live_backup"
fi

if [ ! -d "$dev_staging" ]; then
	echo "Creating $dev_staging"
	mkdir "$dev_staging"
fi

function interpret_reply() {
	if [ "$REPLY" = "y" ] || [ "$REPLY" == "Y" ] || [ -z "$REPLY" ]; then
		echo "Executing...."
		REPLY=y
	else
		echo "Not executing"
		REPLY=n
	fi
}

echo
echo "Path to dev site: $path_to_dev_site"
echo "Path to live site: $path_to_live_site"
echo "Live backup: $live_backup"
echo "Dev staging: $dev_staging"

echo

read -p "Back up live site files? [Yn] "
interpret_reply
if [ "$REPLY" = "y" ]; then
	rsync -av "$path_to_live_site" "$live_backup/"
fi

if [ -n "$live_site_db_name" ]; then
	read -p "Back up live site database? [Yn] "
	interpret_reply
	if [ "$REPLY" = "y" ]; then
		mysqldump "$live_site_db_name" > "$live_backup/${live_site_db_name}.sql"
	fi
fi

echo
read -p "Synchronizing files from dev site to staging area? [Yn] "
interpret_reply
if [ "$REPLY" = "y" ]; then
	exclude=
	if [ -n "$settings_file" ]; then
		exclude="--exclude '*{$settings_file}'"
	fi
	rsync -av $exclude "${path_to_dev_site}" "${dev_staging}/"
fi

if [ -n "$dev_site_db_user" ]; then
	echo
	read -p "Dump dev database to staging area? [Yn] "
	interpret_reply
	if [ "$REPLY" = "y" ]; then
		mysqldump -u "$dev_site_db_user" -p${dev_site_db_pass} "$dev_site_db_name" > "${dev_staging}/${dev_site_db_name}.sql"
	fi
fi

if [ -n "$settings_file" ]; then
	echo
	read -p "Copy live site settings.php to dev staging area? [Yn] "
	interpret_reply
	if [ "$REPLY" = "y" ]; then
		cp "${path_to_live_site}/$settings_file" "${dev_staging}/web/$settings_file"
	fi
fi

echo
echo 
echo "Your live site has been backed up and the dev site is staged to be moved"
echo "Please carefully examine:"
echo  $path_to_sync_staging 
echo "to make sure everything is in place"
echo
read -p "Press any key to make the dev site live or hit ctl-cancel to abort"

echo
read -p "Delete live web site files? [Yn] "
interpret_reply
if [ "$REPLY" = "y" ]; then
	rm -rf $path_to_live_site/*	
	rm -f $path_to_live_site/.htaccess
fi

echo
read -p "Copy in dev  files? [Yn] "
interpret_reply
if [ "$REPLY" = "y" ]; then
	rsync -av "${dev_staging}/web/" "$path_to_live_site/"
fi

if [ -n "$dev_site_db_name" ]; then
	echo
	read -p "Import database? [Yn] "
	interpret_reply
	if [ "$REPLY" = "y" ]; then
		# drop all tables in the existing database
		mysqldump  --add-drop-table --no-data "$live_site_db_name" | grep ^DROP | mysql 
		mysql < "$dev_staging/${dev_db_name}.sql"
	fi
fi

echo "All done."

