%package -n %{-n*}-%1
%define kmp_version %(echo %{-v*}-%2 | tr - _)
Version: %kmp_version
Release: %{-r*}
%(
for spec in {%_sourcedir,%_specdir}/%name.spec /dev/null; do
    [ -e $spec ] && break
done
awk '
BEGIN		{ tags["summary"] = "Summary: The vmmemctl kernel module built for the %{kernel_version} kernel."
		  tags["group"] = "Group: %group" }
/^%%/		{ in_pkg_header = \
		  ($0 ~ /^%%package[ \t]+KMP[ \t]*$/ ||
		   $0 ~ /^%%package[ \t]+-n[ \t]*%name-KMP[ \t]*$/)
		  next }
in_pkg_header && /^(Summary|Group):[ \t]*/ \
		{ tag = tolower($1) ; sub(/:$/, "", tag)
		  tags[tag] = $0 }
END		{ print tags["summary"]
		  print tags["group"] }
' $spec
)
Provides: %{-n*} = %(echo %{-v*}-%2 | tr - _)
Requires: coreutils grep %{name}-common >= %{?epoch:%{epoch}:}10.0.9%{?release:-%{release}}

Requires: vmware-tools-foundation >= 0:8.6.0
Provides: vmware-tools-vmmemctl-kmp-%{type}
Provides: vmware-tools-vmmemctl-kmp
AutoReqProv: on
%{-p:%{expand:%(cd %_sourcedir; cat %{-p*})}}
%description -n %{-n*}-%1
 This package contains a pre-compiled vmmemctl module for use with the %{kernel_version} kernel.   The vmmemctl module expands and contracts, allocating and freeing memory respectively within the guest OS. This causes the guest to invoke its memory management routines, allowing the ESX host to reclaim memory as memory pressure increases within the ESX host. Using this module allows for efficient memory over commitment on ESX hosts by forcing the guests to page out memory instead of forcing the host to page out memory.
%(
for spec in {%_sourcedir,%_specdir}/%name.spec /dev/null; do
    [ -e $spec ] && break
done
awk '
/^%%/		{ in_desc = \
		  ($0 ~ /^%%description[ \t]+KMP[ \t]*$/ ||
		   $0 ~ /^%%description[ \t]+-n[ \t]*%name-KMP[ \t]*$/)
		  next }
in_desc		{ print; good = 1 }
END		{ exit(! good) }
' $spec || \
awk '
/^%%/		{ in_desc = \
		  ($0 ~ /^%%description[ \t]*$/ ||
		   $0 ~ /^%%description[ \t]+-n[ \t]*%name[ \t]*$/)
		  next }
in_desc		{ print; good = 1 }
END		{ exit(! good) }
' $spec
)
%post -n %{-n*}-%1
# Deconfigure if upgrading
if [ $1 -gt 1 ]; then
   # If the type matches the running kernel, stop the init script
   kmodType=`uname -r | sed -e 's/.*-//g'`
   if [ "$kmodType" = "%1" ]; then
      if [ -e /etc/init.d/vmmemctl ]; then
         /etc/init.d/vmmemctl stop || true
      fi
   fi
fi

# Configure
version=%(echo %{-v*}-%2 | tr - _)
nvr=%{-n*}-%1-$version-%{-r*}
wm2=/usr/lib/module-init-tools/weak-modules2
if [ -x $wm2 ]; then
    /bin/bash -${-/e/} $wm2 --add-kmp $nvr 2>/dev/null
fi
# If the type matches the running kernel, start the init script
kmodType=`uname -r | sed -e 's/.*-//g'`
if [ "$kmodType" = "%1" ]; then
   if [ -e /etc/init.d/vmmemctl ]; then
      /etc/init.d/vmmemctl start || true
   else
      modprobe vmw_balloon || true
   fi
fi
%preun -n %{-n*}-%1
# Only deconfigure if we are erasing the package here... not if we are upgrading.
if [ $1 = 0 ]; then
   # If the type matches the running kernel, stop the init script
   kmodType=`uname -r | sed -e 's/.*-//g'`
   if [ "$kmodType" = "%1" ]; then
      if [ -e /etc/init.d/vmmemctl ]; then
         /etc/init.d/vmmemctl stop || true
      fi
   fi
fi

version=%(echo %{-v*}-%2 | tr - _)
nvr=%{-n*}-%1-$version-%{-r*}
rpm -ql $nvr | sed -n '/\.ko$/p' > /var/run/rpm-$nvr-modules
%postun -n %{-n*}-%1
version=%(echo %{-v*}-%2 | tr - _)
nvr=%{-n*}-%1-$version-%{-r*}
modules=( $(cat /var/run/rpm-$nvr-modules) )
rm -f /var/run/rpm-$nvr-modules
if [ ${#modules[*]} = 0 ]; then
    echo "WARNING: $nvr does not contain any kernel modules" >&2
    exit 0
fi
wm2=/usr/lib/module-init-tools/weak-modules2
if [ -x $wm2 ]; then
    printf '%s\n' "${modules[@]}" | /bin/bash -${-/e/} $wm2 --remove-kmp $nvr 2>/dev/null
fi
%files -n %{-n*}-%1
%{-f:%{expand:%(cd %_sourcedir; cat %{-f*})}}
%{!-f:%defattr (-,root,root)}
%{!-f:/lib/modules/%2-%1}
/usr/lib/vmware-tools/symvers
