#!/bin/bash

# Name: make_initrd
# Copyright: (c)2003-2006 Hewlett-Packard Company
#
# Description: creates a new initrd
#
# CD	11/17/03	Initial Development
# CD	01/29/04	Added a command line argument to specify
#                       which kernel to create an initrd for
#                       on Red Hat systems
# CD	04/12/04	Add code to look for HP-initinstall initrds
# CD    04/26/04        Add code to look for life keeper kernels "LK"
# CD	06/14/04	Added support for x86_64 architecture
# CD    06/24/04	Force SUSE mkinitrd to only remake the initrd
#                       for the running kernel
# CD	08/17/04	Force all kernels to be built for SLES 8 ia64
# CD	03/16/05	Added code to look for HP-* initrd's
# CD	03/21/05	Added code to look for HP-initrd-* initrd's
# CD	10/02/06	Added code to look for HP-initrd-*.img-<number>
#                       initrd names

# parse command line arguments

if [ "$1" = "" ]
then
 KERNELVERSION=`uname -r`
else
 KERNELVERSION=$1
fi

# do a sanity check to make sure that /lib/modules/KERNELVERSION
# exists

if [ ! -d /lib/modules/$KERNELVERSION ]
then
 echo "/lib/modules/$KERNELVERSION does not exist"
 exit 1
fi

# determine boot directory

if [ "`uname -m`" = "ia64" ]
then
 if [ -d /boot/efi/efi/redhat ]
 then
 	BOOTDIR="/boot/efi/efi/redhat"
 else
 	BOOTDIR="/boot"
 fi
else
 BOOTDIR="/boot"
fi

# determine how to build the initrd based on which
# initrd build command exists

if test -x /sbin/mk_initrd
then
 #
 # initrd build procedure for SUSE systems
 #

 # determine if we are UL 1.0 ia64

 UL10_64=0

 if [ "`cat /etc/issue | grep "UnitedLinux 1.0"`" != "" ] || [ "`cat /etc/issue | grep "SLES 8"`" != "" ]
 then
 	if [ "`uname -m`" = "ia64" ]
	then
		UL10_64=1
	fi
 fi

 if [ $UL10_64 -eq 1 ]
 then
 	# issue a genric mkinitrd to remake all installed ramdisks
	mkinitrd
 else
 	# look for different kernel naming conventions.  Select the
	# first that matches or use the default SUSE naming convention

 	if test -f ${BOOTDIR}/HP-initinstall-initrd-${KERNELVERSION}
 	then
 		INITRDNAME=HP-initinstall-initrd-${KERNELVERSION}
	elif test -f ${BOOTDIR}/HP-${KERNELVERSION}
	then
		INITRDNAME=HP-${KERNELVERSION}
	elif [ "`ls HP-initrd-$KERNELVERSION.img-* 2>/dev/null | grep -v old |  sort | tail -n 1`" != "" ]
	then
        	INITRDNAME=`ls HP-initrd-$KERNELVERSION.img-* | grep -v old |  sort | tail -n 1`
	elif test -f ${BOOTDIR}/HP-initrd-${KERNELVERSION}.img
	then
		INITRDNAME=HP-initrd-${KERNELVERSION}.img
 	elif test -f ${BOOTDIR}/initrd-${KERNELVERSION}LK
 	then
 		INITRDNAME=initrd-${KERNELVERSION}LK
 	else
 		INITRDNAME=initrd-${KERNELVERSION}
 	fi

 	# save old image

 	cp ${BOOTDIR}/${INITRDNAME} ${BOOTDIR}/${INITRDNAME}.old

	# make new initrd

 	mkinitrd -k vmlinuz-${KERNELVERSION} -i $INITRDNAME

 fi
elif [ -x /sbin/mkinitrd ]
then
 #
 # initrd build procedure for Red Hat systems
 #

 # look for different kernel naming conventions.  Select the
 # first that matches or use the default SUSE naming convention

 # This variable is used to tell whether an ".img" is appended to the end
 # of the initrdname

 APPENDIMG="y"

 if test -f ${BOOTDIR}/HP-initinstall-initrd-${KERNELVERSION}.img 
 then
 	INITRDNAME=HP-initinstall-initrd-${KERNELVERSION}
 elif test -f ${BOOTDIR}/HP-${KERNELVERSION}.img
 then
	INITRDNAME=HP-${KERNELVERSION}
 elif [ "`ls HP-initrd-$KERNELVERSION.img-* 2>/dev/null | grep -v old | sort | tail -n 1`" != "" ]
 then
	INITRDNAME=`ls HP-initrd-$KERNELVERSION.img-* 2>/dev/null | grep -v old | sort | tail -n 1`
	APPENDIMG="n"
 elif test -f ${BOOTDIR}/HP-initrd-${KERNELVERSION}.img
 then
 	INITRDNAME=HP-initrd-${KERNELVERSION}
 elif test -f ${BOOTDIR}/initrd-${KERNELVERSION}LK.img
 then
	INITRDNAME=initrd-${KERNELVERSION}LK
 else
	INITRDNAME=initrd-${KERNELVERSION}
 fi

 # initrd build procedure for red hat systems

 if [ "$APPENDIMG" = "y" ]
 then
 	echo "Creating new initrd - ${INITRDNAME}.img"
 else
	echo "Creating new initrd - ${INITRDNAME}"
 fi

 # remove old temporary initrd

 rm -f ${BOOTDIR}/${INITRDNAME}.tmp 2>/dev/null
 mkinitrd ${BOOTDIR}/${INITRDNAME}.tmp ${KERNELVERSION}

 if [ $? -ne 0 ]
 then
 	echo ""
	echo "mkinitrd failed"
	exit 1
 fi

 if [ "$APPENDIMG" = "y" ]
 then
 	# save old image

 	if [ -f "${BOOTDIR}/${INITRDNAME}.img" ]
	then
 		mv -f ${BOOTDIR}/${INITRDNAME}.img ${BOOTDIR}/${INITRDNAME}.img.old 2>/dev/null
 	fi

 	mv -f ${BOOTDIR}/${INITRDNAME}.tmp ${BOOTDIR}/${INITRDNAME}.img
 else
        # save old image

        if [ -f "${BOOTDIR}/${INITRDNAME}" ]
        then
                mv -f ${BOOTDIR}/${INITRDNAME} ${BOOTDIR}/${INITRDNAME}.old 2>/dev/null
        fi

        mv -f ${BOOTDIR}/${INITRDNAME}.tmp ${BOOTDIR}/${INITRDNAME}
 fi
fi

# run lilo if a configuration exists

if [ -a /etc/lilo.conf ]
then
 /sbin/lilo
fi

