#!/bin/bash

CurrentTime=`date +%k%M`
OverrideFile="/mnt/user/Cloud/gdrive/ApplicationData/watching.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.18:5572 --rc-user=cartache --rc-pass=Cartache1X core/bwlimit | jq '.rate' | sed -e 's/^"//' -e 's/"$//'`
#Current_Bw=$(<$BwFile)


#-------------------------
Day="2M"
Evening="1k"
BeforeMidnight="10M"
Night="15M"
#-------------------------
echo $CurrentTime

#If override file is not present set bandwidth according to time else set it to 10K
if [ ! -e "$OverrideFile" ] ; then
	if [ $CurrentTime -gt 2330 -a $CurrentTime -le 2359 ]; then
		Bw=$BeforeMidnight
	elif [ $CurrentTime -gt 001 -a $CurrentTime -le 830 ]; then
		Bw=$Night
	elif [ $CurrentTime -gt 1700 -a $CurrentTime -le 2330 ]; then
		Bw=$Evening
	elif [ $CurrentTime -gt 830 -a $CurrentTime -le 1700 ]; then
		Bw=$Day
	fi
else
	Bw=$Evening
fi

	echo "Current=$Current_Bw & New=$Bw"

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