#!/bin/bash

#/*******************************************************************
# * This file is part of the Emulex OneCommand Manager remote       *
# * management configuration application for Emulex Fibre           *
# * Channel Host Bus Adapters.                                      *
# * Copyright (C) 2003-2013 Emulex.  All rights reserved.           *
# *                                                                 *
# * www.emulex.com                                                  *
# *                                                                 *
# * Description:                                                    *
# * Invoked from RMAPI after setting linux driver parameter         *
# * (adding entry to modprobe.conf) to recreate the linux system    *
# * initial ram disk.                                               *
# *                                                                 *
# * The lpfc driver loads so early in the machine boot cycle,       *
# * that it will not receive the new parameter value unless the     *
# * updated modprobe.conf is reinserted into the ram disk.          *
# *                                                                 *
#/*******************************************************************
#

exec_command()
{
    local TEMPFILE
    RETURNVALUE=0
    if [ "$2" = background ] ; then
    TEMPFILE=`mktemp /tmp/elx-lpfc-install.XXXXXX`
	(eval $1 ; echo "RETURNVALUE=$?" >> ${TEMPFILE}) &
	while [ -e ${TEMPFILE} ] && [ ! -s ${TEMPFILE} ] ; do
	    sleep 3
	    echo -en "."
	done
	. ${TEMPFILE}
	rm -f ${TEMPFILE}
    else
	eval $1
	RETURNVALUE=$?
    fi
    echo -en "\n"
    return ${RETURNVALUE}
}

get_distribution ()
{
    ARCH=$(uname -m | sed "s/i686/i386/")
    KERNEL=$(rpm -qf /lib/modules/$(uname -r) --qf "%{VERSION}_%{RELEASE}\n")
    UNAME=$(uname -r)

    if [ -f /etc/asianux-release ] ; then
	# this is an Asianux install
	DISTRIBUTION=asianux
	AX_VERSION=$(rpm -qf /etc/asianux-release --qf "%{VERSION}" | sed "s/[^0-9\.]//g")
	RELEASE=asianux${AX_VERSION}_${KERNEL}
	DRIVER_RPM=${RPM_BASENAME}-${DRIVER_VERSION}-${SCRIPT_RPM_RELEASE}.noarch.rpm
	BOOT_FILE=/etc/rc.d/rc.local
	MESSAGESFILE="/var/log/messages"
	if [ ${ARCH} = "ia64" ] ; then
	    KERNEL_IMAGE_PATH="/boot/efi/efi/asianux/vmlinuz-${UNAME}"
	else
	    KERNEL_IMAGE_PATH="/boot/vmlinuz-${UNAME}"
	fi
	return 0
    elif [ -f /etc/redhat-release ] ; then
        # this is a Red Hat install
	DISTRIBUTION=redhat
        RH_VERSION=$(rpm -qf /etc/redhat-release --qf "%{VERSION}" | sed "s/[^0-9\.]//g")
        RELEASE=rhel${RH_VERSION}_${KERNEL}
        DRIVER_RPM=${RPM_BASENAME}-${DRIVER_VERSION}-${SCRIPT_RPM_RELEASE}.noarch.rpm
	BOOT_FILE=/etc/rc.d/rc.local
	MESSAGESFILE="/var/log/messages"
	if [ ${ARCH} = "ia64" ] ; then
	    KERNEL_IMAGE_PATH="/boot/efi/efi/redhat/vmlinuz-${UNAME}"
	else
	    KERNEL_IMAGE_PATH="/boot/vmlinuz-${UNAME}"
	fi
	return 0
    elif [ -f /etc/SuSE-release ]; then
        # this is a Suse install
	DISTRIBUTION=suse
        SUSE_VERSION=$(rpm -qf /etc/SuSE-release --qf "%{VERSION}" | sed "s/[^0-9\.]//g")
        RELEASE=sles${SUSE_VERSION}_${KERNEL}
        DRIVER_RPM=${RPM_BASENAME}-${DRIVER_VERSION}-${SCRIPT_RPM_RELEASE}.noarch.rpm
	BOOT_FILE=/etc/rc.d/boot.local
	MESSAGESFILE="/var/log/messages /var/log/warn"
	if [ ${ARCH} = "ppc64" ] ; then
	    SLES_KERNEL_IMAGE=vmlinux
	    KERNEL_IMAGE_PATH="/boot/vmlinux"
	else
	    SLES_KERNEL_IMAGE=vmlinuz
	    KERNEL_IMAGE_PATH="/boot/vmlinuz-${UNAME}"
	fi
	return 0
    else
        echo ""
        echo "Unable to determine distribution type"
        return 1
    fi
}

# Function:  backup_files_to_elx()
#
# Description:
#   Copy each file passed as a parameter to filename.elx.
#
# Parameters:
#   A list of filenames to back up.
#
# Returns:
#   Nothing.
backup_files_to_elx()
{
    for i in $@ ; do
	if [ -e $i ] ; then
	    cp -f $i $i.elx
	    if [ $? -ne 0 ] ; then
	        echo "Error $? copying $i to $i.elx"
	    else
	        echo "Original ramdisk image $i saved as $i.elx"
	    fi
	fi
    done
}

# Function:  create_ramdisk()
#
# Description:
#   Creates a ramdisk image for the currently running kernel.
#
# Parameters:
#   none.
#
# Returns:
#   Nothing.
create_ramdisk()
{
    if [ ${DISTRIBUTION} = "redhat" ] ||
	[ ${DISTRIBUTION} = "asianux" ] ; then
	# Configure ramdisk for Red Hat Linux or Asianux.
	if [ ${ARCH} = "ia64" ] ; then
	    INITRDFILE="/boot/efi/efi/${DISTRIBUTION}/initrd-${UNAME}.img"
	else
	    INITRDFILE="/boot/initrd-${UNAME}.img"
	fi
	backup_files_to_elx ${INITRDFILE}
	echo -n "Creating ramdisk ."
	exec_command "/sbin/mkinitrd --allow-missing -f ${INITRDFILE} ${UNAME}" background
	if [ $? -ne 0 ] ; then
	    echo "Could not create ramdisk image.  Restoring original ramdisk ..."
	    cp -f /boot/initrd-${UNAME}.img.elx /boot/initrd-${UNAME}.img
	    echo "Original ramdisk restored."
	    echo ""
	    echo "Please run '${INSTALL_SCRIPT} --uninstall' to remove any partially"
	    echo "installed components of the Emulex driver and utility applications."
	    exit 1
	fi
    else
	# Configure ramdisk configuration file for SuSE Linux Enterprise Server.
	if [ ${ARCH} = "ppc64" ] ; then
	    INITRDFILE="/boot/initrd"
	else
	    INITRDFILE="/boot/initrd /boot/initrd.shipped"
	fi
	backup_files_to_elx ${INITRDFILE}

	echo -n "Creating ramdisk ."

        if [ -x /sbin/mkinitrd ]; then
            exec_command "/sbin/mkinitrd -i initrd-${UNAME} -k ${SLES_KERNEL_IMAGE}-${UNAME} >/dev/null 2>&1" background
        else
            exec_command "/sbin/mk_initrd -i initrd-${UNAME} -k ${SLES_KERNEL_IMAGE}-${UNAME} >/dev/null 2>&1" background
	fi
        if [ $? -ne 0 ] ; then
	    echo "Could not create ramdisk image.  Restoring original ramdisk ..."
	    cp -f /boot/initrd.shipped.elx /boot/initrd.shipped && cp -f /boot/initrd.elx /boot/initrd
	    if [ $? -ne 0 ] ; then
		echo "Original ramdisks restored."
	    else
		echo "Original ramdisks could not be restored - error $?"
	    fi
	    echo ""
	    echo "Please run '${INSTALL_SCRIPT} --uninstall' to remove any partially"
	    echo "installed components of the Emulex driver and utility applications."
	    exit 1
	fi
    fi

    return 0
}

print_help()
{
    echo ""
    echo "  The following option must be used:"
    echo ""
    echo "   --createramdisk   - Create a new ramdisk image.  Use this option after"
    echo "                        you have modified driver parameters in the"
    echo "                        /etc/modprobe.conf file."
    echo ""
}

if [ $# -eq 0 ] ; then
    print_help
    exit 1
fi

echo "Determining distribution type and kernel version ..."

if ! get_distribution; then
   echo ${GENERIC_MESSAGE}
   exit 1
fi

if [ "$1" = "--createramdisk" ]; then
    create_ramdisk
else
    print_help
    exit -1
fi
 
exit 0
