#!/bin/sh
patch_rc() {
   if [ ! -f /etc/rc.d/rc.sysinit.orig ] ; then
      cp -f /etc/rc.d/rc.sysinit /etc/rc.d/rc.sysinit.orig
   fi

   if ! grep 'depmod -Aq' /etc/rc.d/rc.sysinit >/dev/null 2>&1 ; then
         sed -e '/depmod -A/s/depmod -A/depmod -Aq/' /etc/rc.d/rc.sysinit >/tmp/.rc.sysinit.tmp
         mv -f /tmp/.rc.sysinit.tmp /etc/rc.d/rc.sysinit
         chmod 755 /etc/rc.d/rc.sysinit
   fi
}

cleanup_driver() {
   find /lib/modules -name 'pdc-ultra.o' -exec rm -f {} \;
}

say_goodbye() {
   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"
}

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

ver=`cat /etc/redhat-release|awk '{print $5}'`

case ${ver} in
    7.3|8.0)
        patch_rc
        echo -n "."
        cleanup_driver 
        echo -n "."
        say_goodbye
        exit
        ;;
    *)
      ;;
esac

if [ ! -f modules.cgz ]; then
   echo "failed"
   echo "This is not SATA150 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/.ut_mod ]; then
   rm -rf /tmp/.ut_mod
fi
mkdir -p /tmp/.ut_mod
echo -n "."

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

patch_rc
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}/pdc-ultra.o /lib/modules/${i}/scsi/ >/dev/null 2>&1
   elif [ ${minor} -eq 4 ]; then
      cp -f ${i}/pdc-ultra.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 pdc-ultra /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

cleanup_driver
echo -n "."

say_goodbye
