#!/bin/sh
#set -x
                                                                                
backup () {
  local file
  file="$1"
  if [ -f "$file" ] ;then
    echo "Backup $file ..."
    if [ ! -f "$file.shipped" ] ;then
      mv -f "$file" "$file.shipped"
    fi
    cp -f "$file.shipped" "$file"
  fi
  if [ -f "$file.shipped" ] ;then
    cp -f "$file.shipped" "$file"
  fi
}

FLOPPY=`dirname $0`
FLOPPY=`( cd $FLOPPY; pwd )`

# it should be none only if you need X11 
# to install via Yast (Graphical Interface)
# DISK="--none--"
DISK=""
#####################################
# find where we are and where we go #
#####################################
if [ ! -d /media/floppy ] ;then
  if [ ! -d /mnt/sysimage ] ;then
    mkdir -p /mnt/sysimage
  fi
  if [ ! -d /boot/grub -a ! -f /etc/lilo.conf ] ;then
    mount /dev/sda2 /mnt/sysimage
    if [ $? == 0 ] ;then
      if [ -d /mnt/sysimage/boot ] ;then
        if [ ! -d /mnt/sysimage/boot/grub ] ;then
          mount /dev/sda1 /mnt/sysimage/boot
        fi
      fi
    fi
  fi
fi
if [ -d /mnt/sysimage/etc ] ;then
  DISK="/mnt/sysimage"
elif [ -d /etc/X11 ] ;then
  DISK=""
fi
if [ "$DISK" == "--none--" ] ;then
  echo "Unknown system layout"
  exit 9
fi
#echo -n ",,, - "; read a

#if [ -d /var/adm/mount/linux/suse ] ;then
  ############################################
  # we have been copied to somewhere in /tmp #
  # by YaST control center's Load Vendor CD  #
  ############################################
  #FLOPPY="/var/adm/mount"
#else
  #FLOPPY=`dirname $0`
#fi

##########################
# select the driver name #
##########################
uname -r > /tmp/b
b=`cat /tmp/b | grep "2.6"`
echo "current kernel is $b"
echo "/proc/pci may not exist in some configurations"

c=`cat /proc/pci | grep ":3132 "`
a=`ls /proc/scsi | grep "sii3132"`
a=$a$c
echo "$a"

# 3112 Section (Open Source Collision)
if [ "$a" == "" ] ;then
  sii="sii3112"
  echo "Moving Open Source Libata SATA modules to /tmp (not needed)"
#read nothing;
#SuSE
#Redhat may require even more steps
  mv $DISK/lib/modules/2.6*/kernel/drivers/scsi/libata.ko /tmp/libata.ko
  mv $DISK/lib/modules/2.6*smp/kernel/drivers/scsi/libata.ko /tmp/libata_smp.ko
  mv $DISK/lib/modules/2.6*bigsmp/kernel/drivers/scsi/libata.ko /tmp/libata_smp.ko
  echo "$DISK/lib/modules/2.6/kernel/drivers/scsi/libata.ko /tmp/libata.ko"
  mv $DISK/lib/modules/2.6*/kernel/drivers/scsi/sata_sil.ko /tmp/sata_sil.ko
  mv $DISK/lib/modules/2.6*smp/kernel/drivers/scsi/sata_sil.ko /tmp/sata_sil_smp.ko
  mv $DISK/lib/modules/2.6*bigsmp/kernel/drivers/scsi/sata_sil.ko /tmp/sata_sil_smp.ko
  echo "$DISK/lib/modules/2.6/kernel/drivers/scsi/sata_sil.ko /tmp/sata_sil.ko"

  echo "some modules may not be present.."

  if [ "$b" == "" ] ;then
     siio="$sii.o"
  else
     siio="$sii.ko"
  fi
else
  sii="sii3132"
  if [ "$b" == "" ] ;then
     siio="$sii.o"
  else
     siio="$sii.ko"
  fi
fi
echo "siI is $siio"

############################
# copy driver to boot disk #
#   update kernel files    #
############################

#handle new RH format
if [ -f "$FLOPPY/rhdd" ] ;then
#if [ -d "$DISK/lib/modules/2.4.21-15.EL" ] ;then
  Arch="x86_64"
else
Arch=""
fi

if [ -d "/lib/modules/2.4.21-99-smp4G" ] ;then
  src="i386-9.0SMP4G"
  ver="2.4.21-99-smp4G"
elif [ -d "/lib/modules/2.4.21-99-smp" ] ;then
  src="i386-9.0SMP"
  ver="2.4.21-99-smp"
elif [ -d "/lib/modules/2.4.21-99-athlon" ] ;then
  src="i386-9.0athlon"
  ver="2.4.21-99-athlon"
elif [ -d "/lib/modules/2.4.21-102-default" ] ;then
  src="x86_64-9.0"
  ver="2.4.21-102-default"
elif [ -d "/lib/modules/2.4.21-102-smp" ] ;then
  src="x86_64-9.0SMP"
  ver="2.4.21-102-smp"
elif [ -d "/lib/modules/2.4.19-SMP" ] ;then
  src="x86_64-ul1smp"
  ver="2.4.19-SMP"
elif [ -d "/lib/modules/2.6.5-7.69-smp" ] ;then
  src="x86_64-sles9smp"
  ver="2.6.5-7.69-smp"
elif [ -d "/lib/modules/2.6.5-7.97-bigsmp" ] ;then
  src="i386-sles9bigsmp"
  ver="2.6.5-7.97-bigsmp"
elif [ -d "/lib/modules/2.6.5-7.97-smp" ] ;then
  src="x86_64-sles9smp"
  ver="2.6.5-7.97-smp"
elif [ -d "/lib/modules/2.6.5-7.97-default" ] ;then
  src="x86_64-sles9"
  ver="2.6.5-7.97-default"
elif [ -d "/lib/modules/2.6.5-7.139-bigsmp" ] ;then
  src="i386-sles9bigsmp"
  ver="2.6.5-7.139-bigsmp"
elif [ -d "/lib/modules/2.6.5-7.139-smp" ] ;then
  src="x86_64-sles9smp"
  ver="2.6.5-7.139-smp"
elif [ -d "/lib/modules/2.6.5-7.139-default" ] ;then
  src="x86_64-sles9"
  ver="2.6.5-7.139-default"
elif [ -d "/lib/modules/2.6.8-24-default" ] ;then
  src="i386-sles9"
  ver="2.6.8-24-default"
elif [ -d "/lib/modules/2.6.8-24-bigsmp" ] ;then
  src="i386-sles9bigsmp"
  ver="2.6.8-24-bigsmp"
elif [ -d "/lib/modules/2.6.8-24-smp" ] ;then
  src="x86_64-sles9smp"
  ver="2.6.8-24-smp"
elif [ -d "/lib/modules/2.6.11.4-20a-bigsmp" ] ;then
  src="i386-sles9bigsmp"
  ver="2.6.11.4-20a-bigsmp"
elif [ -d "/lib/modules/2.6.11.4-20a-default" ] ;then
  src="x86_64-sles9"
  ver="2.6.11.4-20a-default"
elif [ -d "/lib/modules/2.6.11.4-20a-smp" ] ;then
  src="x86_64-sles9smp"
  ver="2.6.11.4-20a-smp"
elif [ -d "/lib/modules/2.6.11.4-20a-xen" ] ;then
  src="x86_64-sles9xen"
  ver="2.6.11.4-20a-xen"
elif [ -d "/lib/modules/2.6.5-7.79-smp" ] ;then
  src="i386-sles9smp"
  ver="2.6.5-7.79-smp"
elif [ -d "/lib/modules/2.6.5-7.79-bigsmp" ] ;then
  src="i386-sles9bigsmp"
  ver="2.6.5-7.79-bigsmp"
elif [ -d "/lib/modules/2.4.21-143-smp" ] ;then
  src="x86_64-ul1smp"
  ver="2.4.21-143-smp"
elif [ -d "/lib/modules/2.4.19-64GB-SMP" ] ;then
  src="i386-ul164GB-SMP"
  ver="2.4.19-64GB-SMP"
elif [ -f "$FLOPPY/modules.cgz" ] ;then
  #DISK="/mnt/sysimage"
  img=".img"
  if [ -d "/tmp/cpio" ] ;then
     echo "/tmp/cpio already created" 
  else
     mkdir /tmp/cpio
  fi 
  (
    cd /tmp/cpio
    echo "Unzip drivers ..."
    cat "$FLOPPY/modules.cgz" | gunzip | cpio -ivH crc
    for dir in * ;do
      if [ -d $DISK/lib/modules/$dir ] ;then
        echo "copying $dir/$Arch/$siio to $DISK/lib/modules/$dir/kernel/drivers/scsi"
        cp -f $dir/$Arch/$siio $DISK/lib/modules/$dir/kernel/drivers/scsi
      fi
    done
  )
  rm -rf /tmp/cpio
else
  echo "ERROR: There is no support for the kernel you want. Please modify this script"
  exit 9
fi
#echo -n ",,, - "; read a

dirs=`( cd $DISK/lib/modules; echo * )`
for dir in $dirs ;do
  modules_dep="$DISK/lib/modules/$dir/modules.dep"
  echo "modules_dep is $modules_dep"
  #read nothing
  if [ -f "$modules_dep" ] ;then
    echo "Updating $modules_dep ..."

    ##########################################
    ## backup the origional modules.dep file #
    ##########################################
    #backup $modules_dep
    ##########################################################
    # problem with having unexpected files in /lib/modules/* #
    ##########################################################


    #####################################################
    # add the Sii driver to the modules dependency file #
    #####################################################
    a=`grep $siio $modules_dep`
    #echo "a is $a greping $siio in module_dep=$modules_dep"
    if [ "$a" == "" ] ;then
      echo "Update $modules_dep ..."
      sii_dir="/lib/modules/$dir/kernel/drivers/scsi"
      if [ "$b" == "" ] ;then
         echo "$sii_dir/$siio: $sii_dir/scsi_mod.o " >>$modules_dep
         echo "NEW LINE to modules_dep $sii_dir/$siio: $sii_dir/scsi_mod.o"
      else
         echo "$sii_dir/$siio: $sii_dir/scsi_mod.ko" >>$modules_dep
         echo "NEW LINE to modules_dep $sii_dir/$siio: $sii_dir/scsi_mod.ko"
      fi
    fi
  fi
  #echo -n ",,, - "; read a
done

############################
# copy driver to boot disk #
############################
if [ "$src" != "" ] ;then
  #suse_obj="$FLOPPY/linux/*/$src/modules/$siio"
  suse_obj="$FLOPPY/linux/suse/$src/modules/$siio"
  echo "cp -f $suse_obj $DISK/lib/modules/$ver/kernel/drivers/scsi"
  cp -f $suse_obj $DISK/lib/modules/$ver/kernel/drivers/scsi
  #echo -n ",,, - "; read a

  #####################################
  # backup and modify the kernel file #
  #####################################
  backup $DISK/etc/sysconfig/kernel
  a=`grep $sii $DISK/etc/sysconfig/kernel`
  if [ "$a" == "" ] ;then
    echo "Update $DISK/etc/sysconfig/kernel ..."
    sed -f $FLOPPY/addsii.sed $DISK/etc/sysconfig/kernel > /tmp/kertmp
    mv -f /tmp/kertmp $DISK/etc/sysconfig/kernel
  fi
  #echo -n ",,, - "; read a
fi
#echo -n ",,, - "; read a

#########################################
# backup and modify the bootloader file #
#########################################
# this will try to force LILO #
# which is not needed anymore #
###############################
#backup /etc/sysconfig/bootloader
#sed -f $FLOPPY/add2sii.sed /etc/sysconfig/bootloader > /tmp/kertmp2
#mv -f /tmp/kertmp2 /etc/sysconfig/bootloader

#######################################
# backup and generate the initrd file #
#######################################
uname -r > /tmp/v
v=`cat /tmp/b | grep "2.6"`
#special handling for 2.6 kernels

dirs=`( cd $DISK/lib/modules; echo * )`
for dir in $dirs ;do
  initrd="initrd-$dir$img"
  if [ -d $DISK/lib/modules/$dir/kernel ] ;then
    sysmap="System.map-$dir"
    backup $DISK/boot/$initrd
    backup $DISK/boot/$sysmap
    #echo -n ",,, - "; read a
    echo "mkinitrd $initrd ..."
    if [ "$src" == "" ] ;then
      ##################
      # Redhat version #
      ##################
      rm -f $DISK/boot/$initrd
      if [ "$DISK" == "" ] ;then
        disk="/"
      else
        disk="$DISK"
      fi
      if [ "$v" == "" ] ;then
	 # 2.4 kernels
         $DISK/usr/sbin/chroot $disk /sbin/mkinitrd --with=scsi_mod --with=$sii /boot/$initrd $dir
      else
         echo "RedHat 2.6 kernel"
         # x86_64 echo "$DISK/usr/sbin/chroot $disk /sbin/mkinitrd --omit-scsi-modules --with=scsi_mod --with=sd_mod --with=$sii /boot/$initrd $dir"
         echo "$DISK/usr/sbin/chroot $disk /sbin/mkinitrd --omit-scsi-modules --with=scsi_mod --with=sd_mod --with=sr_mod --with=$sii /boot/$initrd $dir"
         $DISK/usr/sbin/chroot $disk /sbin/mkinitrd --omit-scsi-modules --with=scsi_mod --with=sd_mod --with=sr_mod --with=$sii /boot/$initrd $dir
	 #read nothing
      fi
    else
      ################
      # SuSE version #
      ################
      ( cd /boot; /sbin/mkinitrd )
    fi
  fi
  #echo -n ",,, - "; read a
done

##########################
# update the boot loader #
##########################
#echo "The next section updates the bootloader"
#echo "This section is not normally required"
#echo "Modify the script if you need it"
#echo "DONE"

#cp -f $FLOPPY/special_update_bootloader.sh /tmp
#chmod 775 /tmp/special_update_bootloader.sh
#$FLOPPY/special_update_bootloader.sh
