#!/bin/bash

# https://rclone.org/

# rclone config ; [~/.config/rclone/rclone.conf]

# ====================================================== #
# Example backup script for general content using rclone #
#                                                        #
# Install rclone:                                        #
#     curl https://rclone.org/install.sh | sudo bash     #
#                                                        #
# Also: https://five.sh/files/scripts/colors.sh          #
#     => /usr/local/bin/colors                           #
#                                                        #
# ====================================================== #

source "/usr/local/bin/colors"

# Declare config
declare -A conf=(
	[config]="gd"
	[mode]="copy"
)

# Declare the local directories to backup
declare -A directories=(
	[art]="/mnt/e/Art/"
	[countries]="/mnt/e/Countries/"
)

# Declare the destinations to backup
declare -A destinations=(
	[art]="Open/Art/"
	[countries]="Open/Countries/"
)

declare -a keys=()

add_key()
{
	if [[ ! -z ${directories["$1"]} ]] && [[ ! -z ${destinations["$1"]} ]]; then
		keys=("${keys[@]}" "$1")
	else
		printf "$(echored "Key does not exist. Make sure it has been declared in both arrays. Exiting.")\n"
		exit
	fi
}

# Set keys to update, check for mode changes et cetera
if [ "$#" -gt 0 ]; then
	for arg in "$@"
	do
		if [ "$arg" = "copy" ] || [ "$arg" = "sync" ]; then
			conf[mode]="$arg"
		else
			add_key "$arg"
		fi
	done
fi

if [ "$#" -eq 0 ] || [ "${#keys[@]}" -eq 0 ]; then
	for key in "${!directories[@]}"
	do
		add_key "$key"
	done
fi

if [ "${#keys[@]}" -eq 0 ]; then
	echo "Got no keys to update."
	exit
fi

printf "> Using key(s) $(echogreen "'${keys[*]}'")\n"
printf "> Using mode $(echoyellow "'${conf[mode]}'")\n\n"

index=0
count=${#keys[@]}

# Iterate over directories, backup data
for key in "${keys[@]}"
do
	((index++))

	printf "> $index/$count; $(echoyellow "${conf[mode]}") ($key[source]) => $(echogreen "'${directories[$key]}'") ..\n"
	printf "> $index/$count; $(echoyellow "${conf[mode]}") ($key[destination]) => $(echoblue "'${conf[config]}:${destinations[$key]}'") ..\n\n"

	rclone "${conf[mode]}" "${directories[$key]}" "${conf[config]}":"${destinations[$key]}" -P

	printf "\n"
done