#!/bin/bash

SRC=$1
DST=$2
AGE=$3
BW=$4
TAILIP=$(hostname)
RCLONE_CONF_FILE=/opt/rclone/rclone.conf

[ -z "$SRC" ] && SRC=vmx-load:move/series
[ -z "$DST" ] && DST=/mnt/union/series
[ -z "$AGE" ] && AGE=10m
[ -z "$BW" ] && BW=2M

#RCLONE_TAG=series
RCLONE_SRC=$SRC
RCLONE_DST=$DST
RCLONE_EXCLUDE1=".zzz_*"
RCLONE_EXCLUDE2=""
RCLONE_MOVE_MIN_AGE=$AGE
RCLONE_RC_IP=vmx-nas.swallow-grue.ts.net:44444
# RCLONE_RC_IP="$TAILIP".swallow-grue.ts.net:44444
RCLONE_CRED=rcd
RCLONE_TRANSFERS=8
RCLONE_CHECKERS=0
RCLONE_LOG_FILE=/var/log/rclone/rclone_move_rc.log
RCLONE_LOG_LEVEL=DEBUG
[[ ! -f "$RCLONE_LOG_FILE" ]] && touch "$RCLONE_LOG_FILE" && sudo chmod 777 "$RCLONE_LOG_FILE"

if [[ $(pidof -x "$(basename "$0")" -o %PPID) ]]; then cat "Script already running, exiting..." >> $RCLONE_LOG_LEVEL ; exit 0; fi

rclone move "${RCLONE_SRC}" "${RCLONE_DST}" \
--config "$RCLONE_CONF_FILE" \
--delete-empty-src-dirs \
--rc-web-gui --rc \
--rc-addr="${RCLONE_RC_IP}" \
--rc-user="${RCLONE_CRED}" \
--rc-pass="${RCLONE_CRED}" \
--rc-web-gui-no-open-browser \
--rc-web-gui-update=true \
--transfers=8 \
--drive-chunk-size=256M \
--ignore-existing \
--ignore-checksum \
--log-file="${RCLONE_LOG_FILE}" \
--log-level="${RCLONE_LOG_LEVEL}"

/usr/bin/pkill -f "${RCLONE_RC_IP}"