#!/bin/sh /etc/rc.common
START=1OO

PROG=/usr/bin/oled

stop() {
	kill -2 $(pgrep /usr/bin/oled)
	kill -9 $(pgrep -f /usr/sbin/netspeed)
	echo "oled exit..."
}

start() {
	enabled=$(uci get oled.@oled[0].enable)
	if [ $enabled -eq 0 ]; then
		exit 0
	fi

	autoswitch=$(uci get oled.@oled[0].autoswitch)
	from=$(uci get oled.@oled[0].from)
	to=$(uci get oled.@oled[0].to)

	if [ ${autoswitch} -eq 1 ]; then
		hour=$(date +"%H")                                                     
                min=$(date +"%M")                                                                      
                ihour=`expr $hour + 0`                                    
                imin=`expr $min + 0`                                                                   
                now=$(($ihour*60+$imin))                                               
                if [[ $now -lt $from || $now -gt $to ]]; then
			stop
			exit 0
		fi		
	fi
	
	#crontab daemon
	if ! grep "/etc/init.d/oled \+restart" /etc/crontabs/root >/dev/null 2>&1; then
		echo "*/5 * * * * /etc/init.d/oled restart >/dev/null 2>&1" >> /etc/crontabs/root
	fi

	date=$(uci get oled.@oled[0].date)
	lanip=$(uci get oled.@oled[0].lanip)
	cputemp=$(uci get oled.@oled[0].cputemp)
	cpufreq=$(uci get oled.@oled[0].cpufreq)
	netspeed=$(uci get oled.@oled[0].netspeed)
	time=$(uci get oled.@oled[0].time)
	drawline=$(uci get oled.@oled[0].drawline)
	drawrect=$(uci get oled.@oled[0].drawrect)
	fillrect=$(uci get oled.@oled[0].fillrect)
	drawcircle=$(uci get oled.@oled[0].drawcircle)
	drawroundrect=$(uci get oled.@oled[0].drawroundrect)
	fillroundrect=$(uci get oled.@oled[0].fillroundrect)
	drawtriangle=$(uci get oled.@oled[0].drawtriangle)
	filltriangle=$(uci get oled.@oled[0].filltriangle)
	displaybitmap=$(uci get oled.@oled[0].displaybitmap)
	displayinvertnormal=$(uci get oled.@oled[0].displayinvertnormal)
	drawbitmapeg=$(uci get oled.@oled[0].drawbitmapeg)
	scroll=$(uci get oled.@oled[0].scroll)
	text=$(uci get oled.@oled[0].text)
	netsource=$(uci get oled.@oled[0].netsource)


	if [ ${netspeed} -eq 1 ]; then
		nohup /usr/sbin/netspeed ${netsource} >/dev/null 2>&1 &
	else
		kill -9 $(pgrep -f /usr/sbin/netspeed)
		rm -f /tmp/netspeed
	fi
	nohup ${PROG} ${date} ${lanip} ${cputemp} ${cpufreq} ${netspeed} ${time} ${drawline} ${drawrect} ${fillrect} ${drawcircle} ${drawroundrect} ${fillroundrect} ${drawtriangle} ${filltriangle} ${displaybitmap} ${displayinvertnormal}  ${drawbitmapeg} ${scroll} "${text}" "${netsource}" 1 > /dev/null 2>&1 &
}

 


restart(){
	enabled=$(uci get oled.@oled[0].enable)
	pgrep -f ${PROG} >/dev/null
	if [ $? -eq 0 ]; then
		if [ $enabled -eq 1 ]; then
			autoswitch=$(uci get oled.@oled[0].autoswitch)
			from=$(uci get oled.@oled[0].from)
			to=$(uci get oled.@oled[0].to)
			if [ ${autoswitch} -eq 1 ]; then
				hour=$(date +"%H")                                                     
                                min=$(date +"%M")                                      
                                ihour=`expr $hour + 0`                                                 
                                imin=`expr $min + 0`                      
                                now=$(($ihour*60+$imin))                                               
                                if [[ $now -lt $from || $now -gt $to ]]; then
					stop
					exit 0
				fi		
			fi

			date=$(uci get oled.@oled[0].date)
			lanip=$(uci get oled.@oled[0].lanip)
			cputemp=$(uci get oled.@oled[0].cputemp)
			cpufreq=$(uci get oled.@oled[0].cpufreq)
			netspeed=$(uci get oled.@oled[0].netspeed)
			time=$(uci get oled.@oled[0].time)
			drawline=$(uci get oled.@oled[0].drawline)
			drawrect=$(uci get oled.@oled[0].drawrect)
			fillrect=$(uci get oled.@oled[0].fillrect)
			drawcircle=$(uci get oled.@oled[0].drawcircle)
			drawroundrect=$(uci get oled.@oled[0].drawroundrect)
			fillroundrect=$(uci get oled.@oled[0].fillroundrect)
			drawtriangle=$(uci get oled.@oled[0].drawtriangle)
			filltriangle=$(uci get oled.@oled[0].filltriangle)
			displaybitmap=$(uci get oled.@oled[0].displaybitmap)
			displayinvertnormal=$(uci get oled.@oled[0].displayinvertnormal)
			drawbitmapeg=$(uci get oled.@oled[0].drawbitmapeg)
			scroll=$(uci get oled.@oled[0].scroll)
			text=$(uci get oled.@oled[0].text)
			netsource=$(uci get oled.@oled[0].netsource)


			kill -9 $(pgrep /usr/bin/oled)
			kill -9 $(pgrep -f /usr/sbin/netspeed)

			if [ ${netspeed} -eq 1 ]; then
				nohup /usr/sbin/netspeed ${netsource} >/dev/null 2>&1 &
			else
				kill -9 $(pgrep -f /usr/sbin/netspeed)
				rm -f /tmp/netspeed
			fi
			nohup ${PROG} ${date} ${lanip} ${cputemp} ${cpufreq} ${netspeed} ${time} ${drawline} ${drawrect} ${fillrect} ${drawcircle} ${drawroundrect} ${fillroundrect} ${drawtriangle} ${filltriangle} ${displaybitmap} ${displayinvertnormal}  ${drawbitmapeg} ${scroll} "${text}" "${netsource}" 0 > /dev/null 2>&1 &
		else
			stop
		fi

	else
		if [ $enabled -eq 1 ]; then
			start
		else
			exit 0
		fi
	fi	
}

boot(){
	start
}
