#!/bin/sh
#
# download kernel source from kernel.org
#
# prepare the kernel source tree for the building of kernel drivers
KERNEL_V=2.6.24.5
TARNAME=slax6
#WORKING=`pwd`
cd /usr/src
#tar -xzf $WORKING/linux-$KERNEL_V.tar.gz
rm -f linux
ln -s /usr/src/linux-$KERNEL_V /usr/src/linux
rm -f /lib/modules/$KERNEL_V/build
ln -s /usr/src/linux-$KERNEL_V /lib/modules/$KERNEL_V/build
rm -f /lib/modules/$KERNEL_V/source
ln -s /usr/src/linux-$KERNEL_V /lib/modules/$KERNEL_V/source
cd linux-$KERNEL_V
zcat /proc/config.gz > .config
make oldconfig && make prepare
touch /usr/src/linux/include/linux/config.h
# install the PDK in the working directory
cd /opt/deploy/Linux/drivers
# build the drivers needed for the PDK tools
# Note: the build of megaide and megasr will ONLY work for RedHat releases
# Ignore the build errors for the megaide and megasr, they are only
# needed when a software RAID is configured in the BIOS.
tar -xzf pdk-driver-src.tar.gz
#remove exit from smi/build.sh
sed -i -e "/exit 1/d" smi/build.sh
#handle RH point release number in megasr/build.sh
sed -i -e "s/ print \$7 / printf \"%1.0f\",\$7 /" megasr/build.sh
#add extern statement to megasr/build.sh
sed -i -e "s/BOOLEAN/extern VOID mega_check_drive_insertion_removal(HW_DEVICE_EXTENSION *);\nBOOLEAN/" megaide/megaide/megaide_ata.c
#define 'IMG' in megaide/build.sh
sed -i -e "s/cp -f  megaide.\$IMG/if [ \"\$KBASE\" = \"2.4\" ]; then IMG=o; else IMG=ko; fi;cp -f  megaide.\$IMG/" megaide/build.sh

sh build.sh
# remove the source files and build scripts that are no longer needed
#rm -rf dmiedit imbdrv iqv megaide megasr ofu smi
# smidrv and imbdrv scripts are needed to install the driver
tar -xzf drivers-slax6.tar.gz ./smidrv
tar -xzf drivers-slax6.tar.gz ./imbdrv
sed -i -e "s/bash/sh/" imbdrv
#rm -f *.sh *.txt *.tar.gz
#create a tar file of the newly built drivers
#tar -czf drivers-$TARNAME.tar.gz smidrv imbdrv amifldrv_mod.o amifldrv_mod.o-dmiedit imb.ko iqvlinux.ko sg.ko smi.ko megaide.ko megasr.ko
tar -czf drivers-$TARNAME.tar.gz smidrv imbdrv amifldrv_mod.o amifldrv_mod.o-dmiedit imb.ko iqvlinux.ko sg.ko smi.ko
if [ $? -ne 0 ]
then
	echo "PDK driver build failed"
	exit 1
fi
