#!/bin/sh
#
# 	$Source: /cvsroot/bootcd/bootcd/bootcdmkinitrd,v $
#       $Id: bootcdmkinitrd,v 1.20 2005/04/21 20:15:17 bs Exp $
#    author: Bernd Schumacher
#     start: long time ago
#     topic: build new initrd
#
#   comment: more informations in man page bootcdmkinitrd(1)
#	

KERNEL=/vmlinuz
INITRD=/initrd.img
ETCMKI=/etc/mkinitrd
ERRLOG=/var/log/errlog

# install the bootloader
INSTBOOTLOAD="yes"

# the next neutral PATH-Prefix is used to mark entries in /etc/mkinitrd/*
# done by bootcdmkinitrd
MARK="/usr/share/bootcd/../../.."

# ---------------------- nothing to configure behind this line ---------------
# include lib
. /usr/share/bootcd/bootcd-run.lib

SCRIPT=""
if [ $# -gt 0 ]; then
  [ "$1" = "-s" ] && SCRIPT="$1"
fi

# Warning
set -u

if [ ! "$SCRIPT" ]; then
  echo "Warning: this script assumes the following:"
  echo "- the running kernel is the one that will be used on bootcd"
  echo "- initrd is used"
  echo "Warning: this script will do the following:"
  echo "- /etc/mkinitrd/ will be changed." 
  echo "- mkinitrd will be called. This will change $INITRD."
  echo "- your bootloader will be called."
  while :; do
    echo -n "OK to continue ? (y|n) "
    read A
    [ "$A" = "n" ] && exit
    [ "$A" = "y" ] && break
  done
fi

# Checks
if [ -f /sbin/grub -a -f /sbin/lilo ]; then
  while :; do
    echo -n "Grub and lilo is installed. Which do you use ? (g|l) "
    read BOOTLOAD
    [ "$BOOTLOAD" = "l" ] && break
    [ "$BOOTLOAD" = "g" ] && break
  done
elif [ -f /sbin/grub ]; then
  BOOTLOAD="g"
  echo "Using grub"
elif [ -f /sbin/lilo ]; then
  BOOTLOAD="l"
  echo "Using lilo"
else
  err "Error: No Bootloader found"
fi

if [ "$BOOTLOAD" = "l" ]; then
  [ ! "$(grep "^[[:blank:]]*initrd=" /etc/lilo.conf)" ] && err "/etc/lilo.conf has no line initrd=. This means that you are not using lilo, or that you are not using initrd. This skript only works with lilo and initrd." 
  
  [ ! "$(grep "^[[:blank:]]*initrd[[:blank:]]*=[[:blank:]]*/initrd.img[[:blank:]]*$" /etc/lilo.conf)" ] && err "Your /etc/lilo.conf has no line with initrd=/initrd.img. Because this script can only support standard configurations, please edit lilo.conf run lilo and reboot to test your configuration. Then run this script again." 
fi

# check for kernel version and initrd-version
k_vers=$(realpath $KERNEL|cut -d"-" -f2-)
[ ! "$k_vers" ] && err "Can't determine the kernel version ... does \"$KERNEL\" exists?"
i_vers=$(realpath $INITRD|cut -d"-" -f2-)
[ ! "$i_vers" ] && err "Can't determine the initrd version ... does \"$INITRD\" exists?"
if [ $(uname -r) != "$k_vers" ]; then
  warn "The running kernel ("$(uname -r)") is not the target kernel ($k_vers), works but don't call lilo!"
  INSTBOOTLOAD="no"
fi

[ "$k_vers" != "$i_vers" ] && err "The kernel version ($k_vers) differs from the initrd version ($i_vers)!"

if [ ! -d $ETCMKI ]; then
  echo "ERROR: No mkinitrd config dir $ETCMKI." >&2
  echo "       Is initrd-tools.deb installed ?" >&2
  exit 1
fi

[ ! -f $INITRD ] && err "$INITRD does not exist."

if [ "$(discover --version 2>/dev/null | grep "discover 2\..*")" ]; then
  DISCOVERV=2
else
  DISCOVERV=1
fi

# Start

[ -f /etc/mkinitrd.tgz ] && mv /etc/mkinitrd.tgz /etc/mkinitrd.tgz.old
(cd /etc; tar czf mkinitrd.tgz mkinitrd)

# modify mkinitrd.conf 
F=/etc/mkinitrd/mkinitrd.conf
touch $F
cp $F $F.tmp
cat $F.tmp  | sed "s/^BUSYBOX=.*/BUSYBOX=yes/" >$F
rm $F.tmp

# modify exe
F=/etc/mkinitrd/exe
touch $F
cp $F $F.tmp
(
cat $F.tmp | grep -v "^$MARK"
[ -f /sbin/discover ] && echo "/sbin/discover"
[ -f /bin/discover ] && echo "/bin/discover"
[ -f /usr/bin/discover ] && echo "/usr/bin/discover"
ls /bin/grep
) | sort -u | sed "s|^|$MARK|" >$F
rm $F.tmp

# modules
F=/etc/mkinitrd/modules
touch $F
cp $F $F.tmp
awk ' 
  !/^cdrom$/ &&
  !/^isofs$/ {
    print
  }
' < $F.tmp >$F
echo "cdrom" >>$F
echo "isofs" >>$F
rm $F.tmp

# files
F=/etc/mkinitrd/files
touch $F
cp $F $F.tmp
(
cat $F.tmp | grep -v "^$MARK"
ls /usr/share/discover/*.lst
ls /usr/share/bootcd/bootcdmodprobe # see prerm
ls /usr/share/bootcd/bootcdproberoot # see prerm
ls /lib/modules/$k_vers/kernel/drivers/usb/host/*.*o # match *.o and *.ko
ls /lib/modules/$k_vers/kernel/drivers/usb/storage/usb-storage.*o
ls /lib/modules/$k_vers/kernel/drivers/cdrom/cdrom.*o
ls /lib/modules/$k_vers/kernel/drivers/scsi/scsi_mod.*o
ls /lib/modules/$k_vers/kernel/fs/nls/*.*o

# extra data for discover 2
for i in libdiscover2 discover-data discover;
do
  [ -f /var/lib/dpkg/info/$i.list ] || continue
  (
  cat /var/lib/dpkg/info/$i.list |
    grep -v "^/usr/share/doc" | # no docu
    grep -v "^/usr/share/man" | # no manpages
    grep -v "^/lib/"          | # libs will be taken by mkinitrd
    grep -v "^/usr/lib/"      | # libs will be taken by mkinitrd
    grep -v "^/bin/"          | # goes to exe if needed
    grep -v "^/usr/bin/"      | # goes to exe if needed
    grep -v "^/sbin/"         | # goes to exe if needed
    grep -v "^/usr/sbin/"       # goes to exe if needed
  cat /var/lib/dpkg/info/$i.list |
    grep "/lib/discover/" # we need discover xml data
  ) |
  while read f; do
    [ -d $f ] && continue # no dirs
    echo "$f"
  done
done
) | sort -u | sed "s|^|$MARK|" >$F
rm $F.tmp

# 50bootcddiscover
F1=/etc/mkinitrd/scripts/50bootcddiscover # see prerm
cat <<end1 >$F1
#!/bin/sh
F2=\$INITRDDIR/scripts/bootcd
cat <<end2 >\$F2
/bin/echo "running /scripts/bootcd" >&2
mount -nt proc proc proc
/usr/share/bootcd/bootcdmodprobe
umount proc
end2
chmod 755 \$F2
end1
chmod 755 $F1

# 60bootcdproberoot
F1=/etc/mkinitrd/scripts/60bootcdproberoot # see prerm
cat <<END1 >$F1
#!/bin/sh

F2=\$INITRDDIR/scripts/getrootdev
cat <<END2 >\$F2

mount -nt proc proc proc
if [ "\\\$(cat /proc/cmdline |grep "root=auto")" ]; then
  echo >&2 "running /scripts/bootcdproberoot"
  mount -nt tmpfs tmpfs dev2 > /dev/null 2>&1 || mount -nt ramfs ramfs dev2

  /usr/share/bootcd/bootcdproberoot

  umount dev2
fi
umount proc
END2
chmod 755 \$F2
END1
chmod 755 $F1

# build initrd
mkinitrd -k -o /initrd.img $k_vers
if [ "INSTBOOTLOAD" == "yes" ]; then 
  [ "$BOOTLOAD" = "l" ] && lilo || update-grub
fi

echo -e "new initrd for kernel version \"$k_vers\" created.\n"

# Warning
[ "INSTBOOTLOAD" == "yes" ] && echo "Please reboot now, before using bootcdwrite"
