#!/usr/bin/bash
DRIVER=hpt37x2
TARGETDIR=/mnt/sysimage
echo "${DRIVER} install script for Turbo Linux 6.5"

function install_stage1
{
	echo "Stage 1 loading ..."
	KERNEL_VER=`uname -r`
	cp -f /mnt/floppy/* /tmp
	insmod /tmp/${DRIVER}-${KERNEL_VER}.o
	echo "Stage 1 finished"
}
function install_stage2
{
	echo "stage2 loading ..."

	if [ ! -f ${TARGETDIR}/etc/lilo.conf ];then
		echo -e "\nError: lilo.conf not found"
		exit 1
	fi

	if [ -d ${TARGETDIR}/lib/modules/2.2.18-2smp ];then
	    KERNEL_VER="2.2.18-2smp"
	else
	    KERNEL_VER="2.2.18-2"
	fi
	cp -f /tmp/${DRIVER}-${KERNEL_VER}.o ${TARGETDIR}/lib/modules/${KERNEL_VER}/scsi/${DRIVER}.o
	cp -f /tmp/vmlinuz-${KERNEL_VER} ${TARGETDIR}/boot/vmlinuz.${DRIVER}
	chroot ${TARGETDIR} /sbin/mkinitrd --with=${DRIVER} /boot/initrd-${KERNEL_VER}.img ${KERNEL_VER}
	mv -f ${TARGETDIR}/etc/lilo.conf ${TARGETDIR}/etc/lilo.conf.old

	cat >/tmp/sedcmd <<EOF
s/^[ 	]*image[ 	]*=[ 	]*\/boot\/vmlinuz.*/image=\/boot\/vmlinuz.${DRIVER}\\
	initrd=\/boot\/initrd-${KERNEL_VER}.img/
EOF

	sed -f /tmp/sedcmd ${TARGETDIR}/etc/lilo.conf.old > ${TARGETDIR}/etc/lilo.conf
	chroot ${TARGETDIR} /sbin/lilo
	echo "stage2 finished"
}

grep -q "${TARGETDIR}" /proc/mounts 
if [ ! $? = '0' ];then
	install_stage1
else
	install_stage2
fi 
