#!/bin/sh
#
#   $Source: /cvsroot/bootcd/bootcd/bootcdproberoot,v $
#       $Id: bootcdproberoot,v 1.8 2010-07-15 13:57:47 bs Exp $
#    author: Bernd Schumacher <bernd.schumacher@hp.com>
# new start: 14.07.2010
#     topic: try to detect the root device and set it as new
#            wait for usb, but stop waiting if device is recognized
#            uses /sys/block/<name>/dev
#

getdsk() 
{ 
  sed -n "s/^.*\<root=\(iso:\)*\([^ :]*\)\>.*$/\2/p" 
}

getiso()
{
  sed -n "s/^.*\<root=iso:\([^ :]*\):\([^ ]*\)\>.*$/\2/p"
}

# for cmdline in \
#   "root=auto" \
#   "root=iso:auto:/mydir/cdimage17.iso" \
#   "root=/dev/sdc" \
#   "root=/dev/sdc1" \
#   "root=iso:/dev/sdc1:/mydir/cdimage17.iso" \
#   "before root=auto behind" \
#   "before root=iso:auto:/mydir/cdimage17.iso behind" \
#   "before root=/dev/sdc behind" \
#   "before root=/dev/sdc1 behind" \
#   "before root=iso:/dev/sdc1:/mydir/cdimage17.iso behind"
# do
#   echo "cmdline=<$cmdline> dsk=<$(echo $cmdline | getdsk)> iso=<$(echo $cmdline | getiso)>"
# done
# exit 0


dsk="$(cat /proc/cmdline | getdsk)"
iso="$(cat /proc/cmdline | getiso)"

# search for this file on the cd
bootcdfile="/etc/bootcd/thisbootcd.conf"
if [ "$iso" ]; then
  bootcdfile="$iso"
fi

ROOT=""
mkdir -p /mnt /dev
for try in 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1; do 
  [ $try -eq 20 ] && echo "bootcdproberoot: searching ..." >&2
  if [ "$dsk" = "auto" ]; then
    # /sys/class/block seems a better option for sata/scsi[?]/usb stuff.
    # /sys/class/block does not exist on 2.6.26-2-amd64
    devnames="$(ls /sys/class/block | grep -e "^sr" -e "^sd" -e "^hd")"
  else
    [ $try -eq 20 ] && echo "bootcdproberoot: only check device from commandline" >&2
    devnames="$(basename $dsk)"
  fi
  for devname in $devnames; do
    echo "bootcdproberoot: searching on devices $devname" >&2

    # try to mount the selected devices to /mnt and look for bootcdfile
    if [ -e /dev/$devname ]; then
      # give other filesystems than iso9660 a chance, but also try iso9660
      # as last option, because auto does not always work
      for i in auto ext2 iso9660; do
        ret=$(mount -t $i -n /dev/$devname /mnt >/dev/null 2>&1; echo $?)
        [ $ret = 0 ] && break
      done
      if [ $ret != 0 ]; then
        echo "bootcdproberoot: $devname is not mountable." >&2
      else
        if [ ! -e "/mnt/$bootcdfile" ]; then 
          echo "bootcdproberoot: $bootcdfile not found on $devname" >&2
           umount /mnt
        else
          echo "bootcdproberoot: found $bootcdfile on $devname" >&2
          ROOT=/dev/$devname
          echo "ROOT=$ROOT" >/conf/param.conf
          umount /mnt
          break 2
        fi
      fi
    fi
  done
  [ $try -eq 20 ] && echo "bootcdproberoot: waiting max 20 sec for usb" >&2
  echo -n "$try " >&2
  sleep 1
done
echo "" >&2
if [ ! "$ROOT" ]; then
  echo "bootcdproberoot: could not find new root device" >&2
  echo "bootcdproberoot: search yourself, try to mount, umount, write result and exit" >&2
  echo "bootcdproberoot: exp: echo \"ROOT=/dev/sr0\" > /conf/param.conf" >&2
  # For DEBUG /bin/sh is enabled
  /bin/sh
fi
