#!/bin/bash

######################
### Upload Script ####
######################
### Version 0.95.5 ###
######################
####### EDIT ONLY THESE SETTINGS #######
# REQUIRED SETTINGS
#https://hc-ping.com/cc29182e-1cad-4218-8533-e53c6d0b8d2a
BackupRemoteLocation="series"
RcloneLogLevel="INFO"
RcloneCommand="move" # choose your rclone command e.g. move, copy, sync
RcloneRemoteName="dbc_series" # 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/series" # 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
ModSort="ascending" # "ascending" oldest files first, "descending" newest files first
LogFile="/opt/logs/rclone/media_series_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"

####### END SETTINGS #######
echo "$(date "+%d.%m.%Y %T") INFO: *** Starting rclone_upload script for ${RcloneUploadRemoteName} ***" >> $LogFile

####### 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

echo "$(date "+%d.%m.%Y %T") INFO: Uploading using upload remote ${RcloneUploadRemoteName}" >> $LogFile

#######  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

rclone $RcloneCommand $LocalFilesLocation $RcloneUploadRemoteName:$BackupRemoteLocation \
	$ServiceAccount \
	--user-agent="$RcloneUploadRemoteName" \
	--log-level=$RcloneLogLevel \
	--buffer-size 512M \
	--drive-chunk-size 512M \
	--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~* \
	--exclude _kut-download* \
	$DeleteEmpty


#######  Remove Control Files  ##########
# remove dummy file
rm /opt/rclone/remotes/$RcloneUploadRemoteName/upload_running$JobName
echo "$(date "+%d.%m.%Y %T") INFO: Script complete" >> $LogFile

exit
