#!/bin/sh
###############################################################################
#
#          Dell Inc. PROPRIETARY INFORMATION
# This software is supplied under the terms of a license agreement or
# nondisclosure agreement with Dell Inc. and may not
# be copied or disclosed except in accordance with the terms of that
# agreement.
#
# Copyright (c) 2010 Dell Inc. All Rights Reserved.
#
# Module Name: uninstall_dm.sh
# Module Version: 1.0.1.4
# Environment: Linux
#
# Abstract/Purpose:
#
#   Uninstalls Device Mapper RDAC driver and associated edits.
#
#
# Parameters:
#
#   None.
#
#
# Returns:
#
#   0 - The uninstallation was successful or DM is not installed
#   1 - Unsupported Linux distro
#   2 - Unable to uninstall scsi_dh_rdac package
#
###############################################################################

# Return codes.
RETCODE_SUCCESS=0
RETCODE_UNSUPPORTED_DISTRO=1
RETCODE_RDAC_UNINSTALL_FAILED=2


###############################################################################
# Sets up messages printed during operation.
###############################################################################
function SetupStrings
{
  IDS_UNSUPPORTED_DISTRO="The current Linux distribution is not supported."
  IDS_CHECKING_LINUX_DISTRO="Checking Linux distribution..."
  IDS_REQUIRED_PACKAGES="The following packages are required for DM installation and do not appear to be installed:"
  IDS_UNINSTALL_ABORTED="Uninstall aborted.  Unable to uninstall"
  IDS_UNINSTALLING="Uninstalling"
  IDS_UPDATING="Updating"
  IDS_CHECK_UPDATE="Checking if update is needed for"
  IDS_CHECK_REMOVE="Checking if uninstall is needed for"
  IDS_UPDATE_YES="yes"
  IDS_UPDATE_NO="no"
  IDS_RAMDISK_REBUILD="Rebuilding ramdisk image"
  IDS_RAMDISK_SAVE="Saving old initrd as"
  IDS_RAMDISK_NEW="Making new initrd as"
  IDS_REBOOT_REQUIRED="*** A system reboot is required for driver uninstall to complete. ***"
}


###############################################################################
# Gets information about the Linux distribution and checks if it is supported.
###############################################################################
function CheckLinuxDist
{
  echo -n "${IDS_CHECKING_LINUX_DISTRO}"

  # Redhat distros.
  if [ -e /etc/redhat-release ]; then 
    LINUX_DISTRO=rhel
    LINUX_KERNEL_REV=$(uname -r)
    LINUX_ARCH=$(uname -i)
    LINUX_REV=$(cat /etc/redhat-release | sed 's/.*release //' | sed 's/ .*//')
    LINUX_REV_MAJOR=$(echo ${LINUX_REV} | sed 's/[\.][0-9]*//')
    LINUX_REV_MINOR=$(echo ${LINUX_REV} | sed 's/[0-9]*[\.]*//')
    if [ -z ${LINUX_REV_MINOR} ]; then
      LINUX_REV_MINOR=0
    fi
    echo "${LINUX_DISTRO} ${LINUX_REV}"    

    # Check for unsupported revs.
    if [ ${LINUX_REV_MAJOR} -lt 5 ]; then
      echo "${IDS_UNSUPPORTED_DISTRO}"
      exit $RETCODE_UNSUPPORTED_DISTRO
    elif [ ${LINUX_REV_MAJOR} -eq 5 -a ${LINUX_REV_MINOR} -lt 4 ]; then
      echo "${IDS_UNSUPPORTED_DISTRO}"
      exit $RETCODE_UNSUPPORTED_DISTRO
    fi

  # SuSE distros.
  elif [ -e /etc/SuSE-release ]; then 
    LINUX_DISTRO=sles
    LINUX_KERNEL_REV=$(uname -r)
    LINUX_ARCH=$(uname -i)
    LINUX_REV_MAJOR=$(cat /etc/SuSE-release | tr '\n' ' ' | sed 's/.*VERSION = //' | sed 's/ .*//')
    LINUX_REV_MINOR=$(cat /etc/SuSE-release | tr '\n' ' ' | sed 's/.*PATCHLEVEL = //' | sed 's/ .*//')
    LINUX_REV=${LINUX_REV_MAJOR}.${LINUX_REV_MINOR}
    echo "${LINUX_DISTRO} ${LINUX_REV}"    

    # Check for unsupported revs.
    if [ ${LINUX_ARCH} != "x86_64" ]; then
      echo "${IDS_UNSUPPORTED_DISTRO}"
      exit $RETCODE_UNSUPPORTED_DISTRO
    elif [ ${LINUX_REV_MAJOR} -lt 10 ]; then
      echo "${IDS_UNSUPPORTED_DISTRO}"
      exit $RETCODE_UNSUPPORTED_DISTRO
    elif [ ${LINUX_REV_MAJOR} -eq 10 -a ${LINUX_REV_MINOR} -lt 3 ]; then
      echo "${IDS_UNSUPPORTED_DISTRO}"
      exit $RETCODE_UNSUPPORTED_DISTRO
    fi
  else
    echo "${IDS_UNSUPPORTED_DISTRO}"
    exit $RETCODE_UNSUPPORTED_DISTRO
  fi
}


###############################################################################
# Updates the the passed original file with the passed modified file
# if they are different.
#
# Parameters:
#   1 - File To Modify
#   2 - Modified File
###############################################################################
function CompareAndUpdateFile
{
  local FILE_TO_MODIFY="${1}"
  local FILE_MODIFIED="${2}"

  # Update file if needed.
  echo -n "${IDS_CHECK_UPDATE} ${FILE_TO_MODIFY}..."
  IGNORE=$(diff "${FILE_TO_MODIFY}" "${FILE_MODIFIED}")
  if [ $? -eq 0 ]; then
    echo "${IDS_UPDATE_NO}"
    rm -f "${FILE_MODIFIED}"
  else
    echo "${IDS_UPDATE_YES}"
    echo "${IDS_UPDATING} ${FILE_TO_MODIFY}..."
    cp -f "${FILE_MODIFIED}" "${FILE_TO_MODIFY}"
    rm -f "${FILE_MODIFIED}"
  fi
}


###############################################################################
# Restore the RHEL 5.4 udev multipath rule edits made during the install.
###############################################################################
function RestoreRHEL54UDevRules
{
  local FILE_TO_MODIFY="/etc/udev/rules.d/40-multipath.rules"
  local FILE_MODIFIED="/etc/udev/rules.d/40-multipath.rules.dell_dm_modified"

  # Strip entries.
  awk '
    /^# Begin Dell MD[0-9x]* Modification/ {modification_depth=modification_depth+1; next;}
    /^# End Dell MD[0-9x]* Modification/ {modification_depth=modification_depth-1; next;}
    modification_depth>0 {if ($1 == "#") {sub("^# ?", ""); print $0;} next;}
    {print $0};
  '  <"${FILE_TO_MODIFY}" >"${FILE_MODIFIED}"
   
  # Update conf file if necessary.
  CompareAndUpdateFile "${FILE_TO_MODIFY}" "${FILE_MODIFIED}"
}


###############################################################################
# Restores the modprobe.conf file by removing edits made during the install.
###############################################################################
function RestoreModprobeConf
{
 if [ "${LINUX_DISTRO}" == "rhel" ]; then
  if [ ${LINUX_REV_MAJOR} -lt 6 ]; then
	local FILE_TO_MODIFY=/etc/modprobe.conf
	local FILE_MODIFIED=/etc/modprobe.conf.dell_dm_modified
  else # it is RHEL6
	local FILE_TO_MODIFY=/etc/modprobe.d/dell_md.conf
	local FILE_MODIFIED=/etc/modprobe.d/dell_md_dm_modified	
  fi
 else  # it is SLES
   	local FILE_TO_MODIFY=/etc/modprobe.conf
	local FILE_MODIFIED=/etc/modprobe.conf.dell_dm_modified
 fi

  # Exit if conf file can't be accessed.
  if [ ! -e "${FILE_TO_MODIFY}" ]; then
    return 0
  fi

  # Strip entries.
  awk '
    /^# Begin Dell MD[0-9x ]* Modification/ {modification_depth=modification_depth+1; next;}
    /^# End Dell MD[0-9x ]* Modification/ {modification_depth=modification_depth-1; next;}
    modification_depth>0 {if ($1 == "#") {sub("^# ?", ""); print $0;} next;}
    /alias scsi_hostadapter[0-9]* dm[-_]multipath/ {next;}
    /alias scsi_hostadapter[0-9]* scsi_dh_rdac/ {next;}
    {print $0};
  '  <"${FILE_TO_MODIFY}" >"${FILE_MODIFIED}"
   
  # Update conf file if necessary.
  CompareAndUpdateFile "${FILE_TO_MODIFY}" "${FILE_MODIFIED}"
}


###############################################################################
# Restores the mutlipath.conf file by removing edits made during the install.
###############################################################################
function RestoreMultipathConf
{
  local FILE_TO_MODIFY=/etc/multipath.conf
  local FILE_MODIFIED=/etc/multipath.conf.dell_dm_modified

  # Exit if conf file can't be accessed.
  if [ ! -e "${FILE_TO_MODIFY}" ]; then
    return 0
  fi

  # Strip entries.
  awk '
    /^# Begin Dell MD[0-9x ]* Modification/ {modification_depth=modification_depth+1; next;}
    /^# End Dell MD[0-9x ]* Modification/ {modification_depth=modification_depth-1; next;}
    modification_depth>0 {if ($1 == "#") {sub("^# ?", ""); print $0;} next;}
    {print $0};
  ' <"${FILE_TO_MODIFY}" >"${FILE_MODIFIED}"
   
  # Update conf file if needed.
  CompareAndUpdateFile "${FILE_TO_MODIFY}" "${FILE_MODIFIED}"
}


###############################################################################
# Restores the SLES boot.local file by removing edits made during install.
###############################################################################
function RestoreSLESBootLocal
{
  local FILE_TO_MODIFY="/etc/init.d/boot.local"
  local FILE_MODIFIED="/etc/init.d/boot.local.dell_dm_modified"

  # Exit if file can't be accessed.
  if [ ! -e "${FILE_TO_MODIFY}" ]; then
    return 0
  fi

  # Strip entries.
  awk '
    /^# Begin Dell MD[0-9x]* Modification/ {modification_depth=modification_depth+1; next;}
    /^# End Dell MD[0-9x]* Modification/ {modification_depth=modification_depth-1; next;}
    modification_depth>0 {if ($1 == "#") {sub("^# ?", ""); print $0;} next;}
    {print $0};
  ' <"${FILE_TO_MODIFY}" >"${FILE_MODIFIED}"
   
  # Update file if needed.
  CompareAndUpdateFile "${FILE_TO_MODIFY}" "${FILE_MODIFIED}"
}


###############################################################################
# Restores the SLES unsupported-modules file by removing edits made during
# install.
###############################################################################
function RestoreSLESUnsupportedModules
{
  local FILE_TO_MODIFY="/etc/modprobe.d/unsupported-modules"
  local FILE_MODIFIED="/etc/modprobe.d/unsupported-modules.dell_dm_modified"

  # Exit if file can't be accessed.
  if [ ! -e "${FILE_TO_MODIFY}" ]; then
    return 0
  fi

  # Strip entries.
  awk '
    /^# Begin Dell MD[0-9x]* Modification/ {modification_depth=modification_depth+1; next;}
    /^# End Dell MD[0-9x]* Modification/ {modification_depth=modification_depth-1; next;}
    modification_depth>0 {if ($1 == "#") {sub("^# ?", ""); print $0;} next;}
    {print $0};
  ' <"${FILE_TO_MODIFY}" >"${FILE_MODIFIED}"
   
  # Update file if needed.
  CompareAndUpdateFile "${FILE_TO_MODIFY}" "${FILE_MODIFIED}"
}


###############################################################################
# Restores the SLES fixed disk storage policy by removing edits made during
# install.
###############################################################################
function RestoreSLES11StoragePolicy
{
  local FILE_TO_MODIFY="/usr/share/hal/fdi/policy/10osvendor/99-storage-policy-fixed-drives.fdi"
  local FILE_MODIFIED="/usr/share/hal/fdi/policy/10osvendor/99-storage-policy-fixed-drives.fdi.dell_dm_modified"

  # Exit if file can't be accessed.
  if [ ! -e "${FILE_TO_MODIFY}" ]; then
    return 0
  fi

  # Strip entries.
  awk '
    /^<!-- Begin Dell MD[0-9x ]* Modification -->/ {modification_depth=modification_depth+1; next;}
    /^<!-- End Dell MD[0-9x ]* Modification -->/ {modification_depth=modification_depth-1; next;}
    modification_depth>0 {next;}
    {print $0};
  ' <"${FILE_TO_MODIFY}" >"${FILE_MODIFIED}"
   
  # Update file if needed.
  CompareAndUpdateFile "${FILE_TO_MODIFY}" "${FILE_MODIFIED}"
}


###############################################################################
# Rebuilds the initial ramdisk image for RHEL.
###############################################################################
function RebuildRHELRamDisk
{
  echo "${IDS_RAMDISK_REBUILD}..."
  if [ "${LINUX_REV_MAJOR}" == "6" ]; then
    local RD_IMAGE="/boot/initramfs-${LINUX_KERNEL_REV}.img"
  else
    local RD_IMAGE="/boot/initrd-${LINUX_KERNEL_REV}.img"
  fi
  if [ -e "${RD_IMAGE}" ]; then
    local RD_IMAGE_COPY="${RD_IMAGE}.dell_dm_old"
    echo "${IDS_RAMDISK_SAVE} ${RD_IMAGE_COPY}"
    cp -f "${RD_IMAGE}" "${RD_IMAGE_COPY}"
  fi
  echo "${IDS_RAMDISK_NEW} ${RD_IMAGE}"
  if [ "${LINUX_REV_MAJOR}" == "6" ]; then
    dracut --force
  else
    mkinitrd -f "${RD_IMAGE}" "${LINUX_KERNEL_REV}"
  fi
  REBOOT_NEEDED=TRUE
}

###############################################################################
# Uninstalls RPMs with existence checking.
#
# Parameters:
#   1 - Package Name
#
# Returns:
#   0 - The package was uninstalled
#   1 - Uninstall was not needed
#   2 - The package uninstall failed
###############################################################################
function UninstallRPM
{
  local RETVAL=0

  # Uninstall package if needed.
  local PKG_NAME="${1}"
  echo -n "${IDS_CHECK_REMOVE} ${PKG_NAME}..."
  INSTALLED_PKG_NAME=$(rpm -q ${PKG_NAME})
  # Installed.
  if [ $? -eq 0 ]; then
    # Uninstall package.
    echo "${IDS_UPDATE_YES}"
    echo "${IDS_UNINSTALLING} ${PKG_NAME}..."
    rpm -e "${PKG_NAME}"
    if [ $? -ne 0 ]; then
      RETVAL=2
    fi
  # Not installed.
  else
    echo "${IDS_UPDATE_NO}"
    RETVAL=1
  fi
  
  return ${RETVAL}
}


###############################################################################
# Uninstalls the DKMS DM RDAC driver.
###############################################################################
function UninstallRDAC
{
  # Restore modprobe.conf if needed.
  if [ ${LINUX_REV_MAJOR} -lt 6 -a ${LINUX_REV_MINOR} -lt 7 ] || 
       [ ${LINUX_REV_MAJOR} -lt 6 -a ${LINUX_REV_MINOR} -lt 2 ]; then
    RestoreModprobeConf
  fi

  # Uninstall RDAC driver if needed.
  UninstallRPM "scsi_dh_rdac"
  if [ $? -eq 0 ]; then
    REBOOT_NEEDED=TRUE
  elif [ $? -ne 1 ]; then
    echo "${IDS_UNINSTALL_ABORTED} scsi_dh_rdac"
    exit $RETCODE_RDAC_UNINSTALL_FAILED
  fi
}


###############################################################################
# Handles DM uninstallation for RHEL.
###############################################################################
function UninstallRHEL
{
  # RHEL 5.4.
  if [ "${LINUX_REV}" == "5.4" ]; then
    # Remove RDAC driver if needed.
    UninstallRDAC

    # Restore the udev rules if needed.
    RestoreRHEL54UDevRules

  elif [ "${LINUX_REV}" == "5.5" ]; then
    # Remove RDAC driver if needed.
    UninstallRDAC
 
  elif [ "${LINUX_REV}" == "5.6" ]; then
    # Remove RDAC driver if needed.
    UninstallRDAC

  #elif [ "${LINUX_REV}" == "5.7" ]; then
    # Remove RDAC driver if needed.
   # UninstallRDAC
	
  elif [ "${LINUX_REV_MAJOR}" == "6" ]; then
    # Remove RDAC driver if needed.
    UninstallRDAC
  fi

  # Restore the multipath.conf file if necessary.
  RestoreMultipathConf
  
  # Rebuild the ramdisk image.
  RebuildRHELRamDisk

  # Remove rescan script if needed.
  if [ -e "/usr/sbin/rescan_dm_devs" ]; then
   rm -f "/usr/sbin/rescan_dm_devs"
  fi
}


###############################################################################
# Handles DM uninstallation for SLES.
###############################################################################
function UninstallSLES
{
  # SLES 10 SP3.
  if [ "${LINUX_REV}" == "10.3" ]; then
    # Remove RDAC driver if needed.
    UninstallRDAC

    # Restore boot.local on SLES if needed.
    RestoreSLESBootLocal
	
  elif [ "${LINUX_REV}" == "10.4" ]; then
    # Remove RDAC driver if needed.
    UninstallRDAC

    # Restore boot.local on SLES if needed.
    RestoreSLESBootLocal
	
  # SLES 11.
  elif [ "${LINUX_REV}" == "11.0" ]; then
    # Do NOT uninstall errata kernel packages: module-init-tools, kernel-default-base, kernel-default, kernel-source.
    # Do NOT uninstall updated DM packages required by errata kernel: multipath-tools, udev, kpartx.

    # Remove RDAC driver if needed.
    UninstallRDAC

    # Restore unsupported-modules on SLES if needed.
    RestoreSLESUnsupportedModules

    # Restore boot.local on SLES if needed.
    RestoreSLESBootLocal

  # SLES 11.1.
  elif [ "${LINUX_REV}" == "11.1" ]; then
    # Remove RDAC driver if needed.
    UninstallRDAC

    # Restore unsupported-modules on SLES if needed.
    RestoreSLESUnsupportedModules
  fi

  # Restore fixed disk storagy policy for SLES 11 if needed.
  if [ "${LINUX_REV_MAJOR}" == "11" ]; then
    RestoreSLES11StoragePolicy
  fi

  # Restore the multipath.conf file if necessary.
  RestoreMultipathConf
  
  # Remove rescan script if needed.
  if [ -e "/usr/sbin/rescan_dm_devs" ]; then
   rm -f "/usr/sbin/rescan_dm_devs"
  fi
}


###############################################################################
# Main DM uninstall routine.
###############################################################################
function UninstallDM
{
  # Setup strings used during the installation.
  SetupStrings
  
  # Get Linux Distro and exit if unsupported.
  CheckLinuxDist

  # Call the uninstall logic for the appropriate distro.
  if [ "${LINUX_DISTRO}" == "rhel" ]; then
    UninstallRHEL
  elif [ "${LINUX_DISTRO}" == "sles" ]; then
    UninstallSLES
  fi
  
  # Reboot notification.
  if [ "${REBOOT_NEEDED}" == "TRUE" ]; then
    echo ""  
    echo "${IDS_REBOOT_REQUIRED}"
    echo ""  
  fi
}


###############################################################################
# Main Execution Starts Here
###############################################################################

# Kick off uninstallation.
UninstallDM
