#!/bin/bash
#
# This script tests the presence and performance of
# the applesmc module on Apple mactel computers.
#
# kosumi68@ubuntuforums.org
#

if [ `whoami` != "root" ]; then
    echo "script must be run as root (run with sudo)"
    exit 1
fi

dev=/sys/devices/platform/applesmc.768

echo -n "model: "
dmidecode -s system-product-name
echo

# Accelerometer
echo -n "accelerometer:"
if [ -a $dev/position ]; then
    echo -n " present"
    out=`cat $dev/position 2>/dev/null`
    if [ $? = 0 ]; then
        echo -n ", readable, output: $out"
    else
        echo -n ", not readable"
    fi
else
    echo -n " not present"
fi
echo

# Fan - only check first fan
echo -n "fan:          "
if [ -a $dev/fan1_output ]; then
    echo -n " present"
    out=`cat $dev/fan1_output 2>/dev/null`
    if [ $? = 0 ]; then
        echo -n ", readable, output: $out"
    else
        echo -n ", not readable"
    fi
else
    echo -n " not present"
fi
echo

# Light
echo -n "light:        "
if [ -a $dev/light ]; then
    echo -n " present"
    out=`cat $dev/light 2>/dev/null`
    if [ $? = 0 ]; then
        echo -n ", readable, output: $out"
    else
        echo -n ", not readable"
    fi
else
    echo -n " not present"
fi
echo

# Leds
leds=`ls $dev/leds/*/brightness 2>/dev/null`
echo -n "leds:         "
if [ "$leds" != "" ]; then
    echo " present,"
    for x in $leds; do
        echo -n " led:                   "
        out=`cat $x 2>/dev/null`
        if [ $? = 0 ]; then
            echo -n "readable, output: $out"
        else
            echo -n "not readable"
        fi
        echo
    done
else
    echo " not present"
fi

# Temperatures
temps=`ls $dev/temp*input 2>/dev/null`
echo -n "temperatures: "
if [ "$temps" != "" ]; then
    echo " present,"
    for x in $temps; do
        echo -n " temperature:           "
        out=`cat $x 2>/dev/null`
        if [ $? = 0 ]; then
            echo -n "readable, output: $out"
        else
            echo -n "not readable"
        fi
        echo
    done
else
    echo " not present"
fi

# Performance
echo -n "performance:   "
if [ "$temps" != "" ]; then
    echo "reading all temperatures"
    typeset -i i=0
    typeset -i e=0
    typeset -i n=500
    typeset -i t1=`date +%s`
    while [ $i -lt $n ]; do
        out=`cat $temps 2>/dev/null`
        if [ $? != 0 ]; then
            e=$e+1
        fi
        i=$i+1
    done
    typeset -i t2=`date +%s`
    fail=`echo scale=3';' $e/$n | bc`
    speed=`echo $n/'('$t2-$t1')' | bc`
    echo " fail frequency:       " $fail
    echo " reads per second:     " $speed
else
    echo " not performed"
fi
echo

cd $dev

# Fan control version
echo -n "smc-fan: "

typeset -i mt_ix=0
typeset -i sf_ix=0
typeset -i ix=1
typeset -i ixmax=`cat key_count`

# find the index of the Mt and Sf registers
while [ $ix -lt $ixmax ]; do
    echo -n $ix > key_at_index
    name=`cat key_at_index_name 2> /dev/null`
    if [ "$name" = "F0Mt" ]; then
	echo -n " Mt"
	mt_ix=$ix
    fi
    if [ "$name" = "F0Sf" ]; then
	echo -n " Sf"
	sf_ix=$ix
    fi
    ix=$ix+1
done

if [ $mt_ix = 0 -a $sf_ix != 0 ]; then
    echo -n " type A"
fi

if [ $mt_ix != 0 -a $sf_ix != 0 ]; then
    echo -n " type B"
fi

if [ $mt_ix != 0 -a $sf_ix = 0 ]; then
    echo -n " type C"
fi
echo
