#!/bin/sh

#
# Script to build a kernel module in /tmp. Useful if the module sources
# are installed in read-only directory.
#
# Copyright (C) 2007 Sun Microsystems, Inc.
#
# This file is part of VirtualBox Open Source Edition (OSE), as
# available from http://www.virtualbox.org. This file is free software;
# you can redistribute it and/or modify it under the terms of the GNU
# General Public License (GPL) as published by the Free Software
# Foundation, in version 2 as it comes in the "COPYING" file of the
# VirtualBox OSE distribution. VirtualBox OSE is distributed in the
# hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
#
# The contents of this file may alternatively be used under the terms
# of the Common Development and Distribution License Version 1.0
# (CDDL) only, as it comes in the "COPYING.CDDL" file of the
# VirtualBox OSE distribution, in which case the provisions of the
# CDDL are applicable instead of those of the GPL.
#
# You may elect to license modified versions of this file under the
# terms and conditions of either the GPL or the CDDL or both.
#
# Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
# Clara, CA 95054 USA or visit http://www.sun.com if you need
# additional information or have any questions.
#

# Attempt to build using DKMS first
DKMS=`which dkms 2>/dev/null` 
if [ -n "$DKMS" ]
then
    echo "Attempting to install using DKMS"
    $DKMS status -m _MODULE_ | while read line
    # first, remove _any_ old module
    do
        if echo "$line" | grep -q added > /dev/null ||
           echo "$line" | grep -q built > /dev/null ||
           echo "$line" | grep -q installed > /dev/null; then
            version=`echo "$line" | sed "s/_MODULE_,\([^,]*\),.*/\1/;t;d"`
            info "  removing old DKMS module _MODULE_ version $version"
            $DKMS remove -m _MODULE_ -v $version --all
        fi
    done
    # there should not be any more matches
    status=`$DKMS status -m _MODULE_ -v _VERSION_`
    if echo $status | grep added > /dev/null ||
        echo $status | grep built > /dev/null ||
        echo $status | grep installed > /dev/null
    then
        $DKMS remove -m _MODULE_ -v _VERSION_ --all
    fi
    # finally install the module
    if $DKMS add -m _MODULE_ -v _VERSION_ &&
        $DKMS build -m _MODULE_ -v _VERSION_ &&
        $DKMS install -m _MODULE_ -v _VERSION_ --force
    then
        exit 0
    fi
    echo "Failed to install using DKMS, attempting to install without"
fi

# find a unique temp directory
num=0
while true; do
  tmpdir="/tmp/vbox.$num"
  if mkdir -m 0755 "$tmpdir" 2> /dev/null; then
    break
  fi
  num=`expr $num + 1`
  if  [ $num -gt 200 ]; then
    echo "Could not find a valid tmp directory"
    exit 1
  fi
done

if [ "$1" = "--save-module-symvers" ]; then
  shift
  SAVE_MOD_SYMVERS="$1"
  shift
fi

if [ "$1" = "--use-module-symvers" ]; then
  shift
  USE_MOD_SYMVERS="$1"
  shift
fi

# copy
cp -a ${0%/*}/* $tmpdir/
if [ -n "$USE_MOD_SYMVERS" ]; then
  cp $USE_MOD_SYMVERS $tmpdir/Module.symvers
fi

# make, cleanup if success
cd "$tmpdir"
if make "$@"; then
  if [ -n "$SAVE_MOD_SYMVERS" ]; then
    if [ -f Module.symvers ]; then
      cp -f Module.symvers $SAVE_MOD_SYMVERS
    else
      cat /dev/null > $SAVE_MOD_SYMVERS
    fi
  fi
  rm -rf $tmpdir
  exit 0
fi

# failure
exit 1
