#
# Makefile -- makefile for the HP OmniBook support module
#
# 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, 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.
#
# Written by Sos Pter <sp@osb.hu>, 2002-2004
#

MODULE_NAME	= omnibook

ifeq ($(KERNELRELEASE),)

MODDIR	= /lib/modules
KVER	= $(shell uname -r)
VMODDIR = $(MODDIR)/$(KVER)
DESTDIR	= $(VMODDIR)/kernel/drivers/char/omnibook
#KSRC	= /usr/src/linux
KSRC	= $(VMODDIR)/build
KMODDIR	= $(KSRC)/drivers/char/omnibook
KINCDIR	= $(KSRC)/include/linux
KDOCDIR	= $(KSRC)/Documentation/omnibook
BDIR	= $(shell pwd)
TODAY	= $(shell date +%Y-%m-%d)
KERNEL	= $(shell uname -r | cut -c 1-3)

CC	= gcc
LD	= ld
DEPMOD	= depmod -a
#RMMOD	= rmmod
RMMOD	= modprobe -r
#INSMOD	= insmod
INSMOD	= modprobe
INSTALL	= install -m 644
MKDIR	= mkdir -p
RM	= rm -f
FIND	= find
endif

DEBUG	= # -DDEBUG_OMNIBOOK
# Used by 2.6 stuff, so let's be consistant.
EXTRA_CFLAGS += -I$(src) -D OMNIBOOK_STANDALONE $(DEBUG) 

ifeq ($(KERNELRELEASE),)
ifneq ($(KERNEL),2.6)
# These are now dealt with automagically
CFLAGS	= -O2 -Wall -D__KERNEL__ -DMODULE -I$(KSRC)/include $(EXTRA_CFLAGS)

CFLAGS 	+= $(shell [ -f $(KSRC)/include/linux/modversions.h ] && \
	 echo "-DMODVERSIONS -include $(KSRC)/include/linux/modversions.h")
CFLAGS	+= -DEXPORT_SYMTAB #-fno-strict-aliasing -fno-common
endif
endif

OBJS	= ac.o apmemu.o battery.o blank.o display.o dmi.o dock.o \
	  ec.o fan.o fan_policy.o init.o lcd.o onetouch.o \
	  temperature.o touchpad.o util.o
ifeq ($(KERNEL),2.4)
OBJS	+= polling.o

# Not needed for 2.6 
export-objs := init.o
endif

# All extra flags delt with automagically
obj-$(CONFIG_OMNIBOOK)	+= $(MODULE_NAME).o
omnibook-objs := $(OBJS)

ifneq ($(KERNEL),2.6)
all:		 $(MODULE_NAME).o
else
all:		 $(MODULE_NAME).ko
endif		

clean:
		$(RM) .*.cmd *.map *.mod.c *.o *.ko *~ "#*#"
		$(RM) -r .tmp_versions
		(cd misc/obtest; $(RM) obtest *.o)

install:	all
		# Removing module from old location
		$(RM) $(MODDIR)/misc/$(MODULE_NAME).*o
		$(MKDIR) $(DESTDIR)
ifeq ($(KERNEL),2.4)
		$(INSTALL) $(MODULE_NAME).o $(DESTDIR)
else
		$(INSTALL) $(MODULE_NAME).ko $(DESTDIR)
endif
		$(DEPMOD)

unload:
ifeq ($(KERNEL),2.4)
		$(RMMOD) $(MODULE_NAME) || :
endif

load:		install unload
		$(INSMOD) $(MODULE_NAME)

uninstall: unload
		$(FIND) $(VMODDIR) -name "$(MODULE_NAME).*o" -exec $(RM) {} \;
		$(DEPMOD)

uninstall-all: unload
		$(FIND) $(MODDIR) -name "$(MODULE_NAME).*o" -exec $(RM) {} \;
		$(DEPMOD)

$(MODULE_NAME).o:	$(OBJS)
		$(LD) -m elf_i386 -r -o $@ $(OBJS)

$(MODULE_NAME).ko:
		PWD=$(shell pwd)
		$(MAKE) -C $(KSRC) SUBDIRS=$(PWD) modules

kinstall:
		$(RM) -r $(KMODDIR)
		$(RM) $(KINCDIR)/omnibook.h
		$(RM) $(KINCDIR)/omnibook_ec.h
		$(MKDIR) $(KMODDIR)
		$(INSTALL) *.c $(KMODDIR)
		$(INSTALL) apmemu.h dmi.h ec.h init.h util.h $(KMODDIR)
		$(INSTALL) omnibook.h $(KINCDIR)
		$(MKDIR) $(KDOCDIR)
		$(INSTALL) doc/README doc/README-OneTouch $(KDOCDIR)
		
kpatch:		kinstall
		(cd $(KSRC); patch -p1 < $(BDIR)/misc/omnibook-integration.patch)

release:	clean
		mkdir -p ../$(MODULE_NAME)-$(TODAY)
		cp -a *.h *.c Makefile doc misc ../$(MODULE_NAME)-$(TODAY)
		sed "s|^\(#define OMNIBOOK_MODULE_VERSION.*\)\".*\"|\1\"$(TODAY)\"|" omnibook.h > ../$(MODULE_NAME)-$(TODAY)/omnibook.h
		rm -f ../$(MODULE_NAME)-$(TODAY).tar ../$(MODULE_NAME)-$(TODAY).tar.gz
		(cd ..; tar cvf $(MODULE_NAME)-$(TODAY).tar $(MODULE_NAME)-$(TODAY); gzip -9 $(MODULE_NAME)-$(TODAY).tar)

current:	clean
		rm -f ../$(MODULE_NAME)-current.tar ../$(MODULE_NAME)-current.tar.gz
		(cd ..; tar cvf $(MODULE_NAME)-current.tar $(MODULE_NAME)-current; gzip -9 $(MODULE_NAME)-current.tar)

# End of file
