#! /bin/sh

: ${UNAME:=`uname -r`}
: ${IS_SMP:="no"}

echo "Backing up modules for $UNAME"

MODDIR="/lib/modules/$UNAME/misc"
VMWARE_MODDIR="/usr/lib/vmware/modules/binary"

Fail()
{
    echo "$@" >&2
    exit 1
}

CheckDir()
{
    [ -d "$1" ] || Fail "Directory \"$1\" not found"
}

CheckDir "$MODDIR"
CheckDir "$VMWARE_MODDIR"

[ -z "$FORCE" ] || (echo "Removing $VMWARE_MODDIR/$UNAME"; rm -rf "$VMWARE_MODDIR/$UNAME")

[ ! -e "$VMWARE_MODDIR/$UNAME" ] || Fail "$VMWARE_MODDIR/$UNAME already exists"

mkdir "$VMWARE_MODDIR/$UNAME" || Fail "Failed to create $VMWARE_MODDIR/$UNAME"
mkdir "$VMWARE_MODDIR/$UNAME/objects" || Fail "Failed to create $VMWARE_MODDIR/$UNAME/objects"

MODULES=`ls -1 $MODDIR/vm* 2>/dev/null`

[ -n "$MODULES" ] || Fail "No VMWare modules found in $MODDIR"

echo "Found VMWare modules:"
echo "$MODULES"

cp -a $MODULES "$VMWARE_MODDIR/$UNAME/objects"

echo "\
UtsRelease $UNAME
ModVersion yes
SMP $IS_SMP
PageOffset C0000000
Comment VMWare modules for $UNAME
" > "$VMWARE_MODDIR/$UNAME/properties"
