#!/bin/sh
#set -x

#  Dazuko configure. Configure Dazuko for easy compilation.
#  Written by John Ogness <jogness@antivir.de>
#
#  Copyright (c) 2003 H+BEDV Datentechnik GmbH
#  All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions
#  are met:
#
#  1. Redistributions of source code must retain the above copyright notice,
#  this list of conditions and the following disclaimer.
#
#  2. Redistributions in binary form must reproduce the above copyright notice,
#  this list of conditions and the following disclaimer in the documentation
#  and/or other materials provided with the distribution.
#
#  3. Neither the name of Dazuko nor the names of its contributors may be used
#  to endorse or promote products derived from this software without specific
#  prior written permission.
#
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
#  POSSIBILITY OF SUCH DAMAGE.


print_help()
{
	echo "\`configure' configures dazuko to adapt to many kinds of systems."
	echo ""
	echo "Usage: ./configure [OPTION]..."
	echo ""
	echo "Configuration:"
	echo "  -h, --help   display this help and exit"
	echo ""
	echo "Fine tuning of the installation directories:"
	echo "  --kernelsrcdir=DIR   kernel source files (Linux only)"
	echo ""
	echo "For better control, use the options below."
	echo ""
	echo "Optional Features:"
	echo "  --disable-FEATURE               do not include FEATURE"
	echo "  --enable-FEATURE                include FEATURE"
	echo "  --disable-event-open            do not capture ON_OPEN events"
	echo "  --disable-event-close           do not capture ON_CLOSE events"
	echo "  --disable-event-exec            do not capture ON_EXEC events"
	echo "  --enable-event-close-modified   capture ON_CLOSE_MODIFIED events"
	echo "  --enable-devfs                  support devfs (Linux only)"
	echo "  --enable-debug                  print extra debug information"
	echo ""
	echo "Report bugs to <jogness@antivir.de>."
}

linux_check_srcdir()
{
	echo -n "kernel source in ${LINUX_SRC}... "

	if [ -f "${LINUX_SRC}/include/linux/version.h" ]
	then
		echo "yes"
		return 0
	else
		echo "no"
		return 1
	fi
}

do_linux()
{
	echo -n "checking if kernel is SMP... "

	if [ -z "$(uname -a | grep SMP)" ]
	then
		SMP=0
		echo "no"
	else
		SMP=1
		echo "yes"
	fi

	echo -n "checking whether sys_call_table is exported... "

	if [ -f "/proc/ksyms" ]
	then
		if [ -z "$(cat /proc/ksyms | grep sys_call_table)" ]
		then
			HIDDEN_SCT=1
			echo "no"
		else
			HIDDEN_SCT=0
			echo "yes"
		fi
	else
		HIDDEN_SCT=0
		echo "unknown (assuming yes)"
	fi

	if [ ${HIDDEN_SCT} -eq 1 ]
	then
		echo -n "checking whether sys_close is exported... "

		if [ -z "$(cat /proc/ksyms | grep sys_close)" ]
		then
			echo "no"
			echo "error: no usable exported symbols found"
			exit 1
		else
			echo "yes"
		fi
	fi

	if [ -z "${LINUX_SRC}" ]
	then
		LINUX_SRC="/lib/modules/$(uname -r)/build"
		linux_check_srcdir
		if [ $? -ne 0 ]
		then
			LINUX_SRC="/usr/src/linux"
			linux_check_srcdir

			if [ $? -ne 0 ]
			then
				echo "error: kernel source files not found"
				exit 1
			fi
		fi
	fi

	echo "CC =		gcc" > Makefile
	echo "CFLAGS +=	-Wall -O -DLINUX -Dlinux" >> Makefile
	echo "KFLAGS +=	\$(CFLAGS) -D__KERNEL__ -DMODULE -I${LINUX_SRC}/include" >> Makefile

	if [ ${SMP} -eq 1 ]
	then
		echo "KFLAGS +=	-D__module__smp -D__SMP__" >> Makefile
	fi

	if [ ${DEBUG} -eq 1 ]
	then
		echo "KFLAGS +=	-DDEBUG" >> Makefile
	fi

	if [ ${DEVFS} -eq 1 ]
	then
		echo "KFLAGS +=	-DCONFIG_DEVFS_FS" >> Makefile
	fi

	if [ ${HIDDEN_SCT} -eq 1 ]
	then
		echo "KFLAGS +=	-DHIDDEN_SCT" >> Makefile
	fi

	if [ ${ON_OPEN} -eq 1 ]
	then
		echo "KFLAGS +=	-DON_OPEN_SUPPORT" >> Makefile
	fi

	if [ ${ON_CLOSE} -eq 1 ]
	then
		echo "KFLAGS +=	-DON_CLOSE_SUPPORT" >> Makefile
	fi

	if [ ${ON_EXEC} -eq 1 ]
	then
		echo "KFLAGS +=	-DON_EXEC_SUPPORT" >> Makefile
	fi

	if [ ${ON_CLOSE_MODIFIED} -eq 1 ]
	then
		echo "KFLAGS +=	-DON_CLOSE_MODIFIED_SUPPORT" >> Makefile
	fi

	echo "" >> Makefile
	echo "all: example example_mt dazuko.o" >> Makefile
	echo "" >> Makefile

	echo "example: example.c" >> Makefile
	echo "	\$(CC) \$(CFLAGS) dazukoio.c example.c -o example" >> Makefile
	echo "" >> Makefile

	echo "example_mt: example_mt.c" >> Makefile
	echo "	\$(CC) \$(CFLAGS) -pthread dazukoio.c example_mt.c -o example_mt" >> Makefile
	echo "" >> Makefile

	echo "dazuko.o: dazuko.c dazuko.h" >> Makefile
	echo "	\$(CC) \$(KFLAGS) -c dazuko.c" >> Makefile
	echo "" >> Makefile

	echo "test: dazuko.o" >> Makefile
	echo "	/sbin/insmod ./dazuko.o" >> Makefile
	echo "	/sbin/rmmod dazuko" >> Makefile
	echo "" >> Makefile

	echo "clean:" >> Makefile
	echo "	rm -f dazuko.o example example_mt" >> Makefile
}

do_summary()
{
	echo ""
	echo " ------- Configuration -------"
	echo ""
	echo -n "events = "
	if [ ${ON_OPEN} -eq 1 ]
	then
		echo -n "ON_OPEN "
	fi

	if [ ${ON_CLOSE} -eq 1 ]
	then
		echo -n "ON_CLOSE "
	fi

	if [ ${ON_EXEC} -eq 1 ]
	then
		echo -n "ON_EXEC "
	fi

	if [ ${ON_CLOSE_MODIFIED} -eq 1 ]
	then
		echo -n "ON_CLOSE_MODIFIED "
	fi

	echo ""

	echo -n "devfs =  "
	if [ ${DEVFS} -eq 1 ]
	then
		echo "yes"
	else
		echo "no"
	fi

	echo -n "debug =  "

	if [ ${DEBUG} -eq 1 ]
	then
		echo "yes"
	else
		echo "no"
	fi

	echo ""

}

#main()
ON_OPEN=1
ON_CLOSE=1
ON_EXEC=1
ON_CLOSE_MODIFIED=0
DEVFS=0
DEBUG=0
SMP=0
HIDDEN_SCT=0
OS=Linux
LINUX_SRC=""

for option in $*
do
	case ${option} in
		--disable-event-open)
			ON_OPEN=0
			;;
		--enable-event-open)
			ON_OPEN=1
			;;
		--disable-event-close)
			ON_CLOSE=0
			;;
		--enable-event-close)
			ON_CLOSE=1
			;;
		--disable-event-exec)
			ON_EXEC=0
			;;
		--enable-event-exec)
			ON_EXEC=1
			;;
		--disable-event-close-modified)
			ON_CLOSE_MODIFIED=0
			;;
		--enable-event-close-modified)
			ON_CLOSE_MODIFIED=1
			;;
		--disable-devfs)
			DEVFS=0
			;;
		--enable-devfs)
			DEVFS=1
			;;
		--disable-debug)
			DEBUG=0
			;;
		--enable-debug)
			DEBUG=1
			;;
		--kernelsrcdir=*)
			LINUX_SRC="${option#--kernelsrcdir=}"
			;;
		--help|-h)
			print_help
			exit 0
			;;
		*)
			echo "configure: error: unrecognized option: ${option}"
			echo "Try \`./configure --help' for more information."
			exit 1
			;;
	esac
done

echo -n "checking host system type... "

if [ -z "$(uname -a | grep -i linux)" ]
then
	OS=unknown
else
	OS=Linux
fi

echo "${OS}"

case ${OS} in
	Linux)
		do_linux
		;;
	*)
		echo "error: unknown host system type"
		exit 1
		;;
esac

do_summary

