#!/bin/bash

######################
### Upload Script ####
######################
### Version 0.95.5 ###
######################
####### EDIT ONLY THESE SETTINGS #######
# REQUIRED SETTINGS
#https://hc-ping.com/cc29182e-1cad-4218-8533-e53c6d0b8d2a
BackupRemoteLocation=""
BackupRemoteDeletedLocation=""
BackupRetention=""
BackupDir=""
RCloneMountIP=""
RcloneLogLevel="ERROR"
RcloneCommand="move" # choose your rclone command e.g. move, copy, sync
RcloneRemoteName="dbc_movies" # Name of rclone remote mount WITHOUT ':'.
RcloneUploadRemoteName="dbc_tft" # If you have a second remote created for uploads put it here.  Otherwise use the same remote as RcloneRemoteName.
LocalFilesShare="/mnt/move/movies" # location of the local files without trailing slash you want to rclone to use
RcloneMountShare="/mnt" # where your rclone mount is located without trailing slash  e.g. /mnt/user/mount_rclone
MinimumAge="1w" # sync files suffix ms|s|m|h|d|w|M|y
RcloneTransfers=4
RcloneCheckers=2
RcloneTpsLimit=4
RcloneBwLimit=60m
ModSort="ascending" # "ascending" oldest files first, "descending" newest files first
LogFile="/opt/logs/rclone/rclone_media_upload.log"
export RCLONE_CONFIG=/opt/rclone/rclone.conf
# Add name to upload job
JobName="_media_upload" # Adds custom string to end of checker file.  Useful if you're running multiple jobs against the same remote.

# Add extra commands or filters
Command1="--exclude /ebooks/**"
Command2="--exclude /snippets/**"
Command3="--exclude /.zzz_mnt_move"
Command4="--drive-upload-cutoff 1000T"
# Use Service Accounts.  Instructions: https://github.com/xyou365/AutoRclone
UseServiceAccountUpload="N" # Y/N. Choose whether to use Service Accounts.
ServiceAccountDirectory="/opt/rclone/sakeys" # Path to your Service Account's .json files.
ServiceAccountFile="sa_upload" # Enter characters before counter in your json files e.g. for sa_gdrive_upload1.json -->sa_gdrive_upload100.json, enter "sa_gdrive_upload".
CountServiceAccounts="5" # Integer number of service accounts to use.
####### END SETTINGS #######
echo "$(date "+%d.%m.%Y %T") INFO: *** Starting rclone_upload script for ${RcloneUploadRemoteName} ***" >> $LogFile
###############################################################################
#####    DO NOT EDIT BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING   #####
###############################################################################
####### Preparing mount location variables #######
LocalFilesLocation="$LocalFilesShare"
RcloneMountLocation="$RcloneMountShare/$RcloneRemoteName" # Location of rclone mount
echo "$(date "+%d.%m.%Y %T") INFO: *** Rclone move selected.  Files will be moved from ${LocalFilesLocation} for ${RcloneUploadRemoteName} ***" >> $LogFile
####### create directory for script files #######
mkdir -p /opt/rclone/remotes/$RcloneUploadRemoteName #for script files
#######  Check if script already running  ##########
echo "$(date "+%d.%m.%Y %T") INFO: *** Starting rclone_upload script for ${RcloneUploadRemoteName} ***" >> $LogFile
if [[ -f "/opt/rclone/remotes/$RcloneUploadRemoteName/upload_running$JobName" ]]; then
	echo "$(date "+%d.%m.%Y %T") INFO: Exiting as script already running." >> $LogFile
	exit
else
	echo "$(date "+%d.%m.%Y %T") INFO: Script not running - proceeding." >> $LogFile
	touch /opt/rclone/remotes/$RcloneUploadRemoteName/upload_running$JobName
fi
#######  check if rclone installed  ##########
echo "$(date "+%d.%m.%Y %T") INFO: Checking if rclone installed successfully." >> $LogFile
if [[ -f "$RcloneMountLocation/.zzz_tdrive" ]]; then
	echo "$(date "+%d.%m.%Y %T") INFO: rclone installed successfully - proceeding with upload." >> $LogFile
else
	echo "$(date "+%d.%m.%Y %T") INFO: rclone not installed - will try again later." >> $LogFile
	rm /opt/rclone/remotes/$RcloneUploadRemoteName/upload_running$JobName
	exit
fi
####### Rotating serviceaccount.json file if using Service Accounts #######
if [[ $UseServiceAccountUpload == 'Y' ]]; then
	cd /opt/rclone/remotes/$RcloneUploadRemoteName/
	CounterNumber=$(find -name 'counter*' | cut -c 11,12)
	CounterCheck="1"
	if [[ "$CounterNumber" -ge "$CounterCheck" ]];then
		echo "$(date "+%d.%m.%Y %T") INFO: Counter file found for ${RcloneUploadRemoteName}." >> $LogFile
	else
		echo "$(date "+%d.%m.%Y %T") INFO: No counter file found for ${RcloneUploadRemoteName}. Creating counter_1." >> $LogFile
		touch /opt/rclone/remotes/$RcloneUploadRemoteName/counter_1
		CounterNumber="1"
	fi
	ServiceAccount="--drive-service-account-file=$ServiceAccountDirectory/$ServiceAccountFile$CounterNumber.json"
	echo "$(date "+%d.%m.%Y %T") INFO: Adjusted service_account_file for upload remote ${RcloneUploadRemoteName} to ${ServiceAccountFile}${CounterNumber}.json based on counter ${CounterNumber}." >> $LogFile
else
	echo "$(date "+%d.%m.%Y %T") INFO: Uploading using upload remote ${RcloneUploadRemoteName}" >> $LogFile
	ServiceAccount=""
fi
#######  Upload files  ##########
#  Remove --delete-empty-src-dirs if rclone sync or copy
if [[  $RcloneCommand == 'move' ]]; then
	echo "$(date "+%d.%m.%Y %T") INFO: *** Using rclone move - will add --delete-empty-src-dirs to upload." >> $LogFile
	DeleteEmpty="--delete-empty-src-dirs "
else
	echo "$(date "+%d.%m.%Y %T") INFO: *** Not using rclone move - will remove --delete-empty-src-dirs to upload." >> $LogFile
	DeleteEmpty=""
fi
# process files
	# rclone $RcloneCommand $LocalFilesLocation $RcloneUploadRemoteName:$BackupRemoteLocation \
	# $ServiceAccount \
	# --user-agent="$RcloneUploadRemoteName" \
	# -vv \
	# --buffer-size 512M \
	# --drive-chunk-size 512M \
	# --tpslimit 5 \
	# --checkers 8 \
	# --transfers 4 \
	# --min-age $MinimumAge \
	# --order-by modtime,$ModSort \
	# $Command1 $Command2 $Command3 $Command4  \
	# --exclude *fuse_hidden* \
	# --exclude *_HIDDEN \
	# --exclude .recycle** \
	# --exclude .Recycle.Bin/** \
	# --exclude *.backup~* \
	# --exclude *.partial~* \
	# --drive-stop-on-upload-limit \
	# $DeleteEmpty

 	rclone $RcloneCommand $LocalFilesLocation $RcloneUploadRemoteName:$BackupRemoteLocation \
	$ServiceAccount \
	--user-agent="$RcloneUploadRemoteName" \
	--log-level=$RcloneLogLevel \
	--buffer-size 512M \
	--drive-chunk-size 512M \
	--bwlimit $RcloneBwLimit \
	--tpslimit $RcloneTpsLimit \
	--checkers $RcloneCheckers \
	--transfers $RcloneTransfers \
	--max-transfer 740G \
	--min-age $MinimumAge \
	--order-by modtime,$ModSort \
	$Command1 $Command2 $Command3 $Command4  \
	--exclude *fuse_hidden* \
	--exclude *_HIDDEN \
	--exclude .recycle** \
	--exclude .Recycle.Bin/** \
	--exclude *.backup~* \
	--exclude *.partial~* \
	--drive-stop-on-upload-limit \
	$DeleteEmpty


#######  Remove Control Files  ##########
# update counter and remove other control files
if [[  $UseServiceAccountUpload == 'Y' ]]; then
	if [[ "$CounterNumber" == "$CountServiceAccounts" ]];then
		rm /opt/rclone/remotes/$RcloneUploadRemoteName/counter_*
		touch /opt/rclone/remotes/$RcloneUploadRemoteName/counter_1
		echo "$(date "+%d.%m.%Y %T") INFO: Final counter used - resetting loop and created counter_1." >> $LogFile
	else
		rm /opt/rclone/remotes/$RcloneUploadRemoteName/counter_*
		CounterNumber=$((CounterNumber+1))
		touch /opt/rclone/remotes/$RcloneUploadRemoteName/counter_$CounterNumber
		echo "$(date "+%d.%m.%Y %T") INFO: Created counter_${CounterNumber} for next upload run." >> $LogFile
	fi
else
	echo "$(date "+%d.%m.%Y %T") INFO: Not utilising service accounts." >> $LogFile
fi
# remove dummy file
rm /opt/rclone/remotes/$RcloneUploadRemoteName/upload_running$JobName
echo "$(date "+%d.%m.%Y %T") INFO: Script complete" >> $LogFile

exit
