all_off="$(tput sgr0)"
bold="${all_off}$(tput bold)"
yellow="${bold}$(tput setaf 3)"
blue="${bold}$(tput setaf 4)"
green="${bold}$(tput setaf 2)"

_instructions() {
  printf "${yellow}==>${all_off} ${bold}ATENTION:${all_off}
${green}==>${all_off} ${bold}Run 'LSI MegaRAID Storage Manager':${all_off}
  ${blue}->${all_off} For open '${bold}LSI MegaRAID Storage Manager${all_off}' client:
     '${bold}Menu${all_off}' -> '${bold}System${all_off}' -> '${bold}MegaRAID Storage Manager StartupUI${all_off}'
     Or launch:
     '\x23 ${bold}/usr/share/MegaRAID_Storage_Manager/startupui.sh${all_off}'\n"
}

post_install() {
  ldconfig &> /dev/null

  . /etc/profile.d/msm.sh
  cd "${MSM_HOME}"
  LC_ALL=C \
  /usr/lib/jvm/java-8-openjdk/jre/bin/java \
   -Djava.util.prefs.systemRoot="${MSM_HOME}/jre" \
   -Djava.util.prefs.userRoot="${MSM_HOME}/jre/.userPrefs" \
   -classpath .:GUI.jar \
   ExternalApp.ConfigRepositoryMain 71244 \
   -set INSTALLER_VERSION "v${1%-*}" \
   &> /dev/null

  _instructions
}

post_upgrade() {
  ldconfig &> /dev/null

  . /etc/profile.d/msm.sh
  cd "${MSM_HOME}"
  rm -fr ActiveClientList.txt
  LC_ALL=C \
  /usr/lib/jvm/java-8-openjdk/jre/bin/java \
    -Djava.util.prefs.systemRoot="${MSM_HOME}/jre" \
    -Djava.util.prefs.userRoot="${MSM_HOME}/jre/.userPrefs" \
    -classpath .:Framework/Framework.jar \
    VivaldiHeartbeat ActiveClientList.txt \
    &> /dev/null
  LC_ALL=C \
  /usr/lib/jvm/java-8-openjdk/jre/bin/java \
    -Djava.util.prefs.systemRoot="${MSM_HOME}/jre" \
    -Djava.util.prefs.userRoot="${MSM_HOME}/jre/.userPrefs" \
    -classpath .:GUI.jar \
    ExternalApp.ConfigRepositoryMain 71244 \
    -clear \
    &> /dev/null
  LC_ALL=C \
  /usr/lib/jvm/java-8-openjdk/jre/bin/java \
    -Djava.util.prefs.systemRoot="${MSM_HOME}/jre" \
    -Djava.util.prefs.userRoot="$MSM_HOME}/jre/.userPrefs" \
    -classpath .:GUI.jar \
    ExternalApp.ConfigRepositoryMain 71244 \
    -set INSTALLER_VERSION "v${1%-*}" \
    &> /dev/null

  _instructions
}

pre_remove() {
  . /etc/profile.d/msm.sh
  cd "${MSM_HOME}"
  rm -fr ActiveClientList.txt
  LC_ALL=C \
  /usr/lib/jvm/java-8-openjdk/jre/bin/java \
    -Djava.util.prefs.systemRoot="${MSM_HOME}/jre" \
    -Djava.util.prefs.userRoot="${MSM_HOME}/jre/.userPrefs" \
    -classpath .:Framework/Framework.jar \
    VivaldiHeartbeat ActiveClientList.txt \
    &> /dev/null
  LC_ALL=C \
  /usr/lib/jvm/java-8-openjdk/jre/bin/java \
    -Djava.util.prefs.systemRoot="${MSM_HOME}/jre" \
    -Djava.util.prefs.userRoot="${MSM_HOME}/jre/.userPrefs" \
    -classpath .:GUI.jar \
    ExternalApp.ConfigRepositoryMain 71244 \
    -clear \
    &> /dev/null

  if [ "$(systemctl is-active lsi_msm)" = "active" ]; then
    systemctl stop lsi_msm
    printf "${green}==>${all_off} ${bold}Stopping 'lsi_msm' Service${all_off}.\n"
  fi
  if [ "$(systemctl is-enabled lsi_msm)" = "enable" ]; then
    systemctl disable lsi_msm
    printf "${green}==>${all_off} ${bold}Disabling 'lsi_msm' Service${all_off}.\n"
  fi
}

post_remove() {
  ldconfig &> /dev/null

  paths=(/usr/share/MegaRAID_Storage_Manager)
  first=true
  for path in ${paths[@]}; do
    if [ -d $path ]; then
      if $first; then
        first=false
        printf "${green}==>${all_off} ${bold}Leftover Paths:${all_off}\n"
      fi
      printf "${blue}  -> ${all_off} ${bold}$path${all_off}\n"
    fi
  done

  files=(/etc/SlAsyncAddpCfg*.cfg)
  first=true
  for file in ${files[@]}; do
    if [ -d $file ]; then
      if $first; then
        first=false
        printf "${green}==>${all_off} ${bold}Leftover files:${all_off}\n"
      fi
      printf "${blue}  -> ${all_off} ${bold}$file${all_off}\n"
    fi
  done
}
