#!/bin/bash

######################
### Upload Script ####
######################
### Version 0.95.5 ###
######################

# REQUIRED SETTINGS
#https://hc-ping.com/cc29182e-1cad-4218-8533-e53c6d0b8d2a
RCLONE_SCRIPT_FILE=$(basename "$0") 
LOG_PATH="/opt/logs"
export RCLONE_CONFIG=/opt/rclone/rclone.conf
if [ "$1" == "" ] ; then
	echo "Syntax: $RCLONE_SCRIPT_FILE <SOURCE> <DESTINATION=dbc_tft:> <FOLDER=/> <COMMAND=move> <LOG_LEVEL=INFO>" 
	exit 
fi
RCLONE_SRC_DIR="$1" # location of the local files without trailing slash you want to rclone to use
[[ "$2" == "" ]] && RCLONE_DEST_DIR="dbc_tft:" || RCLONE_DEST_DIR="$2" 
if [ "$3" != "" ]; then 
	RCLONE_SRC_DIR="$RCLONE_SRC_DIR/$3" 
	RCLONE_DEST_DIR="$RCLONE_DEST_DIR/$3" 
fi
[[ "$4" == "" ]] && RCLONE_COMMAND="move" || RCLONE_COMMAND="$4" # choose your rclone command e.g. move, copy, sync
[[ "$5" == "" ]] && RCLONE_LOG_LEVEL="INFO" || RCLONE_LOG_LEVEL="$5" 
[[ "$6" == "" ]] && RCLONE_MINIMUM_AGE="1w" || RCLONE_MINIMUM_AGE="$6" # sync files suffix ms|s|m|h|d|w|M|y

RCLONE_DEST_DRIVE="${RCLONE_DEST_DIR//:/}"	# Replace ":" with nothing

RCLONE_MOD_SORT="ascending" # "ascending" oldest files first, "descending" newest files first
JOB_NAME="_media_upload" # Adds custom string to end of checker file.  Useful if you're running multiple jobs against the same remote.
RCLONE_CMD_1="--exclude /ebooks/**"
RCLONE_CMD_2="--exclude /snippets/**"
RCLONE_CMD_3="--exclude /.zzz_mnt_move"
RCLONE_CMD_4=""
RCLONE_LOG_FILE="$LOG_PATH/${RCLONE_SCRIPT_FILE%.*}.log"
[[ ! -f "$RCLONE_LOG_FILE" ]] && touch "$RCLONE_LOG_FILE"
####### END SETTINGS #######

echo "$(date "+%d.%m.%Y %T") INFO: *** Starting rclone_upload script for ${RCLONE_DEST_DRIVE} ***" >> "$RCLONE_LOG_FILE"
echo "$(date "+%d.%m.%Y %T") INFO: *** Starting rclone_upload script for ${RCLONE_DEST_DRIVE} ***"

####### Preparing mount location variables #######
echo "$(date "+%d.%m.%Y %T") INFO: *** Rclone move selected.  Files will be moved from ${RCLONE_SRC_DIR} to ${RCLONE_DEST_DRIVE} ***" >> $RCLONE_LOG_FILE
echo "$(date "+%d.%m.%Y %T") INFO: *** Rclone move selected.  Files will be moved from ${RCLONE_SRC_DIR} to ${RCLONE_DEST_DRIVE} ***"
mkdir -p /opt/rclone/remotes/"$RCLONE_DEST_DRIVE" #for script files
#######  Check if script already running  ##########
echo "$(date "+%d.%m.%Y %T") INFO: *** Starting rclone_upload script for ${RCLONE_DEST_DRIVE} ***" >> $RCLONE_LOG_FILE
echo "$(date "+%d.%m.%Y %T") INFO: *** Starting rclone_upload script for ${RCLONE_DEST_DRIVE} ***"
if [[ -f "/opt/rclone/remotes/$RCLONE_DEST_DRIVE/upload_running$JOB_NAME" ]]; then
	echo "$(date "+%d.%m.%Y %T") INFO: Exiting as script already running." >> $RCLONE_LOG_FILE
	echo "$(date "+%d.%m.%Y %T") INFO: Exiting as script already running."
	exit
else
	echo "$(date "+%d.%m.%Y %T") INFO: Script not running - proceeding." >> $RCLONE_LOG_FILE
	echo "$(date "+%d.%m.%Y %T") INFO: Script not running - proceeding."
	touch /opt/rclone/remotes/$RCLONE_DEST_DRIVE/upload_running$JOB_NAME
fi

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

#######  Upload files  ##########
#  Remove --delete-empty-src-dirs if rclone sync or copy
if [[  $RCLONE_COMMAND == 'move' ]]; then
	echo "$(date "+%d.%m.%Y %T") INFO: *** Using rclone move - will add --delete-empty-src-dirs to upload." >> $RCLONE_LOG_FILE
	echo "$(date "+%d.%m.%Y %T") INFO: *** Using rclone move - will add --delete-empty-src-dirs to upload."
	RCLONE_DELETE_EMPTY="--delete-empty-src-dirs "
else
	echo "$(date "+%d.%m.%Y %T") INFO: *** Not using rclone move - will remove --delete-empty-src-dirs to upload." >> $RCLONE_LOG_FILE
	echo "$(date "+%d.%m.%Y %T") INFO: *** Not using rclone move - will remove --delete-empty-src-dirs to upload."
	RCLONE_DELETE_EMPTY=""
fi

rclone $RCLONE_COMMAND $RCLONE_SRC_DIR $RCLONE_DEST_DRIVE:$RCLONE_DEST_DIR \
	--user-agent="$RCLONE_DEST_DRIVE" \
	--log-level=$RCLONE_LOG_LEVEL \
	--buffer-size 512M \
	--drive-chunk-size 512M \
	--min-age $RCLONE_MINIMUM_AGE \
	--order-by modtime,$RCLONE_MOD_SORT \
	$RCLONE_CMD_1 $RCLONE_CMD_2 $RCLONE_CMD_3 $RCLONE_CMD_4  \
	--exclude *fuse_hidden* \
	--exclude *_HIDDEN \
	--exclude .recycle** \
	--exclude .Recycle.Bin/** \
	--exclude *.backup~* \
	--exclude *.partial~* \
	--exclude _* \
	$RCLONE_DELETE_EMPTY


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

exit
