# **** DEFINE VARIABLES HERE ****

threshold=90		# Should be a whole number representation of a percentage
adminemail="jon@doobla.com"
testmode=1
# ****     END VARIABLES     ****

for site_id in `dsitelookup -a site_handle | sed "s/site//"` ; do
	rollover=`psql appldb -c "select rollover from bandwidth where site_id=$site_id;" | tr -d "[:blank:]" | tr "[:space:]" "," | cut -d, -f3`
	thismonth="`date +%Y-%m`-$rollover"
	
	# echo "<?php echo (strtotime(\"+1 month\",strtotime(\"$startdate\"))-strtotime(\"$startdate\"))/86400; ?>" | php -q
	if [ `date +%d` -lt $rollover ] ; then
		anniversary=`echo "<?php echo strtotime(\"$thismonth\") ; ?>" | php -q`
		lastanniversary=`echo "<?php echo strtotime(\"-1 month\",$anniversary) ; ?>" | php -q`
	else
		lastanniversary=`echo "<?php echo strtotime(\"$thismonth\") ; ?>" | php -q`
		anniversary=`echo "<?php echo strtotime(\"+1 month\",$lastanniversary) ; ?>" | php -q`
	fi
	daysthismonth=`echo "<?php echo ($anniversary-$lastanniversary)/86400; ?>" | php -q` 
	dayssince=`echo "<?php echo (strtotime(date(\"Y-m-d\"))-$lastanniversary)/86400; ?>" | php -q`
	startdate=`echo "<?php echo date(\"Y-m-d\",$lastanniversary); ?>" | php -q` ;startdate="'$startdate'"
	enddate=`echo "<?php echo date(\"Y-m-d\",$anniversary); ?>" | php -q` ; enddate="'$enddate'"
	used=`psql appldb -c "select (sum(in_bytes) + sum(out_bytes)) as total from bandwidth_log where site_id=$site_id and ts >= $startdate and ts < $enddate ;" | tr -d "[:blank:]" | tr "[:space:]" "," | cut -d, -f3` 
	allocated=`psql appldb -c "select threshold from bandwidth where site_id=$site_id;" | tr -d "[:blank:]" | tr "[:space:]" "," | cut -d, -f3`
	domain=`psql appldb -c "select domain from siteinfo where site_id=$site_id;" | tr -d "[:blank:]" | tr "[:space:]" "," | cut -d, -f3`
	if [ "$used" == "" ] ; then 
		used=0
	fi
#	if [ $used -ge `echo "scale=0; $allocated*$threshold/100" | bc` ] ; then
	if [ $used -gt 0 ] && [ $dayssince -gt 0 ] ; then
		dailyaverage=`echo "<?php echo $used/$dayssince; ?>" | php -q`
		esttimetoexhaust=`echo "<?php echo floor($allocated/$dailyaverage)-$dayssince; ?>" | php -q`
		email=`psql appldb -c "select email from siteinfo where site_id=$site_id;" | tr -d "[:blank:]" | tr "[:space:]" "," | cut -d, -f3`
		if [ $testmode -eq 1 ] ; then email=$adminemail ; fi
		if [ $esttimetoexhaust -lt 0 ] ; then 
			# Client already past their limit
			echo "Your domain $domain is currently above it's transfer limit for the month. You should consider contacting your provider to upgrade your plan to avoid overage fees." | mail -s "Bandwidth warning for $domain" $email
			list="$list\n$domain"
		else #[ $esttimetoexhaust -ge 0 ] ; then
			# Client has not run out yet but may be approaching their limit
			if [ `echo "<?php echo ($esttimetoexhaust-((int)($daysthismonth+0.5))); ?>" | php -q` -le 0 ] ; then
				# Client is estimated to run out of bandwidth on or before his anniversary
				echo "Your domain $domain is estimated to exceed it's transfer limit for the month in $esttimetoexhaust days.  You should consider contacting your provider to upgrade your plan to avoid overage fees." | mail -s "Bandwidth warning for $domain" $email
				list="$list\n$domain"
#				list="$list\n$domain,$daysthismonth,$dayssince,$dailyaverage,$esttimetoexhaust,$used,$allocated"
			fi
		fi
	fi
done
echo -e "The following domains have been warned about their estimated bandwidth usage being over their limit before their anniversary:\n$list" | mail -s "Bandwidth warning summary for `hostname`" $adminemail

