#!/bin/sh
#set -x

backup () {
  local file
  file="$1"
  if [ -f "$file" ] ;then
    if [ ! -f "$file".shipped ] ;then
      mv -f "$file" "$file".shipped
    fi
    cp -f "$file".shipped "$file"
  fi
}

#########################
# not used, remove soon #
#########################
createLilo () {
  local kern
  local lilo
  local style
  kern="$1"
  lilo="$2"

  >$lilo
  echo "prompt" >>$lilo
  echo "timeout=80" >>$lilo
  echo "default=linuxsmp" >>$lilo
  echo "boot=/dev/sda" >>$lilo
  echo "map=/boot/map" >>$lilo
  echo "install=/boot/boot.b" >>$lilo
  echo "message=/boot/message" >>$lilo
  echo "lba32" >>$lilo
  echo "" >>$lilo
  echo "image=/boot/vmlinuz-$kern" >>$lilo
  echo "	label  = linux" >>$lilo
  echo "	initrd = /boot/initrd-$kern.img" >>$lilo
  echo "	read-only" >>$lilo
  echo "	append = \"root=LABEL=/\"" >>$lilo
  for style in smp bigmem ;do
    if [ -f $DISK/boot/vmlinuz-${kern}$style ] ;then
      echo "image=/boot/vmlinuz-${kern}$style" >>$lilo
      echo "	label  = linux$style" >>$lilo
      echo "	initrd = /boot/initrd-${kern}$style.img" >>$lilo
      echo "	read-only" >>$lilo
      echo "	append = \"root=LABEL=/\"" >>$lilo
    fi
  done
}

KERN=`uname -r | sed 's/BOOT//'`
KERN=`echo $KERN | sed 's/smp//'`
KERN=`echo $KERN | sed 's/bigmem//'`
SETCOLOR_SUCCESS="echo -en \\033[1;32m"
SETCOLOR_FAILURE="echo -en \\033[1;31m"
SETCOLOR_WARNING="echo -en \\033[1;33m"
SETCOLOR_NORMAL="echo -en \\033[0;39m"

$SETCOLOR_NORMAL
echo "########################################################"
echo "#         Silicon Image SATARaid controller            #"
echo "########################################################"
echo ""
echo "you are running $KERN version of kernel."

FLOPPY=`mount | grep /dev/fd0`
if [ "$FLOPPY" != "" ] ;then
  FLOPPY=`( set $FLOPPY; echo $3 )`
else
  echo "floppy disk is not found"
  exit 9
fi

#####################################
# find where we are and where we go #
#####################################
if [ -d /mnt/sysimage/etc ] ;then
  DISK="/mnt/sysimage"
elif [ -d /etc/X11 ] ;then
  DISK=""
else
  echo "Unknown system layout"
  exit 9
fi
#echo -n ",,, - "; read a

###########################################
# search for LILO first since Redhat will #
#  always have a grub.conf file, but the  #
#  lilo.conf will only show up when LILO  #
#     is selected for the bootloader      #
###########################################
if [ -f $DISK/etc/lilo.conf ] ;then
  ##############################################
  # rerun in case we had to build a new initrd #
  ##############################################
  chroot $DISK/ /sbin/lilo
else
  #######################################
  # check for a grub configuration file #
  #######################################
  echo "Locating grub files ..."
  if [ -f $DISK/boot/grub/grub.conf ] ;then
    grub_conf="$DISK/boot/grub/grub.conf"
  elif [ -f $DISK/etc/grub.conf ] ;then
    grub_conf="$DISK/etc/grub.conf"
  else
    grub_conf=""
  fi
  if [ "$grub_conf" != "" ] ;then
    #######################################
    # check the PCI info for 3112 or 3512 #
    #######################################
    fixes="1095:3112 1095:3512 SiI.3112 SiI.3512"
    for fix in $fixes ;do
      fix_grub=`cat /proc/pci | grep "$fix"`
      if [ "$fix_grub" != "" ] ;then
        break
      fi
    done
    if [ "$fix_grub" != "" ] ;then
      ##################################
      # we need to keep the IDE driver #
      #  from trying to run the 3112   #
      ##################################
      echo "Turn off BIOS conflict with scsi C: drives ..."
      backup "$grub_conf"
      sed="/tmp/$$.sed"
      >$sed
      echo ",s/root=LABEL=/ide0=0x1f0 ide1=0x170 hde=noprobe hdg=noprobe root=LABEL=/g" >>$sed
      echo "w" >>$sed
      echo "q" >>$sed
      cat $sed | $DISK/bin/ed "$grub_conf"
      rm -f $sed
    fi
    #echo -n ",,, - "; read a
  fi

  ######################################
  # see if we have to boot from a raid #
  ######################################
  echo "Some RAID Configurations require a LOADER upgrade."
  echo "Answer NO to the question below if the System boots OK."
  echo -n "Will the boot disk be a Silicon Image RAID ? [Y or N] - "
  read answer
  if [ "$answer" == "y" -o "$answer" == "Y" ] ;then
    if [ "$grub_conf" != "" ] ;then
      ######################################
      # find where the shipped grub lives  #
      # Redhat is in /sbin,                #
      # SuSE is in /usr/sbin               #
      #                                    #
      # grub_bin does not need $DISK since #
      # grun-install does a CHROOT first   #
      ######################################
      if [ -x $DISK/sbin/grub ] ;then
        grub_bin="/sbin"
      elif [ -x $DISK/usr/sbin/grub ] ;then
        grub_bin="/usr/sbin"
      else
        echo "'grub' not found"
        exit 9
      fi

      #############################
      # find the installed stage1 #
      #############################
      if [ -f $DISK/boot/grub/stage1 ] ;then
        grub_boot="$DISK/boot/grub"
      else
        echo "'boot/grub/stage1' not found"
        exit 9
      fi
      #echo -n ",,, - "; read a

      #########################################
      # copy GRUB .92 to the installed system #
      #########################################
      backup "$DISK/$grub_bin/grub"
      backup "$DISK/$grub_bin/grub-install"
      backup "$grub_boot/stage1"
      backup "$grub_boot/stage2"
      echo "Installing grub files ..."
      mkdir -p $DISK/usr/share/grub/i386-pc
      cp -f $FLOPPY/grub/i386-pc/* $DISK/usr/share/grub/i386-pc
      cp -f $FLOPPY/grub/grub-install $DISK/$grub_bin
      cp -f $FLOPPY/grub/grub $DISK/$grub_bin
      if [ ! -x $DISK/sbin/grub ] ;then
        ln -s $DISK/$grub_bin/grub $DISK/sbin
      fi
      #echo -n ",,, - "; read a

      ############
      # run GRUB #
      ############
      #root=`cat $DISK/etc/grub.conf | grep "boot=/dev"`
      root=`cat $grub_conf | grep "boot=/dev"`
      if [ "$root" != "" ] ;then
        root=`( IFS="="; set $root; echo $2 )`
      else
        root=`df | grep ' /$'`
        root=`( set $root; echo $1 )`
        root=${root:0:`expr ${#root} - 1`}
      fi
      echo "Running grub ... chroot $DISK/ $grub_bin/grub-install \"$root\""
      chroot $DISK/ $grub_bin/grub-install "$root"
      sync
      #echo -n ",,, - "; read a
    else
      echo "No GRUB or LILO config files found."
    fi # found GRUB
  fi # boot from RAID
fi # GRUB - LILO - none
#echo -n ",,, - "; read a

#############################
# delete extra unused files #
#############################
echo "Cleaning up unused modules"
rm -f $DISK/lib/modules/*/kernel/drivers/scsi/sii65*.o

##############
# for Redhat #
##############
rm -f $DISK/lib/modules/*/updates/sii65*.o

$SETCOLOR_WARNING
echo "################################################################"
echo "#  IF you have UNUSUAL CONFIGURATIONS..                         "
echo "#  Make sure Lilo and/or GRUB config file are correct           "
echo "#  Run Lilo again if necessary. Please inspect files visually   "
echo "#  BACKEP UP files are /etc/lilo.bak or /boot/grub/grub.bak     "
echo "################################################################"
$SETCOLOR_SUCCESS
echo "########################################################"
echo "#  Silicon Image, Inc (C) 2003                         #"
echo "########################################################"
$SETCOLOR_NORMAL
