#!/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 ":3124 "`
a=`ls /proc/scsi | grep "SiI6524"`
a=$a$c
echo "$a"
if [ "$a" == "" ] ;then
  sii="sii6514"
  echo "Moving Open Source Libata SATA modules to /tmp (not needed)"
  mv /lib/modules/2.6*smp/kernel/drivers/scsi/libata.ko /tmp/libata.ko
  mv /lib/modules/2.6*smp/kernel/drivers/scsi/sata_sil.ko /tmp/sata_sil.ko
  if [ "$b" == "" ] ;then
     siio="$sii.o"
  else
     siio="$sii.ko"
  fi
else
  sii="sii6524"
  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.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"
  mkdir /tmp/cpio
  (
    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"
  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 $sii_dir/$siio: $sii_dir/scsi_mod.o"
      else
         echo "$sii_dir/$siio: $sii_dir/scsi_mod.ko" >>$modules_dep
         echo "NEW LINE $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"
  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 #
#######################################
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
      $DISK/usr/sbin/chroot $disk /sbin/mkinitrd --with=scsi_mod --with=$sii /boot/$initrd $dir
    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
