#!/bin/bash

# Name: make_initrd
# Copyright: (c)Copyright 2003-2006, 2008 Hewlett-Packard Development Company, L.P.
#
# 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
# CD	01/27/08	Added help message and accept -k option
# CD	02/25/08	Add recognition for HPFCHBAKITDEBUG
# CD	03/04/08	Add -l option to log building the initrd	
# CD	03/24/08	Fixed if statement check for DOLOG
# CD	10/08/08	Change test for which distribution is running to check
#                       for /etc/SuSE-release and /etc/redhat-release
# KW	11/05/10        Added RHEL 6, use initramfs
#

# Turn debug on?

if [ "$HPFCHBAKITDEBUG" = "y" ]
then
	echo ""
	echo "$0"
	echo ""
	set -x
fi

# Defines

KERNELVERSION=""
LOGGER="/opt/hp/hp-fc-enablement/logger.pl"
DOLOG=0

# 
# Functions
#

# Name: PrintHelp
# Description: Prints a help message
# In: None
# Out: None
# Returns: None
PrintHelp () {
	echo "NAME"
	echo ""
	echo "make_initrd"
	echo ""
	echo "DESCRIPTION"
	echo ""
	echo "Craete new initrd"
	echo ""
	echo "OPTIONS"
	echo ""
	echo "-h, --help   - Prints help message"
	echo "-k, --kernel - Specify kernel to build initrd for"
	echo "-l, --log    - Log building initrd"
}

# Name: ReadArgs
# Description: Parse command line arguments
# In: Command line arguments
# Out: None
# Returns: None
ReadArgs () {
	for i in $*
	do
		if [ "$i" = "-h" ] || [ "$i" = "--help" ]
		then
			PrintHelp
			exit 0
		elif [ "$i" = "-k" ] || [ "$i" = "--kernel" ]
		then
			shift
			KERNELVERSION=$1
		elif [ "$i" = "-l" ] || [ "$i" = "--log" ]
		then
			DOLOG=1
		fi
	done
}

#
# Script Main
#

# Read command line arguments
ReadArgs $*

# Check kernel version

if [ "$KERNELVERSION" = "" ]
then
	KERNELVERSION="`uname -r`"
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 -f /etc/SuSE-release
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
	
	if [ "$DOLOG" = "1" ]
	then
		$LOGGER -m "make_initrd: Issuing command \"mkinitrd -k vmlinuz-${KERNELVERSION} -i $INITRDNAME\""
	fi
 	mkinitrd -k vmlinuz-${KERNELVERSION} -i $INITRDNAME

 fi
elif test -f /etc/redhat-release
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


# Check for RHEL 6.x

MAJVER=0
MINVER=0

if test -f /etc/redhat-release
then
        #
        # RHEL case
        if [ `grep -c "Red Hat Enterprise Linux" /etc/redhat-release` -gt 0 ]
        then
                VERSTR="`grep "Red Hat Enterprise Linux" /etc/redhat-release | awk '{print $7}'`"

                if [ "$VERSTR" != "" ]
                then
                        MAJVER="`echo $VERSTR | awk 'BEGIN {FS="."} {print $1}'`"
                        MINVER="`echo $VERSTR | awk 'BEGIN {FS="."} {print $2}'`"
                fi
        fi

        #
        # OEL case
        if [ `grep -c "Enterprise Linux Enterprise Linux" /etc/redhat-release` -gt 0 ]
        then
                VERSTR="`grep "Enterprise Linux Enterprise Linux" /etc/redhat-release | awk '{print $7}'`"

                if [ "$VERSTR" != "" ]
                then
                        MAJVER="`echo $VERSTR | awk 'BEGIN {FS="."} {print $1}'`"
                        MINVER="`echo $VERSTR | awk 'BEGIN {FS="."} {print $2}'`"
                fi
        fi

     # RHEL 6 case
        if [ "$MAJVER" = "6" ]
        then   
        #   echo "RHEL 6"
        #   echo ""
            INITRDNAME=initramfs-${KERNELVERSION}            
        fi
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
 
 if [ "$DOLOG" = "1" ]
 then
 	$LOGGER -m "make_initrd: Issuing command \"mkinitrd ${BOOTDIR}/${INITRDNAME}.tmp ${KERNELVERSION}\""
 fi
 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
	
	if [ "$DOLOG" = "1" ]
	then
		$LOGGER -m "make_initrd: New initrd is ${INITRDNAME}.img"
	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

	if [ "$DOLOG" = "1" ]
	then
		$LOGGER -m "make_initrd: New initrd is ${INITRDNAME}"
	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

