#       @(#) Makefile 1.00 03/09/13

PREFIX ?= /usr

KERN_VER = $(shell uname -r)
# lets try to get the kernel stuff from /lib/modules and if that
# fails, lets fall back to /usr/src/linux
ifeq (X$(KERN_DIR),X)
	KERN_DIR = /lib/modules/$(KERN_VER)/build
	ifeq ($(shell test -e $(KERN_DIR)/Makefile || echo yes),yes)
		KERN_DIR = /usr/src/linux
	endif
endif
KERN_INC ?= $(KERN_DIR)/include
KERN_MAJ = $(shell echo $(KERN_VER) | cut -b1-3 -)

# Here we detect python and if we find it, try to figure
# out where to install the python library files
ifeq (X$(PYTHON),X)
	PYTHON = $(shell which python)
	ifeq ($(shell test -x $(PYTHON) && echo yes),yes)
		PYTHON_PRE = $(shell $(PYTHON) -c "import sys; print sys.prefix")
		PYTHON_VER = $(shell $(PYTHON) -c "import sys; print sys.version[0:3]")
		PYTHON_V1 = $(shell $(PYTHON) -c "import sys; print sys.version[0:1]")
		PYTHON_V2 = $(shell $(PYTHON) -c "import sys; print sys.version[2:3]")
	else
		PYTHON_V1 = 1
		PYTHON_V2 = 1
	endif
endif
PYTHON_DIR ?= $(PYTHON_PRE)/lib/python$(PYTHON_VER)/site-packages

OBJ = cdemu
ifeq ($(KERN_MAJ),2.4)
	OBJS = $(OBJ).o
else
	OBJS = $(OBJ).ko
endif

ifeq ($(BUILD_GHETTO),yes)
CC ?= gcc
CFLAGS ?= -O2 -Wall
CFLAGS += -I$(KERN_INC) -D__KERNEL__ -DMODULE -DKBUILD_MODNAME=cdemu
all:
	$(CC) $(CFLAGS) -c cdemu.c -o cdemu.o
	$(KERN_DIR)/scripts/modpost $(KERN_DIR)/vmlinux cdemu.o
	$(CC) $(CFLAGS) -c cdemu.mod.c -o cdemu.mod.o
	ld -r -o cdemu.ko cdemu.o cdemu.mod.o

else
ifeq ($(shell test -s $(KERN_DIR)/Makefile && echo yes),yes)
# kernel source found
obj-m += cdemu.o
all:
	make -C $(KERN_DIR) SUBDIRS=$(PWD) modules

modules: $(OBJS)
else

# kernel source _not_ found
CC ?= gcc
CFLAGS ?= -O2 -Wall
CFLAGS += -I$(KERN_INC) -D__KERNEL__ -DMODULE 
ifeq ($(KERN_MAJ),2.6)
	CFLAGS += -DKBUILD_MODNAME=cdemu
endif

all: modules

modules: $(OBJS)
	$(CC) $(CFLAGS) -c $(OBJ).c -o $(OBJS)

endif
endif

clean:
	rm -f .$(OBJ).{ko,mod.o,o}.cmd $(OBJ).{ko,o,mod.{c,o}} *.pyc
	rm -r .tmp_versions

# all check python-check
install:
	@if [ "`whoami`" != "root" ] ; then \
		echo "****" ; \
		echo "**** WARNING -- You might not be root." ; \
		echo "**** If the below install fails, try it as root!" ; \
		echo "****" ; \
		echo "" ; \
	fi
	@echo "**** Installing files ****"
	install -D -m 644 $(OBJS)	$(DESTDIR)/lib/modules/$(KERN_VER)/misc/$(OBJS)
	install -D -m 644 libcdemu.py	$(DESTDIR)$(PYTHON_DIR)/libcdemu.py
	install -D -m 755 cdemu		$(DESTDIR)$(PREFIX)/bin/cdemu
	install -D -m 755 create_cdemu_devs.sh	$(DESTDIR)$(PREFIX)/bin/create_cdemu_devs.sh
	-sh create_cdemu_devs.sh
	@if [ "${DESTDIR}" = "" ] ; then \
		/sbin/depmod -a; \
	fi

check: all
	@modprobe -r $(OBJ)
	@dmesg -c 2>&1 >/dev/null
	@echo -e "\n**** Loading and removing module ****"
	insmod ./$(OBJS)
	dmesg -c
	rmmod $(OBJ)

python-check:
	@if [ -z "$(PYTHON)" ]; then \
		echo; \
		echo "Error: Could not find your python installation."; \
		echo "Maybe this will help: make PYTHON=/path/to/python.bin install"; \
		echo; \
		exit 1; \
	fi
	@if [ $(PYTHON_V1) -lt 2 -o $(PYTHON_V2) -lt 2 ]; then \
		echo; \
		echo "Error: You need at least python-2.2.0 to use cdemu."; \
		echo "Aborting..."; \
		echo; \
		exit 1; \
	fi
