#!/bin/sh
ok='0'
echo -n "FastTrak Driver Installation."
if [ ! -f modules.cgz ]; then
   echo "failed"
   echo "This is not FastTrak Driver Disk for RedHat Linux"
   exit 1
fi

if [ -d /tmp/.ft_mod ]; then
   rm -rf /tmp/.ft_mod
fi
mkdir -p /tmp/.ft_mod

cp modules.cgz /tmp/.ft_mod/
cd /tmp/.ft_mod
if [ ! -f modules.cgz ]; then
   echo "failed"
   echo "Runtime error..setup abort!!(disk full or no permission?)"
   exit 1
fi
echo -n "."

zcat modules.cgz |cpio -id >/dev/null 2>&1
if [ $? -ne 0 ]; then
   echo "failed"
   echo "Runtime error..setup abort!!(corrupt file)"
   exit 1
fi
echo -n "."

for i in `\ls -d *`
do
    if [ ! -d /lib/modules/${i} ]; then
       continue
    fi
    ok='1'
    
    minor=`echo ${i} | awk -F. '{print $2}'`
    if [ ${minor} -eq 2 ]; then
       cp -f ${i}/FastTrak.o /lib/modules/${i}/scsi/ >/dev/null 2>&1
    elif [ ${minor} -eq 4 ]; then
       cp -f ${i}/FastTrak.o /lib/modules/${i}/kernel/drivers/scsi/ >/dev/null 2>&1
    fi
    echo -n "."

done

if [ ${ok} -ne '1' ] ; then
   echo "failed"
   echo "No suitable driver found"
   echo "make sure driver disk is right"
   exit 1
fi

depmod -a >/dev/null 2>&1
echo -n "."

if [ -f /etc/lilo.conf ]; then
   if [ ! -f /etc/lilo.conf.orig ]; then
      cp -f /etc/lilo.conf /etc/lilo.conf.orig >/dev/null 2>&1
   fi

   if ! grep 'ide9=0' /etc/lilo.conf >/dev/null 2>&1; then
      sed -e '/vmlinuz-2.4/a\' -e '        append="ide0=0x1f0,0x3f6,14 ide1=0x170,0x376,15 ide2=0 ide3=0 ide4=0 ide5=0 ide6=0 ide7=0 ide8=0 ide9=0"' /etc/lilo.conf > /tmp/.lilo.conf.tmp
      cp -f /tmp/.lilo.conf.tmp /etc/lilo.conf
   fi
fi
echo -n "."

if [ -f /boot/grub/grub.conf ]; then
   if [ ! -f /boot/grub/grub.conf.orig ]; then
      cp -f /boot/grub/grub.conf /boot/grub/grub.conf.orig >/dev/null 2>&1
   fi

   if ! grep 'ide9=0' /boot/grub/grub.conf >/dev/null 2>&1; then
      sed -e '/vmlinuz-2/s/$/ ide0=0x1f0,0x3f6,14 ide1=0x170,0x376,15 ide2=0 ide3=0 ide4=0 ide5=0 ide6=0 ide7=0 ide8=0 ide9=0/' /boot/grub/grub.conf >/tmp/.grub.conf.tmp
      cp -f /tmp/.grub.conf.tmp /boot/grub/grub.conf
   fi
fi

if grep 'BOOT_IMAGE' /proc/cmdline >/dev/null 2>&1; then
   lilo >/dev/null 2>&1
fi
echo -n "."

echo "done"

RC=`grep 'rc.modules$' /etc/rc.d/rc.sysinit`
if ! grep '^insmod FastTrak' ${RC} >/dev/null 2>&1; then
   echo -n "Do you want to load FastTrak module when linux booting everytime?(Y/N) "
   read key

   case ${key} in
       Y|y)
           echo "" >> `echo ${RC}`
           echo "#Promise FastTrak Driver" >> `echo ${RC}`
           echo -n 'if [ `uname -r |awk -F. ' >> `echo ${RC}`
           echo -n "'" >> `echo ${RC}`
           echo -n '{print $2}' >> `echo ${RC}`
           echo -n "'" >> `echo ${RC}`
           echo '` -eq 4 ]; then' >> `echo ${RC}`
           echo "   insmod scsi_mod >/dev/null 2>&1" >> `echo ${RC}`
           echo "   insmod sd_mod >/dev/null 2>&1" >> `echo ${RC}`
           echo "fi" >> `echo ${RC}`
           echo "insmod FastTrak >/dev/null 2>&1" >> `echo ${RC}`
           chmod 755 ${RC}
           ;;
       N|n)
           echo ""
           echo "The FastTrak driver module can be loaded automatically"
           echo "by appending the following command to ${RC}"
           echo ""
           echo "   insmod scsi_mod"
           echo "   insmod sd_mod"
           echo "   insmod FastTrak"
           ;;
       *)
         echo ""
         ;;
   esac
fi
echo ""
echo "setup is complete, need to reboot system"
echo ""
echo "-------------------------------"
echo "Promise Technology, Inc (C)2002"
