#!/bin/sh
set -e

# Get the package version
NAME=mlnx-ofed-kernel
PACKAGE_NAME=$NAME-dkms
VERSION=`dpkg-query -W -f='${Version}' $PACKAGE_NAME | awk -F "-" '{print $1}' | cut -d\: -f2`

case "$1" in
    remove|upgrade|deconfigure)
      if [  "`dkms status -m $NAME`" ]; then
         dkms remove -m $NAME -v $VERSION --all || true
      fi
    ;;

    failed-upgrade)
    ;;

    *)
        echo "prerm called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

# We remove files on prerm and not on postrm
# to be sure that on an upgrade we don't touch files
# generated by the new package
get_headers_dirs() {
	# Filter list by priority 17 that should list only ones
	# generated by our dkms_ofed_post_build.sh, so please don't use
	# it yourself manually:
	update-alternatives --query ofa_kernel_headers 2>/dev/null \
	|  awk 'BEGIN {RS="\n\n"}; /Priority: 17/{print}'
}
header_dirs=`get_headers_dirs`
for h_dir in $headers_dir; do
	update-alternatives --remove ofa_kernel_headers $h_dir
	rm -rf $h_dir
done

#DEBHELPER#

exit 0
