#!/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.
#
### BEGIN INIT INFO
# Provides:       mshdd
# Required-Start: $syslog $time
# X-UnitedLinux-Should-Start: $time
# Required-Stop:  $syslog
# Default-Start:  2 3 5
# Default-Stop:   0 1 6
# Description:    MSHD service
### END INIT INFO

# Source SuSE config
. /etc/rc.status

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  '{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
}

test -x $monitor || exit 5

# Shell functions sourced from /etc/rc.status:
#      rc_check         check and set local and overall rc status
#      rc_status        check and set local and overall rc status
#      rc_status -v     ditto but be verbose in local rc status
#      rc_status -v -r  ditto and clear the local rc status
#      rc_failed        set local and overall rc status to failed
#      rc_failed <num>  set local and overall rc status to <num><num>
#      rc_reset         clear local rc status (overall remains)
#      rc_exit          exit appropriate to overall rc status
. /etc/rc.status

# First reset status of this service
rc_reset

# Return values acc. to LSB for all commands but status:
# 0 - success
# 1 - generic or unspecified error
# 2 - invalid or excess argument(s)
# 3 - unimplemented feature (e.g. "reload")
# 4 - insufficient privilege
# 5 - program is not installed
# 6 - program is not configured
# 7 - program is not running
# 
# Note that starting an already running service, stopping
# or restarting a not-running service as well as the restart
# with force-reload (in case signalling is not supported) are
# considered a success.

case "$1" in
    start)
	echo -n "Starting MSHD: "
	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
	## Start daemon with startproc(8). If this fails
	## the echo return value is set appropriate.

	# NOTE: startproc return 0, even if service is 
	# already running to match LSB spec.
	startproc $monitor $threshold $cpunum $max_temperature
	/usr/bin/install-mailaddr new
	# Remember status and be verbose
	rc_status -v
	;;
    stop)
	echo -n "Shutting down MSHD: "
	## Stop daemon with killproc(8) and if this fails
	## set echo the echo return value.

	killproc -TERM $monitor

	# Remember status and be verbose
	rc_status -v
	;;
    restart)
	## Stop the service and regardless of whether it was
	## running or not, start it again.
	$0 stop
	$0 start

	# Remember status and be quiet
	rc_status
	;;
    status)
	echo -n "Checking for MSHD: "
	## Check status with checkproc(8), if process is running
	## checkproc will return with exit status 0.

	# Status has a slightly different for the status command:
	# 0 - service running
	# 1 - service dead, but /var/run/  pid  file exists
	# 2 - service dead, but /var/lock/ lock file exists
	# 3 - service not running

	# NOTE: checkproc returns LSB compliant status values.
	checkproc $monitor
	rc_status -v
	;;
    *)
	echo "usage: $0 [start|stop|restart|status]"
	exit 1
	;;
esac
rc_exit
