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

Requires: vmware-tools-foundation >= 0:8.6.0
Provides: vmware-tools-pvscsi-kmp-%{type}
Provides: vmware-tools-pvscsi-kmp
AutoReqProv: on
%{-p:%{expand:%(cd %_sourcedir; cat %{-p*})}}
%description -n %{-n*}-%1
 This package contains a pre-compiled pvscsi module for use with the %{kernel_version} kernel.   Paravirtual SCSI adapters are high-performance storage adapters that can result in greater throughput and lower CPU utilization. Paravirtual SCSI adapters are best suited for high performance storage environments. For more information on pvscsi please see the vSphere basic system administration guide.
%(
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/pvscsi ]; then
         /etc/init.d/pvscsi stop
      fi
   fi
fi

# Configure
version=%(echo %{-v*} | tr - _)
release=%(echo %3-%{-r*} | tr - _)
if [ -e /boot/System.map-%2 ]; then
    /sbin/depmod -a -F /boot/System.map-%2 %2
fi
modules=( $(rpm -ql %{-n*}-%1-$version-$release | grep '\.ko$') )
if [ -x /usr/lib/module-init-tools/weak-modules ]; then
    printf '%s\n' "${modules[@]}" \
    | /usr/lib/module-init-tools/weak-modules --add-modules 2>/dev/null
fi
if [ -e /etc/sysconfig/kernel -a -e /boot/initrd-%2 ]; then
    source /etc/sysconfig/kernel
    run_mkinitrd=
    set -- "${modules##*/}" ; set -- "${@%.ko}"
    for module in $INITRD_MODULES; do
	case " $* " in
	*" $module "*)
	    run_mkinitrd=1
	    break ;;
	esac
    done
    if [ -n "$run_mkinitrd" ]; then
	for image in vmlinuz image vmlinux linux bzImage; do
	    if [ -f /boot/$image-%2 ]; then
		/sbin/mkinitrd -k /boot/$image-%2 \
			       -i /boot/initrd-%2 2>/dev/null\
		|| exit 1
		break
	    fi
	done
    fi
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/pvscsi ]; then
      /etc/init.d/pvscsi start
   else
      modprobe pvscsi
   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/pvscsi ]; then
         /etc/init.d/pvscsi stop
      fi
   fi
fi
version=%(echo %{-v*} | tr - _)
release=%(echo %3-%{-r*} | tr - _)
set -o noclobber
rpm -ql %{-n*}-%1-$version-$release | grep '\.ko$' \
    > /var/run/rpm-%{-n*}-%1-$version-$release-modules
%postun -n %{-n*}-%1
version=%(echo %{-v*} | tr - _)
release=%(echo %3-%{-r*} | tr - _)
modules=( $(cat /var/run/rpm-%{-n*}-%1-$version-$release-modules) )
rm -f /var/run/rpm-%{-n*}-%1-$version-%{-r*}-modules
if [ -e /boot/System.map-%2 ]; then
    /sbin/depmod -a -F /boot/System.map-%2 %2
fi
if [ -x /usr/lib/module-init-tools/weak-modules ]; then
    printf '%s\n' "${modules[@]}" \
    | /usr/lib/module-init-tools/weak-modules --remove-modules 2>/dev/null
fi
if [ -e /etc/sysconfig/kernel -a -e /boot/initrd-%2 ]; then
    source /etc/sysconfig/kernel
    run_mkinitrd=
    set -- "${modules##*/}" ; set -- "${@%.ko}"
    for module in $INITRD_MODULES; do
	case " $* " in
	*" $module "*)
	    run_mkinitrd=1
	    break ;;
	esac
    done
    if [ -n "$run_mkinitrd" ]; then
	for image in vmlinuz image vmlinux linux bzImage; do
	    if [ -f /boot/$image-%2 ]; then
		/sbin/mkinitrd -k /boot/$image-%2 \
			       -i /boot/initrd-%2 2>/dev/null\
		|| exit 1
		break
	    fi
	done
    fi
fi
%files -n %{-n*}-%1
%{-f:%{expand:%(cd %_sourcedir; cat %{-f*})}}
%{!-f:%defattr (-,root,root)}
%{!-f:/lib/modules/%2}
%{?vmware_use_symvers_file: /etc/vmware-tools/symvers}
