# Makefile for cdemu ( http://cdemu.sf.net/ )
# $Header$

PREFIX = /usr
SRCS = cdemu_core.c cdemu_mod.c
OBJS = $(patsubst %.c,%.o, $(SRCS))

#
# Kernel source detection
#  - default to /lib/modules/$(uname -r)/build/
#  - fall back to /usr/src/linux/
#
KERN_VER = $(shell uname -r)
KERN_MAJ = $(shell echo $(KERN_VER) | cut -d. -f1-2 -)
ifndef KERN_DIR
	KERN_DIR = /lib/modules/$(KERN_VER)/build
	ifeq ($(shell test -e $(KERN_DIR)/Makefile || echo yes),yes)
		KERN_DIR = /usr/src/linux
	endif
endif
ifndef KERN_SRC
	ifeq ($(shell test -s $(KERN_DIR)/Makefile && echo yes),yes)
		KERN_SRC = yes
	else
		KERN_SRC = no
	endif
endif
KERN_INC = $(KERN_DIR)/include

ifeq ($(KERN_SRC),no)
    $(error You'll need sources for your (at least 2.6) kernel)
endif


#
# Python detection routine
#  - try to find .py install path
#
ifndef PYTHON
	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


#
# Break kernel-specific cruft into sub .mk files in order
# to keep this stuff readable
#
ifndef MK_INC
	MK_INC = $(shell pwd)
endif
ifeq ($(KERN_MAJ),2.4)
        $(error You'll need at least 2.6 kernel)
endif
ifeq ($(KERN_MAJ),2.6)
	KERN_MICRO = $(shell echo $(KERN_VER) | sed 's/2\.6\.\([0123456789]*\).*/\1/')
	ifeq ($(shell test "$(KERN_MICRO)" -lt "16" >& /dev/null && echo yes),yes)
                $(warning Kernel 2.6.16 is the minimum recommended kernel version)
	endif
	MODVAR = M
	include $(MK_INC)/mk/linux-2.6
endif


#
# Shared make targets
#
.PHONY: mrproper clean docs extra-docs install install-docs check python-check vapier-test

mrproper: clean
	rm -f docs/cdemu.{info,txt,html}

clean:
	rm -f .cdemu*.{ko,mod.o,o}.cmd cdemu*.{ko,o,mod.{c,o}} *.pyc core *~
	rm -rf .tmp_versions Modules.symvers
	rm -f docs/cdemu.{dvi,pdf}

docs:
	cd docs; makeinfo cdemu.texi
	cd docs; makeinfo --plaintext -o cdemu.txt cdemu.texi
	cd docs; makeinfo --no-split --html cdemu.texi

extra-docs:
	cd docs; texi2dvi -c -q cdemu.texi
	cd docs; texi2pdf -c -q cdemu.texi

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 $(KOBJ) $(DESTDIR)/lib/modules/$(KERN_VER)/misc/$(KOBJ)
	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
	@if [ "$(DESTDIR)" = "" ] ; then \
		/sbin/depmod -a ; \
	else \
		echo "**** Skipping depmod" ; \
	fi
	-sh create_cdemu_devs.sh

uninstall:
	-rmmod cdemu
	-rm -f /lib/modules/$(KERN_VER)/misc/$(KOBJ)
	-rm -f $(PYTHON_DIR)/libcdemu.py*
	-rm -f $(PREFIX)/bin/cdemu
	-rm -f $(PREFIX)/bin/create_cdemu_devs.sh

install-docs:
	@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
	gzip docs/cdemu.1
	install -D -m 644 docs/cdemu.1.gz $(DESTDIR)$(PREFIX)/share/man/man1/cdemu.1.gz
	gunzip docs/cdemu.1
	gzip docs/cdemu.info
	install -D -m 644 docs/cdemu.info.gz $(DESTDIR)$(PREFIX)/share/info/cdemu.info.gz
	gunzip docs/cdemu.info

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

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

vapier-test:
	@for v in 2.4.2{1,2,3,6,7} 2.6.{5,6,7,8.1,9,10,11,999} ; do \
		echo -e "\n\n------------- $$v -------------\n\n" ; \
		echo $(MAKE) KERN_DIR=/usr/src/includes/$$v KERN_VER=$$v clean all ; \
		$(MAKE) \
			KERN_SRC=no \
			KERN_INC=/usr/src/includes/linux-$$v/include \
			KERN_VER=$$v \
			clean all 2>&1 | head -n 30 ; \
			_pipestatus="$${PIPESTATUS[*]}"; \
			[[ "$${_pipestatus// /}" -eq 0 ]] || exit 1 ; \
	done
	$(MAKE) clean
	@echo -e "\n\n------------- done -------------\n\n"
