#!/bin/bash
VER=9
DIR=/usr/lib/vmware/modules/source
cd $DIR || exit
echo
echo "[==============================================================================]"
echo "[ Geos One VMware Server 1.0.x Patchset                                        ]"
echo "[==============================================================================]"
echo "[ This Patchset is provided as-is                                              ]"
echo "[ I am not responsible for any kind of damage created by this patchset         ]"
echo "[==============================================================================]"
echo
echo "[==============================================================================]"
echo "[ Preparing Original Vmware Server 1.0.x Modules                               ]"
echo "[==============================================================================]"
echo
[ -e vmmon-only ] && rm -r vmmon-only
[ ! -e vmmon.tar.orig ] && echo "copying vmmon.tar to vmmon.tar.orig"
[ ! -e vmmon.tar.orig ] && cp -a vmmon.tar vmmon.tar.orig
echo "Unpacking $DIR/vmmon.tar.orig:"
tar xf vmmon.tar.orig || exit
[ -e vmnet-only ] && rm -r vmnet-only
[ ! -e vmnet.tar.orig ] && echo "copying vmnet.tar to vmnet.tar.orig"
[ ! -e vmnet.tar.orig ] && cp -a vmnet.tar vmnet.tar.orig
echo "Unpacking $DIR/vmnet.tar.orig:"
tar xf vmnet.tar.orig || exit
echo
echo "[==============================================================================]"
echo "[ Downloading and/or Unpacking Vmware Server 1.0.x Patchset Version $VER          ]"
echo "[==============================================================================]"
echo
[ ! -e vmware-modules-138.10-genpatches-$VER.tar.bz2 ] && echo "Downloading the Patchset"
[ ! -e vmware-modules-138.10-genpatches-$VER.tar.bz2 ] && wget http://ftp.disconnected-by-peer.at/vmware/vmware-modules-138.10-genpatches-$VER.tar.bz2
echo "Unpacking $DIR/vmware-modules-138.10-genpatches-$VER.tar.bz2:"
[ -e vmware-modules-138.10-genpatches-$VER.tar.bz2 ] && tar jxf vmware-modules-138.10-genpatches-$VER.tar.bz2
echo
echo "[==============================================================================]"
echo "[ Apply Vmware Server 1.0.x Module Patchset Version $VER                          ]"
echo "[==============================================================================]"
echo
echo "Patching $DIR/vmmon.tar.orig:"
echo "Patching $DIR/vmnet.tar.orig:"
echo
for f in $DIR/patches/* ; do
	echo "apply Patch $f"
	patch -p1 -i $f
	if [ $? -ne 0 ]; then
		echo
		echo "Patching FAILED!  Restoring original $DIR/vmmon.tar"
		[ -e vmmon-only ] && rm -rf vmmon-only
		echo "Patching FAILED!  Restoring original $DIR/vmnet.tar"
		[ -e vmnet-only ] && rm -rf vmnet-only
		echo
		[ -e patches ] && rm -rf patches
		exit 1
	fi
done
[ -e patches ] && rm -rf patches
echo
echo
echo "[==============================================================================]"
echo "[ Updating Vmware Server 1.0.x Module with Patchset Version $VER                  ]"
echo "[==============================================================================]"
echo
cd $DIR || exit
echo "Success!  Updating $DIR/vmmon.tar:"
tar cf vmmon.tar.new vmmon-only || exit
mv vmmon.tar.new vmmon.tar
[ -e vmmon-only ] && rm -rf vmmon-only
echo "Success!  Updating $DIR/vmnet.tar:"
tar cf vmnet.tar.new vmnet-only || exit
mv vmnet.tar.new vmnet.tar
[ -e vmnet-only ] && rm -rf vmnet-only
echo
echo "[==============================================================================]"
echo "[ Gratulation: Vmware Server 1.0.x Modules update finisched                    ]"
echo "[==============================================================================]"
echo
echo "Done.  Now telling vmware to rebuild all modules: "
echo
echo "# vmware-config.pl compile"
echo
echo "[==============================================================================]"
echo "[ Geos One VMware Server 1.0.x Patchset                                        ]"
echo "[==============================================================================]"
echo "[ This Patchset is provided as-is                                              ]"
echo "[ I am not responsible for any kind of damage created by this patchset         ]"
echo "[==============================================================================]"
echo
vmware-config.pl compile
