#!/bin/bash

CurrentTime=$(date '+%H%M%S')
OverrideFile="/mnt/user/Cloud/gdrive/bw.chk"
#Returns the current rclone bandwidth and removes the double quotes pre/suffix
#rclone rc core/bwlimit returns rate in json form, jq '.rate' extract the rate value from json, sed -e 's/^"//' -e 's/"$//'` removes double quotes
Current_Bw=`rclone rc --rc-addr=192.168.10.222:5573 --rc-user=cartache --rc-pass=Cartache1X core/bwlimit | jq '.rate' | sed -e 's/^"//' -e 's/"$//'`
#Current_Bw=$(<$BwFile)

#If override file is not present set bandwidth according to time else set it to 10K
if [ ! -e "$OverrideFile" ] ; then
  if [ $CurrentTime -ge 233000 ] && [ $CurrentTime -le 75900 ]; then
    Bw="15M"
  elif [ $CurrentTime -ge 80000 ] && [ $CurrentTime -le 170000 ]; then
    Bw="8M"
  elif [ $CurrentTime -ge 170100 ] && [ $CurrentTime -le 232900 ]; then
    Bw="2M"
  fi
else
  Bw="10K"
fi

if [[ $Current_Bw != $Bw ]] ; then
  echo "$(date +%H:%M) - Bandwidth set to $Bw" >> /mnt/user/logs/bw.log
  rclone rc --rc-addr=192.168.10.222:5573 core/bwlimit --rc-user=cartache --rc-pass=Cartache1X rate=$Bw
#  echo "Current=$Current_Bw & New=$Bw"
fi
