
#!/bin/bash
#
# Install Avago MegaRAID Storage Manager (MSM) for Ubuntu 18.04 ~ 20.10
#
# (Not work on Ubuntu 21.04)
#
# Bash Script by Johan at OSSLab (https://www.osslab.com.tw)
#

# Elevate user level to root for files installation (提升使用者權限至root)
cd ~
echo Input password to continue if asked.
sudo echo

# Download MSM Linux x64 17.05.02.01 RPM Package directly from Broadcom website (直接從Broadcom網站下載MSM Linux RPM套件)
wget https://docs.broadcom.com/docs-and-downloads/docs-and-downloads/raid-controllers/raid-controllers-common-files/17.05.02.01_MSM_Linux-x64.zip

# Extract the MSM package (解壓縮MSM Linux x64套件)
unzip 17.05.02.01_MSM_Linux-x64.zip
tar zxvf MSM_linux_x64_installer-17.05.02-01.tar.gz

# Install Alien Package Converter and Alacarte Menu Editor (安裝Alien包裝轉換器和alacarte選單編輯程式)
sudo apt install alien -y
sudo apt install alacarte -y

# Convert RPM to DEB (將RPM轉換成DEB)
cd disk
echo Converting RPM to DEB, please wait…
sudo alien --scripts *.rpm

# Install lib deb and megaraid deb (安裝LibUtil和MegaRaid套件包)
sudo dpkg -i lib-utils2_1.00-11_all.deb
sudo dpkg -i megaraid-storage-manager_17.05.02-2_all.deb

# Delete package files (將套件包刪除以節省空間)
cd ..
rm -rf disk
rm MSM_linux_x64_installer-17.05.02-01.tar.gz
rm MD5CheckSum.txt

# Confirm MSM files are successfully installed or remove then exit (檢查MSM是否有安裝，沒有的話就移除安裝套件並結束)
if [ ! -d "/usr/local/MegaRAID Storage Manager/" ]; then
  echo
  echo MSM not installed successfully.
  echo
  sudo dpkg -r megaraid-storage-manager
  sudo dpkg -r lib-utils2
  echo
  echo Package files uninstalled.
  echo Bash script terminated.
  exit 1
fi

# Start and make MSM service run at startup (啟用並讓開機時執行MSM服務)
sudo /etc/init.d/vivaldiframeworkd start
sudo update-rc.d vivaldiframeworkd defaults

# Create Alacarte Menus for MSM (用Alacarte來建立MSM程式選單)
echo '[Desktop Entry]' >> ~/.local/share/applications/alacarte-made.desktop
echo 'Comment=Avago MegaRAID Storage Manager' >> ~/.local/share/applications/alacarte-made.desktop
echo 'Terminal=false' >> ~/.local/share/applications/alacarte-made.desktop
echo 'Name=MegaRAID Storage Manager' >> ~/.local/share/applications/alacarte-made.desktop
echo 'Exec=sh "/usr/local/MegaRAID Storage Manager/startupui.sh"' >> ~/.local/share/applications/alacarte-made.desktop
echo 'Type=Application' >> ~/.local/share/applications/alacarte-made.desktop
echo 'Icon=/usr/local/MegaRAID Storage Manager/16_prg_icon.png' >> ~/.local/share/applications/alacarte-made.desktop

# Notify user to login with root to run MSM (提醒用戶執行MSM時要用root登入)
clear
echo MSM Installation Completed. System reboot is recommended.
echo
echo 'Hereafter, you can start MSM via start menu -> MegaRAID Storage Manager.'
echo
echo Remember to login with root account for full access of MSM.
echo
read -p "Press Enter to start MSM."

# Now run MSM GUI then end (進入MSM圖形使用者介面並結束)
cd "/usr/local/MegaRAID Storage Manager"
sudo ./startupui.sh