#!/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 'The following general options are available:'
    echo ''
    echo '  -h or -help                     Show this help.'
    echo '  -sysroot <system root>          Specify the system root (for cross-'
    echo '                                  building).'
    echo '  -v                              Causes this script to execute verbosely'
    echo ''
    echo 'The following driver options are available:'
    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 '  -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 'The following API shared library options are available:'
    echo '  -biarch <yes | no>              Applicable to 64-bit targets only; specify '
    echo '                                  '"'"'yes'"'"' in order to build native (64-bit) and '
    echo '                                  compatibility (32-bit) binaries. Default is'
    echo '                                  no.'
    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 ''
}

driver_options="-brief"
api_options="-brief"

while (($# > 0)) ; do
    if [ "$1" == "-help" ] || [ "$1" == "-h" ] ; then
        showhelp
	exit 0
    elif [ "$1" == "-v" ] ; then
        driver_options="$driver_options -v"
        api_options="$api_options -v"
        shift 1
    elif [ "$1" == "-biarch" ] ; then
	if [ "$2" != "yes" ] && [ "$2" != "no" ] ; then
	    echo "*** $2 is not a valid choice for the -biarch option."
	    echo
	    showhelp
	    exit 1
	fi
        api_options="$api_options $1 $2"
	shift 2
    elif [ "$1" == "-class_simple" ] ; then
	if [ "$2" != "yes" ] && [ "$2" != "no" ] ; then
	    echo "*** $2 is not a valid choice for the -class_simple option."
	    echo
	    showhelp
	    exit 1
	fi
        driver_options="$driver_options $1 $2"
	shift 2
    elif [ "$1" == "-device_create_drvdata" ] ; then
	if [ "$2" != "yes" ] && [ "$2" != "no" ] ; then
	    echo "*** $2 is not a valid choice for the -device_create_drvdata option."
	    echo
	    showhelp
	    exit 1
	fi
        driver_options="$driver_options $1 $2"
	shift 2
    elif [ "$1" == "-dpc" ] ; then
	if [ "$2" != "yes" ] && [ "$2" != "no" ] ; then
	    echo "*** $2 is not a valid choice for the -dpc option."
	    echo
	    showhelp
	    exit 1
	fi
        driver_options="$driver_options $1 $2"
	shift 2
    elif [ "$1" == "-include" ] ; then
	if [ "$2" == "" ] ; then
	    echo "*** -include option requires directory argument."
	    echo
	    showhelp
	    exit 1
	fi
        driver_options="$driver_options $1 $2"
	shift 2
    elif [ "$1" == "-kernel" ] ; then
	if [ "$2" == "" ] ; then
	    echo "*** -kernel option requires directory argument."
	    echo
	    showhelp
	    exit 1
	fi
        driver_options="$driver_options $1 $2"
	shift 2
    elif [ "$1" == "-kver" ] ; then
	if [ "$2" == "" ] ; then
	    echo "*** -kver option requires kernel version argument."
	    echo
	    showhelp
	    exit 1
	fi
        driver_options="$driver_options $1 $2"
	shift 2
    elif [ "$1" == "-vma" ] ; then
	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
        driver_options="$driver_options $1 $2"
        shift 2
    elif [ "$1" == "-pfn_range" ] ; then
	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
        driver_options="$driver_options $1 $2"
        shift 2
    elif [ "$1" == "-kmem_cache_create_5" ] ; then
	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
        driver_options="$driver_options $1 $2"
        shift 2
    elif [ "$1" == "-kmem_cache_destroy_int" ] ; then
	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
        driver_options="$driver_options $1 $2"
        shift 2
    elif [ "$1" == "-read_pci_int_pin" ] ; then
	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
        driver_options="$driver_options $1 $2"
        shift 2
    elif [ "$1" == "-resource_size_t" ] ; then
	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
        driver_options="$driver_options $1 $2"
        shift 2
    elif [ "$1" == "-uintptr" ] ; then
	if [ "$UINTPTR" != "yes" ] && [ "$UINTPTR" != "no" ] ; then
	    echo "*** $UINTPTR is not a valid choice for the -uintptr_t option."
   	    echo
	    showhelp
	    exit 1
	fi
        driver_options="$driver_options $1 $2"
        shift 2
    elif [ "$1" == "-vm_fault" ] ; then
	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
        driver_options="$driver_options $1 $2"
        shift 2
    elif [ "$1" == "-arch" ] ; then
	if [ "$2" == "" ] ; then
	    echo "*** -arch option requires CPU architecture argument."
	    echo
	    showhelp
	    exit 1
	fi
	api_options="$api_options $1 $2"
	shift 2
    elif [ "$1" == "-libdir" ] ; then
	if [ "$2" == "" ] ; then
	    echo "*** -libdir option requires system library path argument."
	    echo
	    showhelp
	    exit 1
	fi
	api_options="$api_options $1 $2"
	shift 2    
    elif [ "$1" == "-sysroot" ] ; then
	if [ "$2" == "" ] ; then
	    echo "*** -sysroot option requires system root path argument."
	    echo
	    showhelp
	    exit 1
	fi
	driver_options="$driver_options $1 $2"
	api_options="$api_options $1 $2"
	shift 2
    else
        echo "*** Unknown option: $1"
        exit 1
    fi
done

(cd ./driver/monolithic/linux && ./configure $driver_options) &&
(cd ./api/modules/admxrc2/linux && ./configure $api_options)

exit $?
