# Makefile,v 1.26 1995/11/06 01:07:52 heiko Exp
# VERSION: 1.3
# ex:set ts=4 sw=4 noexpandtab:

VERSION := 1.3

# ############################################################################
# Change if neccessary ....
#

# Where your kernel source tree is located
# kernel_path := /usr/src/linux-1.2

# `make install' will install the module to 
#  $(prefix)/$(KERNEL_VERSION)/$(moddir)
prefix := /lib/modules
moddir := misc

# DEBUG - full debug output (see linux/mcdx.h)
# QUIET - suppress informational messages
DEBUG := 0
QUIET := 1

#
# Nothing else to change
# ############################################################################

MAKEFILE := Makefile
SHELL    := /bin/bash

ifdef kernel_path
SRC := mcdx.c mcd.c \
	   linux/mcdx.h linux/mcd.h \
	   Makefile \
	   ChangeLog COPYING lsm README.mcd README.mcdx

# Get the version of the kernel source tree, _not_ of the
# running kernel
KERNEL_PATH := $(strip $(kernel_path))
KERNEL_VERSION := $(shell \
	v=$$(grep '^VERSION' $(KERNEL_PATH)/Makefile | awk '{print $$3;}');\
	p=$$(grep '^PATCHLEVEL' $(KERNEL_PATH)/Makefile | awk '{print $$3;}');\
	s=$$(grep '^SUBLEVEL' $(KERNEL_PATH)/Makefile | awk '{print $$3;}');\
	echo $$v.$$p.$$s)
#
# Compiler
#
CC      := gcc
CFLAGS  := -DUTS_RELEASE=\"$(KERNEL_VERSION)\" -m486 -O2 -Wall -Wno-comment -Wstrict-prototypes \
        -finline-functions \
        -fomit-frame-pointer \
		-I. -I$(KERNEL_PATH)/include -I$(KERNEL_PATH)/drivers/block 
LDFLAGS := -s -N 

# Note: The CONFIG_MODVERSIONS define is read from autoconf.h
DRIVER_FLAGS := -DMODULE -D__KERNEL__ \
		-DMCDX_DEBUG=$(strip $(DEBUG)) \
		-DMCDX_QUIET=$(strip $(QUIET))

compile.driver := $(CC) $(CFLAGS) $(DRIVER_FLAGS)

#
# Modutils
#
DEPMOD  := /sbin/depmod
INSMOD  := /sbin/insmod

DRIVER := mcdx.o mcd.o
KCONF := $(KERNEL_PATH)/.config


.PHONY: help all clean nuke realclean mrproper tags


help: 		
		@egrep '^([a-z ]+)?:[ 	]+## ' $(MAKEFILE) | expand -4

$(KCONF):
	@echo "First you should do a \`make config' in $(KERNEL_PATH)."
	@/bin/false

all:		## create the driver modules
all: 		$(KCONF) $(DRIVER) 

install:	## install the drivers (doesn't load them)
install:	all
	@DEST=$(prefix)/$(KERNEL_VERSION)/$(moddir);\
	echo Installing $(DRIVER) under $$DEST;\
	[ -d $$DEST ] || mkdir -p $$DEST;\
	cp -v $(DRIVER) $$DEST;\
	/sbin/depmod -a;\
	echo ':';\
	echo ': Make sure, that you have made the correct device';\
	echo ': entries in /dev (see README.mcdx for hints)';\
	echo ': And double check the irq and i/o port settings';\
	echo ': in linux/mcdx.h and linux/mcd.h';\
	echo ':'

uninstall:	## remove the installed drivers (doesn't unload them)
uninstall:	
	@DEST=$(prefix)/$(KERNEL_VERSION)/$(moddir);\
	echo Removing $(DRIVER) from $$DEST;\
	cd $$DEST; rm $(DRIVER);\
	/sbin/depmod -a

distrib:    $(SRC) 
	rm -rf mcdx-$(VERSION);\
	mkdir mcdx-$(VERSION);\
	mkdir mcdx-$(VERSION)/linux;\
	for f in $(SRC);\
		do \
		sed "/sed/! s/@VERSION@/$(VERSION)/; s/1.3.37/$(KERNEL_VERSION)/"\
	 	<$$f > mcdx-$(VERSION)/$$f;\
	done;\
	for f in mcdx-$(VERSION)/$(MAKEFILE) mcdx-$(VERSION)/linux/*.h;\
		do \
		cat /tmp/$$$$ > $$f;\
		rm /tmp/$$$$;\
	done
	mv mcdx-$(VERSION)/lsm mcdx-$(VERSION)/mcdx-$(VERSION).lsm

insert:	
	dir=$(KERNEL_PATH)/drivers/cdrom ;\
	doc=$(KERNEL_PATH)/Documentation/cdrom ;\
	predoc="" ;\
	test -d $$dir || dir=$(KERNEL_PATH)/drivers/block ;\
	test -d $$doc || doc=$$dir; predoc=README. ;\
	cp README.mcd $$doc/$${predoc}mcd ;\
	cp README.mcdx $$doc/$${predoc}mcdx ;\
	cp mcd.c mcdx.c $$dir ;\
	cp linux/mcd.h linux/mcdx.h $(KERNEL_PATH)/include/linux
	
tar.gz:	distrib
	tar czvf mcdx-$(VERSION).$@ mcdx-$(VERSION)

uue:	tar.gz
	uuencode mcdx-$(VERSION).tar.gz mcdx-$(VERSION).tar.gz \
		<mcdx-$(VERSION).tar.gz > mcdx-$(VERSION).tar.gz.uue
	
mcdx.o:	mcdx.c linux/mcdx.h $(MAKEFILE)
	$(compile.driver) -c $<

mcd.o:	mcd.c linux/mcd.h $(MAKEFILE)
	$(compile.driver) -c $<

tags:
	ctags -dtw *.c linux/*.h

clean:

nuke realclean mrproper:	clean
	rm -f *.o 
	rm -f $(DRIVER)
	rm -f .ver
	rm -f tags
	rm -fr mcdx-*

else
.PHONY:	all
all:
	@echo 'Please set the "kernel_path" variable in $(MAKEFILE) first'.
	@false
endif
	
