# (C) Copyright International Business Machines  Corp., 2008
# Makefile for the Linux lin_tape driver

ifneq ($(KERNELRELEASE),)
#include $(PWD)/Kbuild

else
VERSIONRELEASE := $(shell sed --regexp-extended -e '{/\#define LIN_TAPE_VERSION/ s/.*"([0-9.]*)-?([A-Za-z0-9.]*)"/\1 \2/p;d}' lin_tape_tape.h)
endif

obj-m := lin_tape.o

lin_tape-objs := lin_tape_scsi_config.o lin_tape_scsi_tape.o lin_tape_scsi_trace.o \
             lin_tape_ioctl_tape.o lin_tape_ioctl_changer.o lin_tape_extra_ioctl.o

PROC := $(shell uname -m)

ifeq ($(PROC), ppc64)
EXTRA_CFLAGS += -m64
endif

ifeq ($(PROC), powerpc64)
EXTRA_CFLAGS += -m64
endif

ifeq ($(PROC), powerpc)
EXTRA_CFLAGS += -m64 -msoft-float
LDFLAGS += -melf64ppc
endif

ifeq ($(PROC), x86_64)
EXTRA_CFLAGS += -mcmodel=kernel
endif

VERSION := $(word 1,$(VERSIONRELEASE))
RELEASE := $(word 2,$(VERSIONRELEASE))
ifeq ($(RELEASE),)
RELEASE := 1
endif

KERNEL	:= $(shell uname -r)
SRCDIR	:= /lib/modules/$(KERNEL)/build
PWD	:= $(shell pwd)

default modules lin_tape.ko:
	export PWD
	$(MAKE) -C $(SRCDIR) SUBDIRS=$(PWD) PWD=$(PWD) modules

modules_install:
	export PWD
	$(MAKE) -C $(SRCDIR) SUBDIRS=$(PWD) PWD=$(PWD) modules_install

clean: 
	$(MAKE) -C $(SRCDIR) SUBDIRS=$(PWD) PWD=$(PWD) clean
	@rm -rf *.tgz *.tar* *.ko bldtmp .l* .t* .o *.mod.c *odule* IBMtapeutil IBMtapeutil*/ lin_taped *~

compileclean:
	@rm -f *.o

bldtmp:
	mkdir bldtmp
#
# create the IBMtapeutil package
#
UVERSION:=$(shell  [ -e "IBMtapeutil.c" ] &&  grep 'const char \*version = "IBMtapeutil for Linux, Version' IBMtapeutil.c|cut -f 2 -d ','|cut -f 3 -d ' ')
UTILDIR=IBMtapeutil.$(UVERSION)
UTILPKG=$(UTILDIR).$(PROC).tar
UTILSRCS=IBMtapeutil.h IBMtapeutil.c IBM_tape.h
IBMtapeutil: $(UTILSRCS)
	make -f makefile.tapeutil
util $(UTILPKG): IBMtapeutil
	mkdir $(UTILDIR);
	cp -a IBMtapeutil ./$(UTILDIR)/.
	cp -a IBMtapeutil.h ./$(UTILDIR)/.
	cp -a IBMtapeutil.c ./$(UTILDIR)/.
	cp -a makefile.tapeutil ./$(UTILDIR)/makefile
	cp -a IBMtapeutil.ReadMe ./$(UTILDIR)/.
	cp -a IBM_tape.h ./$(UTILDIR)/.
	cp -a IBMtapeconfig ./$(UTILDIR)/.
	tar -cvf $(UTILPKG) $(UTILDIR)
	@rm -rf $(UTILDIR)
#
BASE := $(shell echo $(KERNEL)|sed --regexp-extended -e 's/[^a-zA-Z0-9]*(smp|default|bigsmp|hugemem)*$$//p;d')
KERNELS := $(shell echo $(notdir $(subst /build,,$(shell find $(wildcard /lib/modules/$(BASE)*) -name build)))|sed -e '/$(BASE)[^a-zA-Z0-9]{,1}(smp|default|bigsmp|hugemem)/p')
$(KERNELS:%=bldtmp/lin_tape-%.ko): clean bldtmp
	$(MAKE) KERNEL=$(patsubst bldtmp/lin_tape-%.ko,%,$@) compileclean lin_tape.ko
	mv lin_tape.ko $@

driver: $(KERNELS:%=bldtmp/lin_tape-%.ko) 

alldevs: $(KERNELS:%=bldtmp/lin_tape-%.ko) lin_taped

cleandev: 
	@rm -f $(DEVOBJS)

tarfiles = $(wildcard *.c *.h *.ReadMe Makefile* *.conf COPYING* *.spec \
	   *.fixlist messages lin_tape ibmekm/)

lin_taped: lin_taped.c
	$(CC) $(EXTRA_CFLAGS) lin_taped.c -I ibmekm/inc  -D LINUX ./ibmekm/src/proxy_mgr.c ./ibmekm/src/tcpip_mgr.c -o $@ ;
	strip $@ ;

# Update the version number in the spec file
lin_tape-$(VERSION)/lin_tape.spec: lin_tape.spec lin_tape_tape.h lin_tape-$(VERSION) Makefile
	@sed <lin_tape.spec --regexp-extended -e 's/^(Version: ).*/\1$(VERSION)/;s/^(Release: ).*/\1$(RELEASE)/' >lin_tape-$(VERSION)/lin_tape.spec

lin_tape-$(VERSION):
	@mkdir lin_tape-$(VERSION)

tar lin_tape-$(VERSION).tgz: $(tarfiles) lin_tape-$(VERSION) lin_tape-$(VERSION)/lin_tape.spec
	@cp -a $(tarfiles) lin_tape-$(VERSION)
	@tar czf lin_tape-$(VERSION).tgz lin_tape-$(VERSION)
	@rm -rf lin_tape-$(VERSION)
	@ls -l lin_tape-$(VERSION).tgz

lin_taped-$(VERSION):
	@mkdir lin_taped-$(VERSION)

lin_taped-$(VERSION)/lin_taped: lin_taped-$(VERSION) lin_taped
	@cp -a $(tarfiles) lin_taped-$(VERSION)
#	@cp -a lin_taped $@
	@rm -f lin_taped-$(VERSION)/lin_tape.spec
	@tar czf lin_taped-$(VERSION).tgz lin_taped-$(VERSION)
	@rm -rf lin_taped-$(VERSION)
	@ls -l lin_taped-$(VERSION).tgz

daemonrpm: rpmdaemon
rpmdaemon: lin_taped-$(VERSION)/lin_taped
	rpmbuild -ta lin_taped-$(VERSION).tgz

rpm: lin_tape-$(VERSION).tgz rpmdaemon
	rpmbuild -tb lin_tape-$(VERSION).tgz
#	rpmbuild -ta lin_taped-$(VERSION).tgz

srctar: $(tarfiles) lin_tape-$(VERSION) lin_tape-$(VERSION)/lin_tape.spec
#	@mkdir lin_tape-$(VERSION)
	@cp -a $(tarfiles) lin_tape-$(VERSION)
	@rm -rf lin_tape-$(VERSION)/ibmekm/{ibmekm.fixlist,cfg,src,inc/proxy*,inc/ibmekm*}
	@rm -rf lin_tape-$(VERSION)/lin_taped* 
	@rm -rf lin_tape-$(VERSION)/{*~,*.in,*.ac,*util*} 
	@tar czf lin_tape-$(VERSION).tgz lin_tape-$(VERSION)
	@rm -rf lin_tape-$(VERSION)
	@ls -l lin_tape-$(VERSION).tgz

rpmsrc: srcrpm
srcrpm: srctar
	rpmbuild -ts lin_tape-$(VERSION).tgz

srcrpm_supported: srctar
	cp mdl.supported Module.supported
	cp lin_tape.spec.supported lin_tape.spec
	cp lin_taped.spec.supported lin_taped.spec
	rpmbuild -ts lin_tape-$(VERSION).tgz

allrpm: rpmall
rpmall: rpm srcrpm

maintclean: mrproper
	rm -fr config.log configure config.status autom4te.cache
#	rm -f lin_tape.spec

install:
	insmod ./lin_tape.ko

remove: 
	rmmod lin_tape

update:
	chmod 755 lin_tape.ko

progs: IBMtapeutil lin_taped

.PHONY: install remove update progs all clean cleandev alldevs tar rpm 
