#!/bin/sh

# Written by Nicholas Nakano
# Edited by Giovanni Vindiola

version=$(rclone --version 2>>errors | head -n 1)
if [ -z "$install_beta" ]; then
    current_version=$(curl -fsS https://downloads.rclone.org/version.txt)
else
    current_version=$(curl -fsS https://beta.rclone.org/version.txt)
fi

if [ "$version" = "$current_version" ]; then
    printf "The latest ${install_beta}version of rclone ${version} is already installed. Jumping to configuration step..."
else
	echo "No existing installation of RClone detected, installing now..."
#	# Check operating system
#	OS=""
#	case $OS in
#		msys)
#			OS='windows'
#			;;
#		darwin*)
#			OS='osx'
#			;;
#		linux-gnu)
#			OS='linux'
#			;;
#		Ubuntu)
#			OS='focal'
#			;;
#		*)
#			echo "OS not supported -1."
#			exit 2
#			;;
#	esac
# Detect the platform (similar to $OSTYPE)
case "$OSTYPE" in
  solaris*) echo "SOLARIS" ;;
  darwin*)  echo "OSX" ;; 
  linux*)   echo "LINUX" ;;
  bsd*)     echo "BSD" ;;
  msys*)    echo "WINDOWS" ;;
  cygwin*)  echo "ALSO WINDOWS" ;;
  *)        echo "unknown: $OSTYPE" ;;
esac


	OS_type="$(uname -m)"
	case "$OS_type" in
		x86_64|amd64)
			OS_type='amd64'
			;;
		i?86|x86)
			OS_type='386'
			;;
		aarch64|arm64)
			OS_type='arm64'
			;;
		arm*)
			OS_type='arm'
			;;
		*)
			echo "OS not supported."
			exit 2
			;;
	esac

	if [ "$OS" == 'windows' ] || [ "$OS" == 'Windows_NT' ]; then
			OS='windows'	
			curl -O "https://downloads.rclone.org/rclone-current-${OS}-${OS_type}.zip"
		if [ $? -ne 0 ]; then
			echo "RClone failed to download."
			exit 1
		fi

		unzip rclone-current-${OS}-${OS_type}.zip

		cd rclone-*-${OS}-${OS_type}/
		case $OS in
			windows)
				cp rclone.exe /mingw64/bin/
				echo "Windows: Moving rclone.exe to bin..."
				;;
			*)
				echo "Could not find appropriate executable. Exiting."
				rm rclone-current-${OS}-${OS_type}.zip
				rm -dr rclone-*-${OS}-${OS_type}/
				exit 2
				;;
		esac

		cd ..
		rm rclone-current-${OS}-${OS_type}.zip
		rm -dr rclone-*-${OS}-${OS_type}/
	else
		curl https://rclone.org/install.sh | sudo bash
	fi
	echo "RClone installation complete, moving to configuration step..."
fi

KEYFILE=keys/keys.txt

if test -f "$KEYFILE"; then
	ACCESS_KEY_ID=$(grep "access_key_id" "$KEYFILE" | awk -F= '{print $2}')
	SECRET_ACCESS_KEY=$(grep "secret_access_key" "$KEYFILE" | awk -F= '{print $2}')
	echo "Found key-file, attempting to connect to RClone..."
	rclone config create nautilus-s3 s3 endpoint https://s3-west.nrp-nautilus.io provider Ceph access_key_id $ACCESS_KEY_ID secret_access_key $SECRET_ACCESS_KEY
	rclone lsd nautilus-s3:
else
	echo "Could not find key-file ${KEYFILE}."
	test -f "$KEYFILE"
fi

while [ $? -ne 0 ]
do
	echo "RClone failed to connect to S3 remote. Please manually re-input keys."
	rclone config delete nautilus-s3
	echo -n "Access Key ID?: "
	read ACCESS_KEY_ID
	echo -n "Secret Access Key?: "
	read SECRET_ACCESS_KEY
	rclone config create nautilus-s3 s3 endpoint https://s3-west.nrp-nautilus.io provider Ceph access_key_id $ACCESS_KEY_ID secret_access_key $SECRET_ACCESS_KEY
	rclone lsd nautilus-s3:
done

echo "RClone initialization complete. Pulling files from S3 storage via RClone..."

rclone copy nautilus-s3:cyberarch-content ./ -P --no-traverse --filter-from rclone-filter.txt

if [ $? -ne 0 ]; then
	echo "RClone failed to download files from S3."
	exit 1
fi

echo "Updating local git-hooks..."

cp -R git-hooks/* .git/hooks/

echo "All files pulled from S3 successfully. Operation complete."

exit 0