#!/bin/sh

if [ ! -f /usr/sbin/ioreg -o ! -f /usr/sbin/kextstat -o ! -f /usr/sbin/sysctl ]
then
    echo "WARNING: BSD subsystem not installed."
    echo "Skipping installation checks."
    exit 53
fi

echo "Checking OS version..."
if [ "`/usr/sbin/sysctl -n kern.osrelease | cut -d "." -f 1`" \< "7" ]
then
    echo "ERROR: Incompatible operating system version detected."
    exit 112
fi
echo "Darwin version `sysctl -n kern.osrelease` detected."

echo "Checking for other alternative trackpad drivers..."
if /usr/sbin/kextstat | grep com.ragingmenace.driver.SideTrack > /dev/null
then
    echo "ERROR: SideTrack seems to be installed on your machine."
    echo "iScroll2 is not compatible with SideTrack."
    exit 113
fi
if /usr/sbin/kextstat | grep uk.org.outpost.driver.OutpostADBMouse > /dev/null
then
    echo "ERROR: FingaMIDI seems to be installed on your machine."
    echo "iScroll2 is not compatible with FingaMIDI."
    exit 114
fi
echo "No other known alternative drivers detected."

echo "Checking if trackpad driver is loaded..."
if ! (/usr/sbin/kextstat | grep com.apple.driver.AppleADBMouse > /dev/null || \
    /usr/sbin/kextstat | grep name.razzfazz.iScroll2 > /dev/null || \
    /usr/sbin/kextstat | grep name.razzfazz.driver.iScroll2 > /dev/null)
then 
    echo "ERROR: No compatible trackpad driver loaded!"
    exit 115
fi
echo "Apple trackpad driver is loaded."

echo "Checking if trackpad supports W-Enhanced mode..."
if ! (/usr/sbin/ioreg -n AppleADBMouseType4 | grep "W Enhanced Trackpad" > /dev/null || \
    /usr/sbin/ioreg -n iScroll2 | grep "W Enhanced Trackpad" > /dev/null)
then
    echo "ERROR: Trackpad does not seem to support W-Enhanced mode."
    exit 116
fi
echo "Trackpad seems to support W-Enhanced mode."

exit 0
