#!/bin/sh
echo -n "FastTrak Driver Installation."
if [ ! -f /etc/redhat-release ] ; then
   echo "failed"
   echo "have not chroot or installation not complete"
   exit 1
fi

if [ ! -f modules.cgz ]; then
   echo "failed"
   echo "This is not FastTrak Driver Disk for RedHat Linux"
   exit 1
fi

if [ ! -e /etc/lilo.conf -a ! -e /boot/grub/grub.conf ]; then
   echo "failed"
   echo "RedHat Installation Not Complete"
   exit  1
fi

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

cp modules.cgz /tmp/.ft_mod
if [ $? -ne 0 ]; then
   echo "failed"
   echo "Runtime error..setup abort!!(disk full or no permission?)"
   exit 1
fi
cd /tmp/.ft_mod
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 "."

#backup
if [ ! -f /etc/lilo.conf.orig ]; then
   cp -f /etc/lilo.conf /etc/lilo.conf.orig >/dev/null 2>&1
fi
if [ ! -f /boot/grub/grub.conf.orig ]; then
   cp -f /boot/grub/grub.conf /boot/grub/grub.conf.orig >/dev/null 2>&1
fi
echo -n "."

for i in `\ls -d *`
do
   if [ ! -d /lib/modules/${i} -o ! -f /boot/vmlinuz-${i} ]; then
      continue
   fi

   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 "."

   if [ ! -f /boot/initrd-${i}.ft ] ; then
      /sbin/mkinitrd -f --preload scsi_mod --preload sd_mod --with FastTrak /boot/initrd-${i}.ft ${i} >/dev/null 2>&1
   fi
   echo -n "."

   if [ -f /etc/lilo.conf ]; then
      if ! grep initrd-${i}.ft /etc/lilo.conf >/dev/null 2>&1 ; then
         sed -e '/vmlinuz-'${i}'$/a\' -e '        initrd=/boot/initrd-'${i}'.ft' /etc/lilo.conf > /tmp/.lilo.conf.tmp
         mv -f /tmp/.lilo.conf.tmp /etc/lilo.conf
      fi
   fi
   echo -n "."

   if [ -f /boot/grub/grub.conf ]; then
      if ! grep initrd-${i}.ft /boot/grub/grub.conf >/dev/null 2>&1 ; then
         RD=`awk '/vmlinuz-'${i}' / {print $2}' /boot/grub/grub.conf|sed -e 's/vmlinuz/initrd/'`
         sed -e '/vmlinuz-'${i}' /a\' -e '        initrd '${RD}'.ft' /boot/grub/grub.conf >/tmp/.grub.conf.tmp
         mv -f /tmp/.grub.conf.tmp /boot/grub/grub.conf
      fi
   fi
   echo -n "."

done

if [ -f /etc/lilo.conf ]; then
   /sbin/lilo >/dev/null 2>&1
fi
echo "done"
echo ""
echo "setup is complete."
echo "unmount floppy disk and press Ctrl-Alt-F7 to return back"
echo ""
echo "-------------------------------"
echo "Promise Technology, Inc (C)2002"
