#!/bin/bash
CONF="/etc/sl-sensors.conf"
DIALOG=${DIALOG=dialog}
tempfile=`tempfile 2>/dev/null` || tempfile=/tmp/test$$
trap "rm -f $tempfile" 0 1 2 5 15
if [ -f /tmp/sensors ]; then
	echo "Sensors is already configured, remove /tmp/sensors to reconfigure!"
        exit 0
else

$DIALOG --backtitle "STRESSLiNUX by STRESSLiNUX.ORG (c) 2oo3 - 2oo9" \
        --title "Hardware Sensors" --clear \
	--radiolist "Please select your vendor/mainboard: " 20 75 10 `/bin/bash /usr/bin/gen-dialog-sensors.sh` 2> $tempfile

retval=$?

choice=`cat $tempfile`
case $retval in
  0)
	clear
	config=$choice
	MODS=`cat $CONF | grep "^$config:"| awk -F: '{print $2}'`
	SCONF=`cat $CONF | grep "^$config:" | awk -F: '{print $4}'`

	LMODS=`lsmod | awk '{print $1}'|wc -l`
	LMC=`expr $LMODS - 2`
	#for RMOD in `lsmod | awk '{print $1}'| tail -n$LMC`
	#do
        #	rmmod $RMOD 2>/dev/null
	#done

	# try to remove only sensor modules
	for RMOD in `ls -1A /lib/modules/\`uname -r\`/kernel/drivers/hwmon/*.ko`
	do
		MODNAME=`basename ${RMOD} .ko`
		rmmod $MODNAME 2>/dev/null
	done
	for RMOD in `ls -1A /lib/modules/\`uname -r\`/kernel/drivers/i2c/chips/*.ko`
	do
		MODNAME=`basename ${RMOD} .ko`
		rmmod $MODNAME 2>/dev/null
	done
	for RMOD in `ls -1A /lib/modules/\`uname -r\`/kernel/drivers/i2c/algos/*.ko`
	do
		MODNAME=`basename ${RMOD} .ko`
		rmmod $MODNAME 2>/dev/null
	done
	for RMOD in `ls -1A /lib/modules/\`uname -r\`/kernel/drivers/i2c/busses/*.ko`
	do
		MODNAME=`basename ${RMOD} .ko`
		rmmod $MODNAME 2>/dev/null
	done


	if [ "`uname -r|awk -F"." '{print $1"."$2}'`" == "2.6" ]; then
		modprobe i2c-sensor 2>/dev/null
	fi
	for MOD in `echo $MODS|sed s/"\/"/" "/g`
	do
	        MPL=`echo $MOD|sed s/"#"/" "/g`
        	modprobe $MPL
	done

	if [ -f /etc/sensors/$SCONF ]; then
        echo "Copying chipset specific sensors.conf ..."
        cp /etc/sensors/$SCONF /etc/sensors.conf
	/usr/bin/sensors -s
	fi
	clear
	echo "Switch to console 12 (ALT+F12 -> tty12) to view voltages and temperatures"
	echo "Values are updated every two seconds."
	touch /tmp/sensors
     ;;
  1)
    echo "Cancel pressed.";;
  255)
    echo "ESC pressed.";;
esac
fi
