#!/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
diff -ur source-orig/vmxnet3-only/$shared/compat_netdevice.h source/vmxnet3-only/shared/compat_netdevice.h
--- source-orig/vmxnet3-only$shared/compat_netdevice.h	2010-05-18 16:12:12.000000000 +0300
+++ source/vmxnet3-only$shared/compat_netdevice.h	2010-05-18 17:23:46.000000000 +0300
@@ -319,9 +319,11 @@
	 (dev)->weight = (quota);  \\
        } while (0)
 #      define compat_napi_complete(dev, napi) netif_rx_complete(dev)
+/*
        struct napi_struct {
           int dummy;
        };
+*/
 #   endif

 #endif
EOF
if [ -d vmxnet-only/shared ]; then shared="/shared"; else shared=""; fi;
cat << EOF >cloudlinux1.patch
diff -ur source-orig/vmxnet-only/shared/compat_netdevice.h source/vmxnet-only/shared/compat_netdevice.h
--- source-orig/vmxnet-only$shared/compat_netdevice.h	2010-05-18 17:17:36.000000000 +0300
+++ source/vmxnet-only$shared/compat_netdevice.h	2010-05-18 17:23:20.000000000 +0300
@@ -319,9 +319,11 @@
	 (dev)->weight = (quota);  \\
        } while (0)
 #      define compat_napi_complete(dev, napi) netif_rx_complete(dev)
+/*
        struct napi_struct {
           int dummy;
        };
+*/
 #   endif

 #endif
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 /
 
