#!/bin/sh

export KSROOT=/jffs/koolshare
source $KSROOT/scripts/base.sh

eval `dbus export xiaomi_`
temperature=`cat /proc/dmu/temperature | cut -f 3 -d" " | cut -c 1,2 | grep "^[0-9]\{2\}"`
sleep_cron=`cru l | grep xiaomi_sleep | wc -l`

auto_start() {
	if [ $temperature -lt 80 ]; then
		NEWSPEED=2
	elif [ $temperature -ge 80 -a $temperature -lt 90 ];then
		NEWSPEED=3
	elif [ $temperature -ge 90 -a $temperature -lt 100 ];then
		NEWSPEED=4
	else
		NEWSPEED=2
	fi
	OLDSPEED=`nvram get fanctrl_dutycycle`
	if [ $OLDSPEED -ne $NEWSPEED ]; then
		nvram set fanctrl_dutycycle=$NEWSPEED
		nvram commit
	fi 
}

xiaomi_sleep() {
	if [ "$xiaomi_sleep" == "1" ]; then
		if [ ! "$sleep_cron" == "2" ]; then
			cru d xiaomi_start
			cru a xiaomi_start "0 $xiaomi_sleep_start_time * * * /bin/sh $KSROOT/scripts/xiaomi_sleep.sh enable"
			cru d xiaomi_end
			cru a xiaomi_end "0 $xiaomi_sleep_end_time * * * /bin/sh $KSROOT/scripts/xiaomi_sleep.sh disable"
		fi
	else
		cru d xiaomi_start
		cru d xiaomi_end
	fi
}

if [ "$xiaomi_custom_enable" == "1" ]; then
	case $xiaomi_speed in
	"1")
    		nvram set fanctrl_dutycycle=1
    	;;
	"2")
    		nvram set fanctrl_dutycycle=2
    	;;
	"3")
    		nvram set fanctrl_dutycycle=3
   	 ;;
	"4")
    		nvram set fanctrl_dutycycle=4
    	;;
	"5")
		nvram set fanctrl_dutycycle=5
		;;
	esac
	xiaomi_sleep
else
	auto_start
	xiaomi_sleep
fi
