#!/bin/bash

# functions for getting hardware information for alterator-lilo
# * places for bootloader (list_bootloader_places, get_default_boot_dev)
# * ...


# for localized lists set
# dec_pt=$(LC_NUMERIC="$in_language" locale decimal_point)

. alterator-sh-functions
. alterator-hw-functions
. grub-raid-boot

quote_udevadm_out='s|^\([^=]\+\)=\(.*\)|\1="\2"|'

LILO_DESTDIR="${LILO_DESTDIR:-}"

list_bootloader_places(){

  detect_raidroot

  if [ -n "${raidboot:-}" ]; then
    write_enum_item "$raidboot" "$(_ "RAID") (${raidboot#/dev/})"
    return 0
  fi

  for i in $(disk_list); do

    ! disk_is_virtual   "$i" || continue

    ! [ -z `echo "$i" | sed -e 's/^sr[0-9]*//'` ] || continue
    ! [ -z `echo "$i" | sed -e 's/^fd[0-9]*//'` ] || continue

    local info="$(disk_info "$i")"

    unset DEVNAME
    eval $(/sbin/udevadm info --name=$i --query=env 2>/dev/null | sed -e "$quote_udevadm_out")

    local size="$(disk_size "$i")"
    size="$(human_readable_size "$size")"

    DEVNAME="${DEVNAME:-/dev/$i}" # on 2.6.24 udevadm will fail
    local sym="$(blockdev_get_symlink "${DEVNAME:-/dev/$i}")"

    write_enum_item "$sym" "$(_ "Hard drive") ${DEVNAME##*/}${info:+, $info}${size:+, $size}"
  done
  local bootdev="$(get_part_with_mntpt "/boot")"
  local rootdev="$(get_part_with_mntpt "/")"
  local dev=${bootdev:-$rootdev}
  [ -z "$dev" ] || write_enum_item "$(blockdev_get_symlink $dev)" "$(_ "Linux partition") (${dev#/dev/})"
}

get_part_with_mntpt(){
  local target="$1"

  local dev mntpt type opts n1 n2
  while read dev mntpt type opts n1 n2; do

    [ "$mntpt" = "$target" ] || continue
    [ "$type" != "rootfs" -a "$type" != "squashfs" ] || continue

    if [ "${dev#UUID=}" != "$dev" ]; then
      for d in $(/sbin/blkid -t "$dev" -s UUID | cut -d' ' -f1); do
        d="${d%:}"
        ! disk_is_virtual "${d#/dev/}" || [ -z "${d##/dev/md*}" ] || continue
        dev="$d"
        break
      done
    fi

    dev="$(readlink -f "$dev")"

    local noevms="${dev#/dev}"
    noevms="${noevms#/evms/md}"
    noevms="/dev${noevms#/evms}"

    [ -b "$noevms" ] && echo "$noevms" || echo "$dev"

  done < "$LILO_DESTDIR/etc/fstab"
}
