#!/bin/bash
FILE="/etc/modprobe.conf"

function exist_entries() {
  isThere=$(cat $1 | grep scsi_hostadapter)
}

function new_entry() {
  cat <<EOF > /etc/modprobe.d/clara-vmscsi.conf
alias scsi_hostadapter mptbase
alias scsi_hostadapter1 mptscsih
alias scsi_hostadapter2 mptfc
alias scsi_hostadapter3 mptspi
alias scsi_hostadapter4 mptsas
EOF

}

function alter_entries() {
  sed -e '/^alias scsi_hostadapter/ s/alias scsi_hostadapter/#SEE \/etc\/modprobe.d\/clara-vmscsi.conf FOR DETAILS WHY BELOW LINE WAS COMMENTED\n# alias scsi_hostadapter/g' -i $1
}

function exist_in_subdirectory() {
  exist_entries "/etc/modprobe.d/*.conf"
  if [[ ! -n $isThere ]]; then
    new_entry
  else
    alter_entries "/etc/modprobe.d/*.conf"
    new_entry
  fi
}

if [[ ! -f $FILE ]]; then
  exist_in_subdirectory
else
  exist_entries "/etc/modprobe.conf"
  if [[ -n $isThere ]]; then
    alter_entries "/etc/modprobe.conf"
  fi
  exist_in_subdirectory
fi

$(which mkinitrd) --preload mptbase -f -v /boot/initrd-$(uname -r).img $(uname -r)

