#!/bin/bash

mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
[ -d $mydir ] || exit 4
configfile="$mydir/spm_cloudsave_config.sh"

if [ -f "$configfile" ]
then
	. "$configfile"
else
	echo "No config file find :("
	exit 4
fi


cloudsave_rsync ()
{
	if [ -e "$CLOUDSAVES_TEMP" ] ; then
		echo "Temp directory seem to yet exists :("
		exit 4
	fi


	date +%Y%m%d%Hh%M%S
	echo "cp -al source → cloud temp..."
	cp -al "$CLOUDSAVES_SOURCE" "$CLOUDSAVES_TEMP"
	sync


	date +%Y%m%d%Hh%M%S
	echo "Trunc namelength in cloud temp..."
	adp_namelength.sh trunc "$CLOUDSAVES_TEMP" 110
	sync


	date +%Y%m%d%Hh%M%S
	echo "Rsync cloud temp → clear"
	rsync -aH --delete --numeric-ids "$CLOUDSAVES_TEMP/" "$CLOUDSAVES_eCLEAR/"
	sync


	date +%Y%m%d%Hh%M%S
	echo "rm -r temp..."
	rm -r "$CLOUDSAVES_TEMP"


	date +%Y%m%d%Hh%M%S
	echo "Rsync cloud crypted → target..."
	# -H ? --inplace ? --link-dest  ?
	# rsync -az --delete --numeric-ids "$CLOUDSAVES_eCRYPTED/" "$CLOUDSAVES_TARGET"
}

cloudsave_rsync
