#
# Makefile (ceddrv)
#
# Copyright 1995   rubini@ipvvis.unipv.it
#
####
#
# The main configuration parameters are in cedCfg.h

# To compile with "-g -Wall -DDEBUG" and without stripping
# uncomment the following line.
#
# QQQ: Alessandro: May you change this to 3 types of DEBUG:
# AAA: I've done two of them. But a single PDEBUG(()) can be disabled
#	by turning it into PDEBUGG(()).
#
#DEBUG_M = t    # debug module
DEBUG_P = t	   # debug programs

INSTALL = install -c
INSTALLBIN = $(INSTALL) -m 755
INSTALLLIB = $(INSTALL) -m 644

ifndef PREFIX
  PREFIX = /usr/local
endif

MODDIR = /lib/modules/$(shell uname -r)
LIBDIR = $(PREFIX)/lib
BINDIR = $(PREFIX)/bin
INCDIR = $(PREFIX)/include
MANDIR = $(PREFIX)/man/man4
INFODR = $(PREFIX)/info

############## no more editing...

# unix clients
CLIENTOBJS = checkced.o
CLIENTS = $(CLIENTOBJS:.o=)

# dossish clients (needing the compatibility library)
DLIENTOBJS = cload.o samplechan.o
DLIENTS = $(DLIENTOBJS:.o=)

# extra clients (compile, but not install)
XLIENTOBJS = seltest.o simple.o losscheck.o
XLIENTS = $(XLIENTOBJS:.o=)


CFLAGS = -O3 -fomit-frame-pointer
LDFLAGS = -L. -l$(TARGET) -s

#
# If debugging is asked for, enable it.
#
ifdef DEBUG_P
  # -O is needed, because of "extern inline"
  CFLAGS = -g -O -DDEBUG1401P -Wall
  LDFLAGS = -L. -l$(TARGET)
  ifdef DEBUG_M
    CFLAGS += -DDEBUG1401M
  endif
else
  ifdef DEBUG_M
    CFLAGS = -g -O -DDEBUG1401M -Wall
  endif
endif

ifndef CED1401_LIBRARY
  CED1401_LIBRARY = $(PREFIX)/lib/ced1401
endif

CFLAGS += -DCED1401_LIBRARY="\"$(CED1401_LIBRARY)\""

TARGET = ceddrv

LIB = lib$(TARGET).a
   LOBJS = lib.o
LIBDOS = libceddos.a
   LDOSOBJS = load.o libdos.o 
INCLUDES = $(TARGET).h

INFO = doc/$(TARGET).info
MAN = $(TARGET).4

ifeq ($(RANLIB),)
   RANLIB = ranlib
endif

# if not Linux, print a message and die
ifneq ($(shell uname),Linux)
beforeall: check
endif

# force make depend
ifneq (.depend,$(wildcard .depend))
all: depend
endif

# lacks documentation support
all: $(TARGET).o $(LIB) $(LIBDOS) $(CLIENTOBJS) $(CLIENTS) \
                 $(DLIENTOBJS) $(DLIENTS) $(XLIENTOBJS) $(XLIENTS) \
	             $(INFO) $(MAN) checkmmap

check:
	@if [ `uname` != Linux ]; then \
	  echo "" ;\
	  echo "**** This package can compile only under Linux."; \
	  echo "**** Please read the docs (after \"make -C doc\") "; \
	  echo "" ;\
	  exit 1; fi

checkmmap:
	@if [ -z "`grep ' _mem_map$$' /proc/ksyms`" ]; then \
		echo "*** WARNING: \"mem_map\" is not a public symbol of the kernel"; \
	    echo "*** The module can't run with this kernel. (See \"./README\")." \
		exit 1; fi

checkversion:
	@echo ""; echo "    === Version checkup ==="; echo ""
	@grep -n _checkit_ *.h doc/doc.$(TARGET); echo ""
	

$(TARGET).o: 
	$(CC) -c $(CFLAGS) $*.c -o $*.o

$(LIB): $(LOBJS)
	$(AR) r $@ $^
	$(RANLIB) $@

$(LIBDOS):  $(LDOSOBJS)
	$(AR) r $@ $^
	$(RANLIB) $@

# Without this silly rule it won't work :-(
# $(LD) won't work, either.
%: %.o
	$(CC) $*.o -o $@ $(LDFLAGS)

$(CLIENTS): $(LIB)

$(DLIENTS): $(LIB) $(DOSLIB)

# temporary hack, before I get a wiser way....
cload: 
	$(CC) $(CFLAGS) -o $@ $@.o $(LDFLAGS) -lceddos

samplechan:
	$(CC) $(CFLAGS) -o $@ $@.o $(LDFLAGS) -lceddos


$(INFO): doc/doc.$(TARGET)
	-@$(MAKE) -C doc info || \
		(echo "Warning: failed to make the info file"; exit 1)

$(MAN): doc/doc.$(TARGET) doc/manpager
	doc/manpager doc/doc.$(TARGET)

	
############## misc targets

.PHONY: install clean cleanest check distrib terse tar bin\
             mpage sample sample-dist depend dep DEPEND DEP

install: all
	@for i in $(MODDIR) $(BINDIR) $(LIBDIR) $(INCDIR) $(MANDIR); do\
		if [ -d $$i ]; then set +x; else (set -x; mkdir -p $$i); fi; \
	done
	$(INSTALLBIN) $(TARGET).o   $(MODDIR)
	$(INSTALLBIN) $(CLIENTS)    $(BINDIR) 
	$(INSTALLBIN) $(DLIENTS)    $(BINDIR) 
	$(INSTALLLIB) $(LIB)        $(LIBDIR)
	$(INSTALLLIB) $(INCLUDES)   $(INCDIR)
	-$(INSTALLLIB) $(INFO)      $(INFODR)
	$(INSTALLLIB) $(MAN)        $(MANDIR)

terse:
	rm -f *~ */*~ */*.dvi */*.log

clean: terse
	rm -f .depend TAGS Test.log
	$(MAKE) -C doc clean
	rm -f $(TARGET).o $(LIB) $(LIBDOS) $(CLIENTS) $(DLIENTS) $(XLIENTS) *.o
	rm -f $(MAN)

cleanest: clean
	rm $(MAN) $(INFO)
	rm -r bin

tar: 
	n=`basename \`pwd\``; cd ..; tar cvf - $$n | gzip > $$n.tar.gz


mpage:
	$(MAKE) -C doc mpage

depend dep:
	gcc -MM *.c > .depend

DEPEND DEP:
	gcc -M *.c > .depend


distrib: clean tar checkversion

ifeq (.depend,$(wildcard .depend))
include .depend
endif


