#!/bin/sh

# Unload and delete any existing non-RAID 3124 drivers.
sudo  kextunload /System/Library/Extensions/SiliconImage3124u.kext 
sudo rm -rf /System/Library/Extensions/SiliconImage3124u.kext 
sudo  kextunload /System/Library/Extensions/SiliconImage3124.kext 
sudo rm -rf /System/Library/Extensions/SiliconImage3124.kext 
sudo rm -rf /usr/sbin/SiCoreService


#  If the SATARAID5 daemon is running the 3124 is probably running on the
#  RAID version of the 3124 driver, so find the daemons's process and terminate it.
#  We need to do this BEFORE attempting to unload the 3124 RAID driver because
#  the driver won't unload if the daemon is holding a reference to it.

r=`ps ax |grep SATARaid5Daemon | grep -v grep | grep -v $$ |grep -v $PPID | grep -v stop | grep -v start | awk '{print $1}'`
if [ "${r}" != "" ] ; then
    sudo /bin/kill -9 $r
fi

#  Now check to see if the RAID 3124 driver is loaded, and if it is, unload it as well.
#  We need to do this because either driver (RAID or non-RAID) can be loaded on any 3124
#  controller.
a=`sudo kextstat | grep com.SiliconImage.driver.Si3124r5`
if [ "$a" != "" ] ; then
    sudo kextunload -b com.SiliconImage.driver.Si3124r5
fi

# If the RAID driver is in the extensions directory we need to remove it from there as well.
if [ -d /System/Library/Extensions/SiliconImage3124r5.kext ] ; then
   sudo rm -r /System/Library/Extensions/SiliconImage3124r5.kext
   sudo touch /System/Library/Extensions
fi
