#!/bin/sh

# Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in 
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

rm -rf build
_KVER=$1
_SRC=$2
TMPDIR1=build
MAKEFILE="${TMPDIR1}/Makefile"
TEST_H="${TMPDIR1}/test.h"
TEST_C="${TMPDIR1}/test.c"

mkdir -p $TMPDIR1

config_host_h="${_SRC}/config-host.h"

rm -rf $config_host_h

remove_test() {
   make clean >> config.log 2>&1
   cd ..
   truncate -s 0 $TEST_C
}

output_sym() {
  echo "#define $1 1" >> $config_host_h
}

fatal() {
  echo $@
  echo "Configure failed, check config.log and/or the above output"
  rm -rf $config_host_h
  exit 1
}

compile_prog() {
   cd ${TMPDIR1}
   echo $1 '\n' >> config.log
   if [ $# -gt 1 ]; then
	kflags=$2
	make $kflags >> config.log 2>&1
   else
        make >> config.log 2>&1
   fi
   ret=$?
   if [ "$ret" -eq 0 ]
   then
        echo '\n' $1 "yes" >> config.log
	echo $1 "yes"
	echo '\n' "Invoking make clean" >> config.log
        remove_test
        return 0	
   else
        echo '\n' $1 "no"  >> config.log
        echo $1 "no"
	echo '\n' "Invoking make clean" >> config.log
        remove_test
	return 1
   fi
}

echo "/*" > $config_host_h
echo " * Automatically generated by configure - do not modify" >> $config_host_h
printf " * Configured with:" >> $config_host_h
printf " * '%s'" "$0" "$@" >> $config_host_h
echo "" >> $config_host_h
echo " */" >> $config_host_h

echo "#ifndef CONFIG_HOST_H" >> $config_host_h
echo "#define CONFIG_HOST_H" >> $config_host_h

#Check if KVER is already passed, if not assign the current kernel version
if [ -z "$_KVER" ]
then
	export KVER=$(uname -r)
else
	export KVER=${_KVER}
fi
export MODULES_DIR=/lib/modules/$KVER
export KDIR=$MODULES_DIR/build

cat > $MAKEFILE <<EOF
obj-m += test.o
all:
	make -j8 -C $KDIR M=$PWD/build modules
clean:
	make -j8 -C $KDIR M=$PWD/build clean
EOF

cat > $TEST_H <<EOF
#include <linux/module.h>
#include <linux/kernel.h>

MODULE_LICENSE("GPL v2");
EOF

cat > $TEST_C <<EOF

#include "test.h"
#include <linux/device.h>
#include <linux/export.h>
int test (void)
{
    struct class* nvfs_class;
    nvfs_class = class_create(THIS_MODULE, "testclass");
    return 0;
}
EOF
if compile_prog "Checking if class_create has two parameters or not ..."; then
        output_sym "CLASS_CREATE_HAS_TWO_PARAMS"
fi

cat > $TEST_C <<EOF
#include "test.h"
#include <linux/mm_types.h>
int test (void)
{
    struct vm_area_struct vma;
    vma.vm_flags = 0;
    return 0;
}
EOF
if compile_prog "Checking if vma_flags are modifiable directly ..."; then
        output_sym "NVFS_VM_FLAGS_NOT_CONSTANT"
fi

echo "#endif" >> $config_host_h
echo "Configuration complete"
rm -rf build