#!/bin/bash

#
# This shell script attempts to detect certain features of the Linux
# distribution so that the ADM-XRC driver will build correctly.
#

function showhelp()
{
    echo 'Usage: '$0' [option ...]'
    echo ''
    echo 'Where '"'"option"'"' can be one of the following:'
    echo '  -class_simple <yes | no>        Specify whether or not the kernel uses'
    echo '                                  old-style '"'"'struct class_simple'"'"'.'
    echo '  -device_create_drvdata <yes | no>'
    echo '                                  Specify whether or not device_create'
    echo '                                  function has the drvdata argument.'
    echo '  -dpc <yes | no>                 Specify whether or not to defer CPU-'
    echo '                                  intensive interrupt processing to non-IRQ'
    echo '                                  context (default yes).'
    echo '  -include <path>                 Specify the path to the kernel header files.'
    echo '  -kernel <path>                  Specify the path to the kernel sources.'
    echo '  -kmem_cache_create_5 <yes | no> Specify whether kmem_cache_create takes 5'
    echo '                                  parameters (yes) or 6 (no). Use this option'
    echo '                                  if autodetection fails.'
    echo '  -kmem_cache_destroy_int <yes | no>'
    echo '                                  Specify whether or not kmem_cache_destroy'
    echo '                                  returns a value (yes) or is void (no). Use'
    echo '                                  this option if auto detection fails.'
    echo '  -kver <version>                 Specify kernel verion, eg. 2.4.10, in case'
    echo '                                  autodetection fails.'
    echo '  -pfn_range <yes | no>           Specify whether to use io_remap_pfn_range()'
    echo '                                  or io_remap_page_range() (Linux 2.6.x only),'
    echo '                                  in case autodection fails.'
    echo '  -resource_size_t <yes | no>     Specify whether or not Linux kernel header'
    echo '                                  files define '"'"'resource_size_t'"'"' type.'
    echo '  -sysroot <system root>          Specify the system root (for cross-'
    echo '                                  building).'
    echo '  -uintptr_t <yes | no>           Specify whether or not Linux kernel header'
    echo '                                  files define '"'"'uintptr_t'"'"' type.'
    echo '  -vm_fault <yes | no>            Specify whether or not VMA '"'"'no_page'"'"' handler'
    echo '                                  uses '"'"'struct vm_fault'"'"'.'
    echo '  -vma <yes | no>                 Specify whether or not remap_page_range()'
    echo '                                  requires a 2.6.x-style VMA argument, in case'
    echo '                                  autodetection fails.'
    echo ''
    echo 'To perform a cross-build, set the environment variables ARCH and CROSS_COMPILE'
    echo 'before running this script. For example:'
    echo ''
    echo '  $ ARCH=powerpc'
    echo '  $ CROSS_COMPILE=powerpc-e600-linux-gnualtivec-'
    echo '  $ export ARCH CROSS_COMPILE'
    echo '  $ PATH=/path/to/toolchain:$PATH'
    echo '  $ ./configure'
    echo ''
}

this_dir=`pwd`

RULES_FILE=".build_defs"
KERNEL_PATH_FILE=".kernel_path"

#
# Set up default values
#
BRIEF="no"
CLASS_SIMPLE=""
DEVICE_CREATE_DRVDATA=""
DPC="yes"
IO_REMAP_PFN_RANGE=""
KMEM_CACHE_CREATE_5=""
KMEM_CACHE_DESTROY_INT=""
LINUX_KERNEL="/lib/modules/`uname -r`/build"
QUIET="yes"
READ_PCI_INT_PIN=""
RESOURCE_SIZE=""
SYSROOT=""
TARGET="linux"
UINTPTR=""
UTS_RELEASE=""
VM_FAULT=""
VMA_ARG=""

while (($# > 0)) ; do
    if [ "$1" == "-help" ] || [ "$1" == "-h" ] ; then
        showhelp
        exit 0
    elif [ "$1" == "-sysroot" ] ; then
        if [ "$2" == "" ] ; then
            echo "*** -sysroot option requires path argument."
            echo
            showhelp
            exit 1
        fi
        SYSROOT="$2"
        shift 2
    elif [ "$1" == "-class_simple" ] ; then
        CLASS_SIMPLE="$2"
        if [ "$2" != "yes" ] && [ "$2" != "no" ] ; then
            echo "*** $2 is not a valid choice for the -class_simple option."
            echo
            showhelp
            exit 1
        fi
        shift 2
    elif [ "$1" == "-device_create_drvdata" ] ; then
        DEVICE_CREATE_DRVDATA="$2"
        if [ "$2" != "yes" ] && [ "$2" != "no" ] ; then
            echo "*** $2 is not a valid choice for the -device_create_drvdata option."
            echo
            showhelp
            exit 1
        fi
        shift 2
    elif [ "$1" == "-dpc" ] ; then
        DPC="$2"
        if [ "$2" != "yes" ] && [ "$2" != "no" ] ; then
            echo "*** $2 is not a valid choice for the -dpc option."
            echo
            showhelp
            exit 1
        fi
        shift 2
    elif [ "$1" == "-include" ] ; then
        if [ "$2" == "" ] ; then
            echo "*** -include option requires directory argument."
            echo
            showhelp
            exit 1
        fi
        LINUX_INCLUDE="$2"
        shift 2
    elif [ "$1" == "-kernel" ] ; then
        if [ "$2" == "" ] ; then
            echo "*** -kernel option requires directory argument."
            echo
            showhelp
            exit 1
        fi
        LINUX_KERNEL="$2"
        shift 2
    elif [ "$1" == "-kver" ] ; then
        if [ "$2" == "" ] ; then
            echo "*** -kver option requires kernel version argument."
            echo
            showhelp
            exit 1
        fi
        UTS_RELEASE="$2"
        shift 2
    elif [ "$1" == "-vma" ] ; then
        VMA_ARG="$2"
        if [ "$VMA_ARG" != "yes" ] && [ "$VMA_ARG" != "no" ] ; then
            echo "*** $VMA_ARG is not a valid choice for the -vma option."
               echo
            showhelp
            exit 1
        fi
        shift 2
    elif [ "$1" == "-pfn_range" ] ; then
        IO_REMAP_PFN_RANGE="$2"
        if [ "$IO_REMAP_PFN_RANGE" != "yes" ] && [ "$IO_REMAP_PFN_RANGE" != "no" ] ; then
            echo "*** $IO_REMAP_PFN_RANGE is not a valid choice for the -pfn_range option."
            echo
            showhelp
            exit 1
        fi
        shift 2
    elif [ "$1" == "-kmem_cache_create_5" ] ; then
        KMEM_CACHE_CREATE_5="$2"
        if [ "$KMEM_CACHE_CREATE_5" != "yes" ] && [ "$KMEM_CACHE_CREATE_5" != "no" ] ; then
            echo "*** $KMEM_CACHE_CREATE_5 is not a valid choice for the -kmem_cache_create_5 option."
            echo
            showhelp
            exit 1
        fi
        shift 2
    elif [ "$1" == "-kmem_cache_destroy_int" ] ; then
        KMEM_CACHE_DESTROY_INT="$2"
        if [ "$KMEM_CACHE_DESTROY_INT" != "yes" ] && [ "$KMEM_CACHE_DESTROY_INT" != "no" ] ; then
            echo "*** $KMEM_CACHE_DESTROY_INT is not a valid choice for the -kmem_cache_destroy_int option."
            echo
            showhelp
            exit 1
        fi
        shift 2
    elif [ "$1" == "-read_pci_int_pin" ] ; then
        READ_PCI_INT_PIN="$2"
        if [ "$READ_PCI_INT_PIN" != "yes" ] && [ "$READ_PCI_INT_PIN" != "no" ] ; then
            echo "*** $READ_PCI_INT_PIN is not a valid choice for the -read_pci_int_pin option."
            echo
            showhelp
            exit 1
        fi
        shift 2
    elif [ "$1" == "-resource_size_t" ] ; then
        RESOURCE_SIZE="$2"
        if [ "$RESOURCE_SIZE" != "yes" ] && [ "$RESOURCE_SIZE" != "no" ] ; then
            echo "*** $RESOURCE_SIZE is not a valid choice for the -resource_size_t option."
            echo
            showhelp
            exit 1
        fi
        shift 2
    elif [ "$1" == "-uintptr" ] ; then
        UINTPTR="$2"
        if [ "$UINTPTR" != "yes" ] && [ "$UINTPTR" != "no" ] ; then
            echo "*** $UINTPTR is not a valid choice for the -uintptr_t option."
            echo
            showhelp
            exit 1
        fi
        shift 2
    elif [ "$1" == "-vm_fault" ] ; then
        VM_FAULT="$2"
        if [ "$VM_FAULT" != "yes" ] && [ "$VM_FAULT" != "no" ] ; then
            echo "*** $VM_FAULT is not a valid choice for the -vm_fault option."
            echo
            showhelp
            exit 1
        fi
        shift 2
    elif [ "$1" == "-v" ] ; then
        QUIET="no"
        shift 1
    elif [ "$1" == "-brief" ] ; then
        BRIEF="yes"
        shift 1
    else
        echo "*** Unknown option: $1"
        exit 1
    fi
done

if [ "$BRIEF" == "no" ] ; then
    echo "ADM-XRC Linux Driver configuration script"
    echo
fi

if [ "$LINUX_INCLUDE" == "" ] ; then
    LINUX_INCLUDE="$LINUX_KERNEL/include"
fi

if [ ! -d "$LINUX_KERNEL" ] ; then
    echo "WARNING: The directory $LINUX_KERNEL was not found."
    echo "         It is likely that compilation will fail. Use the '-kernel' option to"
    echo "         specify the path to the kernel source files."
fi

if [ ! -d "$LINUX_INCLUDE" ] ; then
    echo "WARNING: The directory $LINUX_INCLUDE was not found."
    echo "         It is likely that compilation will fail. Use the '-include' option"
    echo "         to specify the correct path to specify the path to the kernel header"
    echo "         files, or use the '-kernel' option to specify the path to the kernel"
    echo "         source files."
fi

if [ "$UTS_RELEASE" == "" ] ; then
    if [ -f "$LINUX_INCLUDE/generated/utsrelease.h" ] ; then
        if grep UTS_RELEASE $LINUX_INCLUDE/generated/utsrelease.h > /dev/null 2>&1 ; then
            UTS_RELEASE=`grep UTS_RELEASE $LINUX_INCLUDE/generated/utsrelease.h | awk -- '{ if (FNR==1) { print $3 } }'`
            UTS_RELEASE=`echo $UTS_RELEASE | sed -e 's/"//g'`
        else
            echo "WARNING: The file"
            echo ""
            echo "           $LINUX_INCLUDE/generated/utsrelease.h"
            echo ""
            echo "         does not contain a definition for UTS_RELEASE. It is likely"
            echo "         that an attempt to build this driver will fail."
            UTS_RELEASE="0.0.0"
        fi
    elif [ -f "$LINUX_INCLUDE/linux/utsrelease.h" ] ; then
        if grep UTS_RELEASE $LINUX_INCLUDE/linux/utsrelease.h > /dev/null 2>&1 ; then
            UTS_RELEASE=`grep UTS_RELEASE $LINUX_INCLUDE/linux/utsrelease.h | awk -- '{ if (FNR==1) { print $3 } }'`
            UTS_RELEASE=`echo $UTS_RELEASE | sed -e 's/"//g'`
        else
            echo "WARNING: The file"
            echo ""
            echo "           $LINUX_INCLUDE/linux/utsrelease.h"
            echo ""
            echo "         does not contain a definition for UTS_RELEASE. It is likely"
            echo "         that an attempt to build this driver will fail."
            UTS_RELEASE="0.0.0"
        fi
    elif [ -f "$LINUX_INCLUDE/linux/version.h" ] ; then
        if grep UTS_RELEASE $LINUX_INCLUDE/linux/version.h > /dev/null 2>&1 ; then
            UTS_RELEASE=`grep UTS_RELEASE $LINUX_INCLUDE/linux/version.h | awk -- '{ if (FNR==1) { print $3 } }'`
            UTS_RELEASE=`echo $UTS_RELEASE | sed -e 's/"//g'`
        else
            echo "WARNING: The file"
            echo ""
            echo "           $LINUX_INCLUDE/linux/version.h"
            echo ""
            echo "         does not contain a definition for UTS_RELEASE. It is likely"
            echo "         that an attempt to build this driver will fail."
            UTS_RELEASE="0.0.0"
        fi
    else
        echo "WARNING: Neither of the files"
        echo ""
        echo "           $LINUX_INCLUDE/linux/version.h"
        echo "           $LINUX_INCLUDE/linux/utsrelease.h"
        echo ""
        echo "         were found. This may indicate that the Linux kernel sources at"
        echo ""
        echo "           $LINUX_KERNEL"
        echo ""
        echo "         have not been configured and/or compiled. Unless either of these"
        echo "         files are present, it is likely that an attempt to build this"
        echo "         driver will fail."
        UTS_RELEASE="0.0.0"
    fi
fi

UTS_RELEASE_1=`echo $UTS_RELEASE | awk -F . -- '{ print $1 }'`
UTS_RELEASE_2=`echo $UTS_RELEASE | awk -F . -- '{ print $2 }'`
UTS_RELEASE_3=`echo $UTS_RELEASE | sed -e "s/$UTS_RELEASE_1\\.$UTS_RELEASE_2\\.//g"`

ok=0
if [ "$UTS_RELEASE_1" == "2" ] ; then
    case $UTS_RELEASE_2 in
        3)
        ok=1
        makefile="Makefile-2.4"
        MODEXT=".o"
        ;;
        4)
        ok=1
        makefile="Makefile-2.4"
        MODEXT=".o"
        ;;
        5)
        ok=1
        makefile="Makefile-2.6"
        MODEXT=".ko"
        ;;
        6)
        ok=1
        makefile="Makefile-2.6"
        MODEXT=".ko"
        ;;
    esac
elif [ "$UTS_RELEASE_1" -ge "3" ] ; then
    ok=1
    makefile="Makefile-2.6"
    MODEXT=".ko"
fi
if [ "$ok" == 0 ] ; then
    echo "*** Unsupported Linux kernel version: $UTS_RELEASE"
    exit 1
fi

if [ "$VMA_ARG" == "" ] ; then
    # Linux kernel 2.5.x/2.6.x/3+.x requires VMA argument with remap_page_range()
    if [ "$UTS_RELEASE_1" == "2" ] && [ "$UTS_RELEASE_2" == "5" ] ; then
        VMA_ARG="yes"
    fi
    if [ "$UTS_RELEASE_1" == "2" ] && [ "$UTS_RELEASE_2" == "6" ] ; then
        VMA_ARG="yes"
    fi
    if [ "$UTS_RELEASE_1" -ge "3" ] ; then
        VMA_ARG="yes"
    fi
fi

(cd configure-scripts; rm -f `find . -noleaf -name '*.o' -print`; rm -f `find . -noleaf -name '*.ko' -print`)

if [ "$CLASS_SIMPLE" == "" ] ; then
    if [ "$UTS_RELEASE_1" == "2" ] && [ "$UTS_RELEASE_2" == "6" ] ; then
        if [ "$QUIET" == "yes" ] ; then
            (cd $LINUX_KERNEL; make SUBDIRS=$this_dir/configure-scripts/class_simple modules) >/dev/null 2>/dev/null
        else
            (cd $LINUX_KERNEL; make SUBDIRS=$this_dir/configure-scripts/class_simple modules)
        fi
        if [ -f $this_dir/configure-scripts/class_simple/test_mod.ko ] ; then
            CLASS_SIMPLE="yes"
        else
            CLASS_SIMPLE="no"
        fi
    else
        CLASS_SIMPLE="no"
    fi
fi

if [ "$CLASS_SIMPLE" == "no" ] ; then
    if [ "$DEVICE_CREATE_DRVDATA" == "" ] ; then
        if [ "$QUIET" == "yes" ] ; then
            (cd $LINUX_KERNEL; make SUBDIRS=$this_dir/configure-scripts/device_create_drvdata modules) >/dev/null 2>/dev/null
        else
            (cd $LINUX_KERNEL; make SUBDIRS=$this_dir/configure-scripts/device_create_drvdata modules)
        fi
        if [ -f $this_dir/configure-scripts/device_create_drvdata/test_mod.ko ] ; then
            DEVICE_CREATE_DRVDATA="yes"
        else
            DEVICE_CREATE_DRVDATA="no"
        fi
    fi
fi

if [ "$IO_REMAP_PFN_RANGE" == "" ] ; then
    if [ "$UTS_RELEASE_1" == "2" ] && [ "$UTS_RELEASE_2" == "6" ] ; then
        if [ "$QUIET" == "yes" ] ; then
            (cd $LINUX_KERNEL; make SUBDIRS=$this_dir/configure-scripts/io_remap_pfn_range modules) >/dev/null 2>/dev/null
        else
            (cd $LINUX_KERNEL; make SUBDIRS=$this_dir/configure-scripts/io_remap_pfn_range modules)
        fi
        if [ -f $this_dir/configure-scripts/io_remap_pfn_range/test_mod.ko ] ; then
            IO_REMAP_PFN_RANGE="yes"
        else
            IO_REMAP_PFN_RANGE="no"
        fi
    elif [ "$UTS_RELEASE_1" -ge "3" ] ; then
        IO_REMAP_PFN_RANGE="yes"
    else
        IO_REMAP_PFN_RANGE="no"
    fi
fi

if [ "$KMEM_CACHE_CREATE_5" == "" ] ; then
    if [ "$UTS_RELEASE_1" == "2" ] && [ "$UTS_RELEASE_2" == "6" ] ; then
        if [ "$QUIET" == "yes" ] ; then
            (cd $LINUX_KERNEL; make SUBDIRS=$this_dir/configure-scripts/kmem_cache_create_5 modules) >/dev/null 2>/dev/null
        else
            (cd $LINUX_KERNEL; make SUBDIRS=$this_dir/configure-scripts/kmem_cache_create_5 modules)
        fi
        if [ "$QUIET" == "yes" ] ; then
            (cd $LINUX_KERNEL; make SUBDIRS=$this_dir/configure-scripts/kmem_cache_create_6 modules) >/dev/null 2>/dev/null
        else
            (cd $LINUX_KERNEL; make SUBDIRS=$this_dir/configure-scripts/kmem_cache_create_6 modules)
        fi
        if [ -f $this_dir/configure-scripts/kmem_cache_create_5/test_mod.ko ] ; then
            KMEM_CACHE_CREATE_5="yes"
        else
            if [ -f $this_dir/configure-scripts/kmem_cache_create_6/test_mod.ko ] ; then
                KMEM_CACHE_CREATE_5="no"
            else
                # Couldn't determine, but we'll assume 6 args
                echo "WARNING: Couldn't determine number of args to use for kmem_cache_create()."
                echo "         Assuming 6."
                KMEM_CACHE_CREATE_5="no"
            fi
        fi
    elif [ "$UTS_RELEASE_1" -ge "3" ] ; then
        KMEM_CACHE_CREATE_5="yes"
    else
        KMEM_CACHE_CREATE_5="no"
    fi
fi

if [ "$KMEM_CACHE_DESTROY_INT" == "" ] ; then
    if [ "$UTS_RELEASE_1" == "2" ] && [ "$UTS_RELEASE_2" == "6" ] ; then
        if [ "$QUIET" == "yes" ] ; then
            (cd $LINUX_KERNEL; make SUBDIRS=$this_dir/configure-scripts/kmem_cache_destroy_int modules) >/dev/null 2>/dev/null
        else
            (cd $LINUX_KERNEL; make SUBDIRS=$this_dir/configure-scripts/kmem_cache_destroy_int modules)
        fi
        if [ -f $this_dir/configure-scripts/kmem_cache_destroy_int/test_mod.ko ] ; then
            KMEM_CACHE_DESTROY_INT="yes"
        else
            KMEM_CACHE_DESTROY_INT="no"
        fi
    elif [ "$UTS_RELEASE_1" -ge "3" ] ; then
        KMEM_CACHE_DESTROY_INT="yes"
    else
        KMEM_CACHE_DESTROY_INT="no"
    fi
fi

if [ "$READ_PCI_INT_PIN" == "" ] ; then
    if [ "$UTS_RELEASE_1" == "2" ] && [ "$UTS_RELEASE_2" == "6" ] ; then
        if [ "$QUIET" == "yes" ] ; then
            (cd $LINUX_KERNEL; make SUBDIRS=$this_dir/configure-scripts/pci_int_pin modules) >/dev/null 2>/dev/null
        else
            (cd $LINUX_KERNEL; make SUBDIRS=$this_dir/configure-scripts/pci_int_pin modules)
        fi
        if [ -f $this_dir/configure-scripts/pci_int_pin/test_mod.ko ] ; then
            READ_PCI_INT_PIN="no"
        else
            READ_PCI_INT_PIN="yes"
        fi
    elif [ "$UTS_RELEASE_1" -ge "3" ] ; then
        READ_PCI_INT_PIN="yes"
    else
        READ_PCI_INT_PIN="yes"
    fi
fi

if [ "$RESOURCE_SIZE" == "" ] ; then
    if [ "$UTS_RELEASE_1" == "2" ] && [ "$UTS_RELEASE_2" == "6" ] ; then
        if [ "$QUIET" == "yes" ] ; then
            (cd $LINUX_KERNEL; make SUBDIRS=$this_dir/configure-scripts/resource_size_t modules) >/dev/null 2>/dev/null
        else
            (cd $LINUX_KERNEL; make SUBDIRS=$this_dir/configure-scripts/resource_size_t modules)
        fi
        if [ -f $this_dir/configure-scripts/resource_size_t/test_mod.ko ] ; then
            RESOURCE_SIZE="yes"
        else
            RESOURCE_SIZE="no"
        fi
    elif [ "$UTS_RELEASE_1" -ge "3" ] ; then
        RESOURCE_SIZE="yes"
    else
        RESOURCE_SIZE="no"
    fi
fi

if [ "$UINTPTR" == "" ] ; then
    if [ "$UTS_RELEASE_1" == "2" ] && [ "$UTS_RELEASE_2" == "6" ] ; then
        if [ "$QUIET" == "yes" ] ; then
            (cd $LINUX_KERNEL; make SUBDIRS=$this_dir/configure-scripts/uintptr_t modules) >/dev/null 2>/dev/null
        else
            (cd $LINUX_KERNEL; make SUBDIRS=$this_dir/configure-scripts/uintptr_t modules)
        fi
        if [ -f $this_dir/configure-scripts/uintptr_t/test_mod.ko ] ; then
            UINTPTR="yes"
        else
            UINTPTR="no"
        fi
    elif [ "$UTS_RELEASE_1" -ge "3" ] ; then
        UINTPTR="yes"
    else
        UINTPTR="no"
    fi
fi

if [ "$VM_FAULT" == "" ] ; then
    if [ "$UTS_RELEASE_1" == "2" ] && [ "$UTS_RELEASE_2" == "6" ] ; then
        if [ "$QUIET" == "yes" ] ; then
            (cd $LINUX_KERNEL; make SUBDIRS=$this_dir/configure-scripts/vm_fault modules) >/dev/null 2>/dev/null
        else
            (cd $LINUX_KERNEL; make SUBDIRS=$this_dir/configure-scripts/vm_fault modules)
        fi
        if [ -f $this_dir/configure-scripts/vm_fault/test_mod.ko ] ; then
            VM_FAULT="yes"
            else
            VM_FAULT="no"
        fi
    elif [ "$UTS_RELEASE_1" -ge "3" ] ; then
        VM_FAULT="yes"
    else
        VM_FAULT="no"
    fi
fi

if [ "$VMA_ARG" == "" ] ; then
    if [ "$QUIET" == "yes" ] ; then
        (cd $LINUX_KERNEL; make SUBDIRS=$this_dir/configure-scripts/remap_page_range modules) >/dev/null 2>/dev/null
        (cd $LINUX_KERNEL; make SUBDIRS=$this_dir/configure-scripts/remap_page_range_vma modules) >/dev/null 2>/dev/null
    else
        (cd $LINUX_KERNEL; make SUBDIRS=$this_dir/configure-scripts/remap_page_range modules)
        (cd $LINUX_KERNEL; make SUBDIRS=$this_dir/configure-scripts/remap_page_range_vma modules)
    fi
    if [ -f $this_dir/configure-scripts/remap_page_range/test-obj.o ] ; then
        VMA_ARG="no"
    fi
    if [ -f $this_dir/configure-scripts/remap_page_range_vma/test-obj.o ] ; then
        VMA_ARG="yes"
    fi
fi

#
# Emit the file that contains the kernel sources path
#
rm -f $KERNEL_PATH_FILE
echo "#" >>$KERNEL_PATH_FILE
echo "# $KERNEL_PATH_FILE" >>$KERNEL_PATH_FILE
echo "# Generated automatically by configure - do not modify (unless" >>$KERNEL_PATH_FILE
echo "# you know what you're doing!)" >>$KERNEL_PATH_FILE
echo "#" >>$KERNEL_PATH_FILE
echo "" >>$KERNEL_PATH_FILE

echo "# The following set up the kernel directories" >>$KERNEL_PATH_FILE
echo "Linux kernel directory:            $LINUX_KERNEL"
echo "LINUX_KERNEL=$LINUX_KERNEL" >>$KERNEL_PATH_FILE

#
# Emit the build rules file
#
rm -f $RULES_FILE
echo "#" >>$RULES_FILE
echo "# .build_defs" >>$RULES_FILE
echo "# Generated automatically by configure - do not modify (unless" >>$RULES_FILE
echo "# you know what you're doing!)" >>$RULES_FILE
echo "#" >>$RULES_FILE
echo "" >>$RULES_FILE

echo "Linux kernel version:              $UTS_RELEASE_1.$UTS_RELEASE_2.$UTS_RELEASE_3"
echo "# Version of Linux kernel used." >>$RULES_FILE
echo "UTS_RELEASE=$UTS_RELEASE" >>$RULES_FILE
echo "UTS_RELEASE_1=$UTS_RELEASE_1" >>$RULES_FILE
echo "UTS_RELEASE_2=$UTS_RELEASE_2" >>$RULES_FILE
echo "UTS_RELEASE_3=$UTS_RELEASE_3" >>$RULES_FILE
echo "Linux kernel module extension:     $MODEXT"
echo "MODEXT=$MODEXT" >>$RULES_FILE
echo "" >>$RULES_FILE
echo "Kernel module install prefix:      $SYSROOT"
echo "# SYSROOT sets the prefix for the install directory for the kernel module." >>$RULES_FILE
echo "SYSROOT=$SYSROOT" >>$RULES_FILE
echo "" >>$RULES_FILE

echo "# At some point in the development of the 2.6.x Linux kernel line, the" >>$RULES_FILE
echo "# 'struct class_simple' was removed in favour of 'struct class'" >>$RULES_FILE
echo "# We detect the existence of the old-style 'class_simple'." >>$RULES_FILE
if [ "$CLASS_SIMPLE" == "yes" ] ; then
    echo "Use struct class_simple:           yes"
    echo "EXTRA_CFLAGS += -DUSE_CLASS_SIMPLE=1" >>$RULES_FILE
else
    echo "Use struct class_simple:           no"
    echo "EXTRA_CFLAGS += -DUSE_CLASS_SIMPLE=0" >>$RULES_FILE
fi
echo "" >>$RULES_FILE

echo "# At some point in the development of the 2.6.x Linux kernel line, the" >>$RULES_FILE
echo "# 'device_create' function gained a 'drvdata' parameter." >>$RULES_FILE
echo "# We detect the existence of this parameter." >>$RULES_FILE
if [ "$DEVICE_CREATE_DRVDATA" == "yes" ] ; then
    echo "device_create has drvdata param:   yes"
    echo "EXTRA_CFLAGS += -DDEVICE_CREATE_DRVDATA=1" >>$RULES_FILE
else
    echo "device_create has drvdata param:   no"
    echo "EXTRA_CFLAGS += -DDEVICE_CREATE_DRVDATA=0" >>$RULES_FILE
fi
echo "" >>$RULES_FILE

echo "# At some point in the development of the 2.6.x Linux kernel line, the" >>$RULES_FILE
echo "# function io_remap_pfn_range() appeared, which obsoleted the function" >>$RULES_FILE
echo "# io_remap_page_range(). We detect the existence of io_remap_pfn_range()" >>$RULES_FILE
echo "# and use it if present." >>$RULES_FILE
if [ "$IO_REMAP_PFN_RANGE" == "yes" ] ; then
    echo "Use io_remap_pfn_range():          yes"
    echo "EXTRA_CFLAGS += -DUSE_IO_REMAP_PFN_RANGE=1" >>$RULES_FILE
else
    echo "Use io_remap_pfn_range():          no"
    echo "EXTRA_CFLAGS += -DUSE_IO_REMAP_PFN_RANGE=0" >>$RULES_FILE
fi
echo "" >>$RULES_FILE

echo "# As of kernel 2.6.23, kmem_cache_create has 5 parameters. Prior" >>$RULES_FILE
echo "# to that, it had 6. We detect the number of parameters." >>$RULES_FILE
if [ "$KMEM_CACHE_CREATE_5" == "yes" ] ; then
    echo "kmem_cache_create 5 parameters:    yes"
    echo "EXTRA_CFLAGS += -DKMEM_CACHE_CREATE_5=1" >>$RULES_FILE
else
    echo "kmem_cache_create 5 parameters:    no"
    echo "EXTRA_CFLAGS += -DKMEM_CACHE_CREATE_5=0" >>$RULES_FILE
fi
echo "" >>$RULES_FILE

echo "# As of kernel 2.6.19, kmem_cache_destroy has return type void. Prior" >>$RULES_FILE
echo "# to that, it had return type int. We detect the return type." >>$RULES_FILE
if [ "$KMEM_CACHE_DESTROY_INT" == "yes" ] ; then
    echo "kmem_cache_destroy returns int:    yes"
    echo "EXTRA_CFLAGS += -DKMEM_CACHE_DESTROY_INT=1" >>$RULES_FILE
else
    echo "kmem_cache_destroy returns int:    no"
    echo "EXTRA_CFLAGS += -DKMEM_CACHE_DESTROY_INT=0" >>$RULES_FILE
fi
echo "" >>$RULES_FILE

echo "# At some point in the 2.6.x kernel line, the 'pin' member was added" >>$RULES_FILE
echo "# to struct pci_dev. We detect whether the 'pin' member is present in" >>$RULES_FILE
echo "# struct pci_dev in the kernel header files we are compiling for." >>$RULES_FILE
if [ "$READ_PCI_INT_PIN" == "yes" ] ; then
    echo "Read PCI IRQ pin from device:      yes"
    echo "EXTRA_CFLAGS += -DSTRUCT_PCI_DEV_HAS_PIN=0" >>$RULES_FILE
else
    echo "Read PCI IRQ pin from device:      no"
    echo "EXTRA_CFLAGS += -DSTRUCT_PCI_DEV_HAS_PIN=1" >>$RULES_FILE
fi
echo "" >>$RULES_FILE

echo "# At some point in the 2.6.x kernel line, the 'resource_size_t'" >>$RULES_FILE
echo "# type was added. We detect whether the 'resource_size_t' type" >>$RULES_FILE
echo "# is defined in the kernel header files we are compiling for." >>$RULES_FILE
if [ "$RESOURCE_SIZE" == "yes" ] ; then
    echo "resource_size_t is defined:        yes"
    echo "EXTRA_CFLAGS += -DHAS_RESOURCE_SIZE_T=1" >>$RULES_FILE
else
    echo "resource_size_t is defined:        no"
    echo "EXTRA_CFLAGS += -DHAS_RESOURCE_SIZE_T=0" >>$RULES_FILE
fi
echo "" >>$RULES_FILE

echo "# At some point in the 2.6.x kernel line, the 'uintptr_t' type was" >>$RULES_FILE
echo "# added. We detect whether the 'uintptr_t' type is defined in the" >>$RULES_FILE
echo "# kernel header files we are compiling for." >>$RULES_FILE
if [ "$UINTPTR" == "yes" ] ; then
    echo "uintptr_t is defined:              yes"
    echo "EXTRA_CFLAGS += -DHAS_UINTPTR_T=1" >>$RULES_FILE
else
    echo "uintptr_t is defined:              no"
    echo "EXTRA_CFLAGS += -DHAS_UINTPTR_T=0" >>$RULES_FILE
fi
echo "" >>$RULES_FILE

echo "# At some point in the 2.6.x kernel line, the 'no_page' handler" >>$RULES_FILE
echo "# for VMA operations was changed to pass in a pointer to 'struct" >>$RULES_FILE
echo "# vm_fault. We detect what kind of 'no_page' handler declaration"  >>$RULES_FILE
echo "# is required for the kernel we are compiling for." >>$RULES_FILE
if [ "$VM_FAULT" == "yes" ] ; then
    echo "no_page uses 'struct vm_fault'     yes"
    echo "EXTRA_CFLAGS += -DNO_PAGE_STRUCT_VM_FAULT=1" >>$RULES_FILE
else
    echo "no_page uses 'struct vm_fault'     no"
    echo "EXTRA_CFLAGS += -DNO_PAGE_STRUCT_VM_FAULT=0" >>$RULES_FILE
fi
echo "" >>$RULES_FILE

echo "# Certain Redhat Linux kernels (eg. 2.4.20-18.8) incorporate 2.6.x" >>$RULES_FILE
echo "# functionality, such as a modified prototype for remap_page_range()" >>$RULES_FILE
echo "# that requires an extra parameter" >>$RULES_FILE
if [ "$VMA_ARG" == "yes" ] ; then
    echo "remap_page_range() with VMA arg:   yes"
    echo "EXTRA_CFLAGS += -DREMAP_PAGE_RANGE_WITH_VMA_ARG=1" >>$RULES_FILE
else
    echo "remap_page_range() with VMA arg:   no"
    echo "EXTRA_CFLAGS += -DREMAP_PAGE_RANGE_WITH_VMA_ARG=0" >>$RULES_FILE
fi
echo "" >>$RULES_FILE

if [ "$BRIEF" == "no" ] ; then
    echo
    echo "Configured, configuration is in $RULES_FILE & $KERNEL_PATH_FILE. Now do:"
    echo "  o 'make clean all' if you are an unprivileged user."
    echo "  o 'make clean all install' if you are root and building from a system"
    echo "     directory."
    echo
fi
