#!/bin/bash
if [ ! -d /usr/lib/vmware-tools ]; then 
    echo -en "Could not found vmware tools lib in /usr/lib/vmware-tools.\nEnter path to the vmware-tools libs directory\nor install prefix[/usr/lib/vmware-tools]: "
    read mypath

    if [ -z "$mypath" ]; then mypath="/usr/lib/vmware-tools"; fi;

    if [ ! -d $mypath/modules/source ]; then 
	if [ ! -d $mypath/usr/lib/vmware-tools/modules/source ]; then
	    echo "Could not find $mypath/modules/source or $mypath/usr/lib/vmware-tools/modules/source"
	    exit 1;
	else
	    mypath="$mypath/usr/lib/vmware-tools/modules/source"
	fi;
    else 
	mypath="$mypath/modules/source"
    fi;
else
    mypath="/usr/lib/vmware-tools/modules/source"
fi;

echo "found $mypath"
cd $mypath
tar xvf vmxnet.tar > /dev/null
tar xvf vmxnet3.tar > /dev/null
if [ -d vmxnet3-only/shared ]; then shared="/shared"; else shared=""; fi;
cat << EOF >cloudlinux.patch
--- source/vmxnet3-only$shared/compat_netdevice.h.orig	2009-06-11 22:07:36.000000000 +0300
+++ source/vmxnet3-only$shared/compat_netdevice.h	2011-04-28 18:45:27.000000000 +0300
@@ -270,9 +270,11 @@ static inline int compat_unregister_netd
 #define compat_napi_enable(dev, napi)       napi_enable(napi)
 #define compat_napi_disable(dev, napi)      napi_disable(napi)
 #else
+/*
 struct napi_struct {
    int dummy;
 };
+*/
 
 #define compat_netif_napi_add(dev, napi, pollcb, quota) \\
    do {                        \\
diff -up source/vmxnet-only/compat_netdevice.h.orig source/vmxnet-only/compat_netdevice.h
--- source/vmxnet-only$shared/compat_netdevice.h.orig	2009-06-11 22:07:28.000000000 +0300
+++ source/vmxnet-only$shared/compat_netdevice.h	2011-04-28 18:46:43.000000000 +0300
@@ -270,9 +270,11 @@ static inline int compat_unregister_netd
 #define compat_napi_enable(dev, napi)       napi_enable(napi)
 #define compat_napi_disable(dev, napi)      napi_disable(napi)
 #else
+/*
 struct napi_struct {
    int dummy;
 };
+*/
 
 #define compat_netif_napi_add(dev, napi, pollcb, quota) \\
    do {
EOF

patch -p1 < cloudlinux.patch
if [ $? -ne 0 ]; then
rm -f cloudlinux.patch cloudlinux1.patch
rm -rf vmxnet-only vmxnet3-only
echo cleaning up everything without any changing
exit 1;
fi

rm -f vmxnet-only/shared/compat_netdevice.h.orig
rm -f vmxnet3-only/shared/compat_netdevice.h.orig
patch -p1 < cloudlinux1.patch
tar cvf vmxnet.tar vmxnet-only &>/dev/null
tar cvf vmxnet3.tar vmxnet3-only &>/dev/null
rm -f cloudlinux.patch cloudlinux1.patch
rm -rf vmxnet-only vmxnet3-only
cd /
 
