#!/usr/bin/env bash

function usage {
	echo "usage:"
	echo "  datatier-init-dirs /path/to/tier1 /path/to/tier2 [ /path/to/tier3 ... ]"
}

function join_by { local IFS="$1"; shift; echo "$*"; }

command -v rsync > /dev/null 2>&1 || { echo "rsync must be installed to run this script. Install it and try again."; exit 1; }

while getopts "h" opt; do
	case $opt in
	h)
		usage
		exit 0
		;;
	*)
		echo "Unknown option."
		echo
		usage
		exit 1
		;;
	esac
done

if [[ "$#" < "2" ]]; then
	echo Must provide at least two tier paths.
	usage
	exit 1
fi

# check that each argument is a valid path
for tier in $@; do
	if [ ! -d "$tier" ]; then
		echo "$tier is not a directory!"
		echo
		usage
		exit 1
	fi
done

read -p "Clone directories across {$(join_by , $@)}? [y/N]" response
case $response in
	y|yes|Y|YES)
		;;
	*)
		exit 0
		;;
esac

for src in $@; do
	for dst in $@; do
		[[ "$src" != "$dst" ]] && rsync -a --include='*/' --exclude='*' $src/* $dst
	done
done

exit 0
