#!/bin/bash

CurrentTime=`date +%k%M`
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 -gt 800 -a $CurrentTime -le 1700 ]; then
    Bw="7M"
  elif [ $CurrentTime -gt 1700 -a $CurrentTime -le 2330 ]; then
    Bw="2M"
  elif [ $CurrentTime -gt 2330 -a $CurrentTime -le 800 ]; then
    Bw="15M"
  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