# -*- makefile -*-
#
# Copyright (C) 2009-2014, Joachim Wiedorn <ad_debian@joonet.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.


# COMMON

LIBDIR		:= /tmp/fritz
OBJECTS		:= main.o driver.o tools.o tables.o queue.o lib.o
CARDLIB		:= ../lib/$(shell getconf LONG_BIT)_$(CARD)-lib.o
TARGDIR		:= $(DESTDIR)/lib/modules/$(shell uname -r)/misc

FRMDIR  	:= $(DESTDIR)/usr/share/isdn
CARDFRM2	:= fus2base.frm
CARDFRM3	:= fus3base.frm
FRMFILES	:= ../frm/$(CARDFRM2) ../frm/$(CARDFRM3)

FILES		:= $(CARD).ko
FILESO		:= $(CARD).o
CARDS		:= $(CARD)

ifeq ($(CARD),fcpcmcia)
FILES		+= $(CARD)_cs.ko
FILESO		+= $(CARD)_cs.o
CARDS		+= $(CARD)_cs
endif


ifneq ($(KERNELRELEASE),)


# KERNEL

# needed while running inside kernel sources
EXTRA_CFLAGS	+= -D__$(CARD)__ -DTARGET=\"$(CARD)\"
ifndef DEBUG
EXTRA_CFLAGS	+= -DNDEBUG
endif
EXTRA_LDFLAGS	+= $(LIBDIR)/$(CARD)-lib.o

obj-m       	:= $(FILESO)
$(CARD)-objs	:= $(OBJECTS)


else


# ARCHIVE

SOURCES		:= $(patsubst %.o,%.c,$(OBJECTS))
HEADERS		:= defs.h driver.h lib.h libdefs.h libstub.h lock.h \
		   main.h queue.h tables.h tools.h

ifeq ($(KDIR),)
KDIR		:= /lib/modules/$(shell uname -r)/build
endif

all:		$(FILES)

$(CARD).ko:	$(LIBDIR) $(SOURCES) $(HEADERS)
		install -m 644 $(CARDLIB) $(LIBDIR)/$(CARD)-lib.o
		@echo "Go into kernel sources and compile module $(CARD):"
		$(MAKE) -C $(KDIR) SUBDIRS=$(shell pwd) modules

$(CARD)_cs.ko:	$(CARD)_cs.c $(CARD).ko

clean:
		$(RM) $(OBJECTS)
		$(RM) $(FILESO)
		$(RM) $(FILES)
		$(RM) .*.cmd  .*.o.d
		$(RM) $(CARD)*.mod.*
		$(RM) Module.symvers
		$(RM) modules.order
		$(RM) -r .tmp_versions
		$(RM) -r $(LIBDIR)

install:	$(TARGDIR)  $(FRMDIR)
		install -m 644 $(FRMFILES) $(FRMDIR)
		install -m 644 $(FILES) $(TARGDIR)
		(cd $(TARGDIR); strip --strip-debug $(FILES))
		@if test `which depmod`; then depmod -a; fi

uninstall: 
		(cd $(FRMDIR); $(RM) $(CARDFRM2) $(CARDFRM3))
		(cd $(TARGDIR); $(RM) $(FILES))
		@if test `which depmod`; then depmod -a; fi

$(LIBDIR):	
		@mkdir -p $(LIBDIR)

$(TARGDIR):	
		@mkdir -p $(TARGDIR)

$(FRMDIR):	
		@mkdir -p $(FRMDIR)


endif

