#/bin/ash

export LD_LIBRARY_PATH="/mnt/lib:/mnt/usr/lib"
export PATH="/bin:/sbin:/usr/bin:/usr/sbin:/mnt/bin:/mnt/sbin:/mnt/usr/bin:/mnt/usr/sbin"

[ -r /tmp/power.log ] || exit

[ `tail -n 1 /tmp/power.log|cut -d '.' -f 1` -gt 750 ] && exit

case $1 in
	3h) p=3hour;;
	1d) p=day;;
	3d) p=3day;;
	1w) p=week;;
	3w) p=3week;;
	3m) p=3month;;
	1y) p=year;;
	*) exit;;
esac

/mnt/usr/bin/rrdtool graph /tmp/www/power/images/$p.png -s now-$1 -e now \
	-t "PowerUsage @ home" \
        -v Watt \
        -w 800 -h 200 \
	--units-exponent 0 \
	--lower-limit 0 \
        DEF:power=/tmp/power.rrd:house:AVERAGE \
        "CDEF:Watt=power" \
        "CDEF:Low=Watt,0,250,LIMIT" \
        "CDEF:Mid=Watt,250,750,LIMIT" \
        "CDEF:High=Watt,750,1500,LIMIT" \
        "CDEF:VHigh=Watt,1500,5000,LIMIT" \
	-c BACK#000000 -c CANVAS#202020 -c MGRID#00ff00 -c GRID#008000 -c FONT#00ff00 \
	-c AXIS#00ff00 -c ARROW#00ff00 -c SHADEA#000000 -c SHADEB#000000 \
	AREA:Low#00ff00:"0 - 250" \
	AREA:Mid#ffbf00:"250 - 750" \
	AREA:High#FF0000:"750 - 1500" \
	AREA:VHigh#ffffff:"> 1.5kW" \
        "GPRINT:power:MIN:  Min\\: %.2lfW" \
        "GPRINT:power:MAX: Max\\: %.2lfW" \
        "GPRINT:power:AVERAGE: Avg\\: %.2lfW" \
        "GPRINT:power:LAST: Current\\: %.3lfW"

/mnt/usr/bin/rrdtool graph /tmp/www/water/images/$p.png -s now-$1 -e now \
	-t "WaterUsage @ home" \
        -v "l/m" \
        -w 800 -h 200 \
	--units-exponent 0 \
	--lower-limit 0 \
        DEF:water=/tmp/water.rrd:water:AVERAGE \
        "CDEF:flow=water" \
        "CDEF:Low=flow,0,0.250,LIMIT" \
        "CDEF:Mid=flow,0.250,0.750,LIMIT" \
        "CDEF:High=flow,0.750,1.500,LIMIT" \
        "CDEF:VHigh=flow,1.500,5.000,LIMIT" \
	-c BACK#000000 -c CANVAS#202020 -c MGRID#00ff00 -c GRID#008000 -c FONT#00ff00 \
	-c AXIS#00ff00 -c ARROW#00ff00 -c SHADEA#000000 -c SHADEB#000000 \
	AREA:Low#00ff00:"0 - 0.250" \
	AREA:Mid#ffbf00:"0.250 - 0.750" \
	AREA:High#FF0000:"0.750 - 1.500" \
	AREA:VHigh#ffffff:"> 1.5l/m" \
        "GPRINT:water:MIN:  Min\\: %.2lfl/m" \
        "GPRINT:water:MAX: Max\\: %.2lfl/m" \
        "GPRINT:water:AVERAGE: Avg\\: %.2lfl/m" \
        "GPRINT:water:LAST: Current\\: %.3lfl/m"

#cp /tmp/www/power/images/$p.png /mnt/power/images/
#cp /tmp/www/water/images/$p.png /mnt/water/images/
