#!/bin/ash
# $Id: installer-boot,v 1.34 2002/10/08 16:46:22 malekith Exp $

# this script is responsible for installing bootloader

##########################################
################# main ###################
##########################################

set -e 
#set -x

script_name=$0
PATH=$PATH:. . installer-functions

find_config_paths $@
load_config

is_dest_mounted

# given a partition name return its disk
strip_partition_number () {
    case "$1" in
      /dev/*/c[0-7]d[0-9]* )
        # hardware raid
        echo $1 | sed -e 's/p[0-9]*$//' || :
       ;;
      *) 
        echo $1 | sed -e 's/[0-9]*$//' || :
        ;;
    esac
}

dest_root_partition=`grep ' /dest ' /proc/mounts | sed 's/ .*$//' || : `
dest_boot_partition=`grep ' /dest/boot ' /proc/mounts | sed 's/ .*$//' || : `
test "$dest_boot_partition" || dest_boot_partition=$dest_root_partition || :

case "$boot_loader_device" in
  auto )
    dest_boot_device=$(strip_partition_number $dest_boot_partition)
    ;;
  /dev/* )
    dest_boot_device="$boot_loader_device"
    ;;
  * )
    die "$(nls "bad boot_loader_device: '%s'" "$boot_loader_device")"
    ;;
esac

log info $(nls "dest_boot_device = %s" "$dest_boot_device")

boot_disk=$(strip_partition_number $dest_boot_device)
if test -x /bin/parted ; then
  # hmm.. what if we havn't got parted?
  if parted -s $boot_disk print | grep -q "[ 	]boot" ; then
    : ok
  else
    log warn "$(nls "No partition is active on %s. Making 1st partition active" "$boot_disk")"
    parted -s $boot_disk set 1 boot on || :
  fi
fi

case "$boot_loader" in
  "" | none )
    # user do not want bootloader
    log info "boot_loader variable is not set in $conffile, omitting installation of bootloader"
    ;;
    
  yaboot )
    chroot /dest /sbin/yabootconfig || die "Installation of bootloader failed"
    ;;
    
  rc-boot )
    # configure rc-boot to run
    bootconfig=sysconfig/rc-boot/config
    imageconfig=sysconfig/rc-boot/images/pld
    #set_var $bootconfig LOADER $(echo $boot_loader | sed 's/-.*//')
    set_var $bootconfig DOIT yes
    set_var $bootconfig BOOT $dest_boot_device
    set_var $bootconfig STAGE2 auto
    set_var $bootconfig DEFAULT pld
    set_var $imageconfig TYPE linux
    set_var $imageconfig ROOT auto
    set_var $imageconfig KERNEL /boot/vmlinuz
    set_var $imageconfig INITRD /boot/initrd
    set_var $imageconfig TITLE "PLD Linux 1.0 (Ra)"

    for img in $boot_loader_other ; do
      eval $(parse_boot_loader_entry $img)
      imageconfig=sysconfig/rc-boot/images/$img_label
      set_var $imageconfig TYPE $img_os
      set_var $imageconfig ROOT $img_root
      set_var $imageconfig KERNEL "$img_kernel"
      set_var $imageconfig INITRD "$img_initrd"
    done
    
    #kernel_ver=$(chroot /dest rpm -q kernel --queryformat '%{VERSION}-%{RELEASE}')
    #set_var $imageconfig KERNEL /boot/vmlinuz-$kernel_ver
    #set_var $imageconfig INITRD /boot/initrd-$kernel_ver.gz
    #mv /dest/etc/$imageconfig /dest/etc/sysconfig/rc-boot/images/pld-$kernel_ver
    # install bootloader
    log warn "`nls "Installing bootloader (%s)" "$boot_loader"`"
    chroot /dest /sbin/rc-boot || die "Installation of bootloader failed"
    ;;

  lilo )
    cat >/dest/etc/lilo.conf <<EOF
lba32
boot = $dest_boot_device
map = /boot/map
install = /boot/boot.b
image = /boot/vmlinuz
	initrd = /boot/initrd
	label = pld
	root = $dest_root_partition
	read-only
	
EOF

    for img in $boot_loader_other ; do
      eval $(parse_boot_loader_entry $img)
      case $img_os in
      linux )
      	echo "image = $img_kernel" >> /dest/etc/lilo.conf
	if [ "$img_initrd" ] ; then
	  echo "	initrd = $img_initrd" >> /dest/etc/lilo.conf
	fi
	  cat >> /dest/etc/lilo.conf <<EOF
	label=$img_label
	root=$img_root
	read-only
	
EOF
	  ;;
      dos | bsd )
	  cat >> /dest/etc/lilo.conf <<EOF
other=$img_root
	label=$img_label
	
EOF
	  ;;
      evil )
        die "evil bootloader entry"
	;;
	 
      esac
    done
    
    log warn "`nls "Installing bootloader (%s)" "$boot_loader"`"
    log_wrap chroot /dest lilo || die "Installation of bootloader failed"
    ;;

  * )
    die "$(nls "Bad bootloader: %s" "$boot_loader")"
    ;;
esac

# vim:ft=sh
