#! /bin/sh

set -e

version="$1"
vmlinuz_location="$2"

# Note we only trigger on a remove, and not again on purge
if [ -n "$DEB_MAINT_PARAMS" ]; then
    eval set -- "$DEB_MAINT_PARAMS"
    case "$1" in
        remove)
            : echo nop
            ;;
        *)
            exit 0;
    esac
fi

# passing the kernel version is required
[ -z "$version" ] && exit 1

MODULEDIR=/lib/modules/$version
HEADERDIR=/usr/src/linux-headers-$version

# exit silently if there is no module dir
test -d $MODULEDIR || exit 0

if [ -L $MODULEDIR/build ]; then
    linked_to=$(readlink -f $MODULEDIR/build)
    if [ "$HEADERDIR" = "$linked_to" ]; then
        rm -f $MODULEDIR/build
    fi
fi

exit 0
