#!/bin/bash
set -e

# RClone Config file
RCLONE_CONFIG=/opt/rclone/rclone.conf; export RCLONE_CONFIG
RCLONE_USER_AGENT=sinedbret_agent; export RCLONE_USER_AGENT
RCLONE_EXCLUDE_FILE=/opt/scripts/rclone/excludes
RCLONE_LOGFILE=/opt/scripts/logs/tdrive_upload.log
# Local Drive - This must be a local mount point on your server that is used for the source of files
# WARNING: If you make this your rclone Google Drive mount, it will create a move loop
# and DELETE YOUR FILES!
# Make sure to set this to the local path you are moving from!!
LOCAL=/mnt/move

# Exit if running
if [[ $(pidof -x "$(basename "$0")" -o %PPID) ]]; then
echo "Already running, exiting..."; exit 1; fi

# Check for excludes file
if [[ ! -f "$RCLONE_EXCLUDE_FILE" ]]; then
echo "excludes file not found, aborting..."; exit 1; fi

# Is $LOCAL actually a local disk?
if /bin/findmnt "$LOCAL" -o FSTYPE -n | grep fuse; then
echo "FUSE file system found, exiting..."; exit 1; fi

# Move older local files to the cloud...
# I added in 3 days to let the files sit a few days so Plex intro anaylsis can happen locally
# /usr/bin/rclone --config "$RCLONE_CONFIG" move "$LOCAL" tdrive: --log-file /opt/scripts/logs/tdrive_upload.log -v --exclude-from /opt/scripts/rclone/excludes --delete-empty-src-dirs --fast-list --drive-stop-on-upload-limit

RCLONE_CMD=rclone move "$LOCAL" tdrive: -v --log-file "$RCLONE_LOGFILE" --delete-empty-src-dirs  --drive-stop-on-upload-limit --exclude-from "$RCLONE_EXCLUDE_FILE"

flock --timeout 3600 /opt/rclone/move_upload.lock --command "$RCLONE_CMD"

#/usr/bin/rclone --config /opt/rclone/rclone.conf move /mnt/move tdrive: -v --fast-list --drive-stop-on-upload-limit
#/usr/bin/rclone move /mnt/move tdrive: --log-file /opt/scripts/logs/tdrive_upload.log -v --exclude-from /opt/scripts/rclone/excludes --delete-empty-src-dirs --fast-list --drive-stop-on-upload-limit
