#!/bin/bash

# (C) 2017 Intel Corporation. All rights reserved.
# Your use of Intel Corporation's design tools, logic functions and other
# software and tools, and its AMPP partner logic functions, and any output
# files any of the foregoing (including device programming or simulation
# files), and any associated documentation or information are expressly subject
# to the terms and conditions of the Intel Program License Subscription
# Agreement, Intel MegaCore Function License Agreement, or other applicable
# license agreement, including, without limitation, that your use is for the
# sole purpose of programming logic devices manufactured by Intel and sold by
# Intel or its authorized distributors.  Please refer to the applicable
# agreement for further details.


###############################################################################
# Script removes packages and configuration settings added during install
###############################################################################

if [ -n "$PAC_BSP_ENV_DEBUG_UNINSTALL_SCRIPT" ]; then
  set -x
fi

###############################################################################
# Global variables
#   HOST_OS: OS running on host system
###############################################################################
HOST_OS=""

###############################################################################
# Determine host OS and exit if OS not supported
# Globals:
#   HOST_OS  
###############################################################################
check_os() {
  kernel=$(uname -r)
  kernel_status=$?

  # lsb_release not installed by default on CentOS 7
  if command -v lsb_release > /dev/null; then
    distro=$(lsb_release -is)
    distro_status=$?
    release=$(lsb_release -rs)
    release_status=$?
    if [ $distro_status == 0 ] && [ $release_status == 0 ] && [ $kernel_status == 0 ]; then
      if [ "$distro" == "RedHatEnterpriseServer" ] || [ "$distro" == "CentOS" ] && [[ $release =~ ^7.[0-9]+ ]]; then
        HOST_OS="rhel7"
      elif [ "$distro" == "Ubuntu" ] && [ "$release" == "18.04" ] &&  [[ $kernel == 4.15.* ]]; then
        HOST_OS="ubuntu1804"
      else
        error "unsupported OS: $distro $release kernel $kernel"
      fi
    else
      error "could not determine OS"
    fi
  elif [ -f /etc/redhat-release ] && [[ $kernel == 3.10.* ]]; then
    HOST_OS="rhel7"
  else
    error "could not determine OS"
  fi
}

###############################################################################
# Remove config files that were set during install
###############################################################################
delete_config() {
  echo "Removing configuration files"
  sudo rm /etc/security/limits.d/90-intel-fpga-opencl-limits.conf
  sudo rm /etc/udev/rules.d/90-intel-fpga-opencl.rules
  sudo rm /etc/sysctl.d/intel-fpga-opencl-sysctl.conf
}

###############################################################################
# Remove OPAE packages installed on Ubuntu 18.04 system
###############################################################################
remove_ubuntu_pkgs() {
  # shellcheck disable=SC2016
  pkg_list=$(dpkg-query -W -f '${Package} ' "*opae*")
  if [ -n "$pkg_list" ]; then
    # shellcheck disable=SC2086
    sudo apt remove -y $pkg_list
  fi
}

###############################################################################
# Remove OPAE packages installed on RHEL 7 system
###############################################################################
remove_rhel_pkgs() {
  pkg_list=$(yum list installed "*opae*" | grep opae | cut -f1 -d" " | xargs)
  if [ -n "$pkg_list" ]; then
    # shellcheck disable=SC2086
    sudo yum remove -y $pkg_list
  fi
  sudo ldconfig
}

###############################################################################
# Call function to remove package from detected OS
###############################################################################
uninstall_packages() {
  if [ "$HOST_OS" == ubuntu1804 ]; then
    remove_ubuntu_pkgs
  elif [ "$HOST_OS" == rhel7 ]; then
    remove_rhel_pkgs
  else
    echo "Host OS not detected. Skipping OPAE removal"
  fi
}


###############################################################################
# Main entry point
###############################################################################
check_os

echo -n "Do you want to remove OPAE packages (disables FPGA runtime and compilation) [Y/n] "
read answer
if [ "$answer" != "n" ]; then
  echo "Starting uninstall of Intel FPGA driver and OPAE"
  uninstall_packages
fi

echo -n "Do you want to remove OPAE config settings [Y/n] "
read answer
if [ "$answer" != "n" ]; then
  echo "Deleting OPAE config files"
  delete_config
fi

echo "PAC OpenCL BSP uninstall complete"
