#!/bin/sh
#
# Author: Xiang-Yu Wang <rain.wang@mic.com.tw>
#
# Copyright (c) 2003, MiTAC Inc.
#
# This code is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# chkconfig: 2345 20 80
# description: MSHD service
#


# source function library
. /etc/init.d/functions

monitor=/usr/bin/mshd
configf=/etc/mshd.conf
options="init=0"
threshold="53 57 48 53 67 72 60 68"
cpunum="1"
max_temperature="70"
cpu_frequence() {
	Frequence=$(grep  "model name" /proc/cpuinfo | awk -F'\ ' '{print $7;}')
	case "$Frequence" in
	2.26GHz)
		echo 70   
	    ;;
	2.40GHz)
		echo 70
		;;
	2.53GHz)
		echo 71
		;;
	2.66GHz)
		echo 74
		;;
	2.80GHz)
		echo 75
		;;
	3.06GHz)
		echo 69
		;;
	*)
		echo 69
	esac
}

cpu_detect () {
	if [ $( grep 'processor' /proc/cpuinfo | wc -l ) -eq 2 ]; then
		echo 1
	elif [ $( grep 'processor' /proc/cpuinfo | wc -l ) -eq 4 ]; then
		echo 2
	else
		exit 1
	fi
}

start () {
	echo -n "Starting MSHD: "
	if [ -n "$( status $monitor | grep 'is running' )" ]; then
		success
		echo
		return $?
	fi
	options=$( grep '# options ' $configf | sed 's/# options //g' )
	/sbin/modprobe i2c-i801 
	/sbin/modprobe w83781d $options
	echo "4 4 8" > /proc/sys/dev/sensors/w83782d-i2c-0-29/fan_div
	cpunum=$( cpu_detect )
	max_temperature=$( cpu_frequence )
	if [ -f $configf ]; then
		if [ -n "$( grep -v '#' $configf )" ]; then
			threshold=$( grep -v '#' $configf )
		fi
	fi
	daemon $monitor $threshold $cpunum $max_temperature
	/usr/bin/install-mailaddr new
	echo 
	return $?
}

stop () {
	echo -n "Shutting down MSHD: "
	killproc $monitor
	echo 
	return $?
}

case "$1" in
start)
	start
	;;
stop)
	stop
	;;
restart)
	$0 stop
	$0 start
	;;
status)
	status $monitor
	;;
else)
	cpu_frequence
	;;
*)
	echo "usage: $0 [start|stop|restart|status]"
	exit 1
esac

exit $?
