# Copyright (c) 2006  Hewlett-Packard Development Company, L.P.

NAME=hpmouse
VERSION=1.1.0
RELEASE=1

LOCAL_X = Xincludes/usr/X11R6
                   
BINDIR = $(DESTDIR)/usr/bin
ROOTDIR = /opt/hp/hpmouse

ifeq ($(ARCH),)
  ARCH = $(shell /bin/arch)
endif
ifeq ($(ARCH),x86_64)
  ARCH_DEFINES = -D__x86_64__ -D_XSERVER64
  LIBDIR = lib64
  BITS=64
else
  ARCH_DEFINES = -D__i386__
  LIBDIR = lib
endif

INSTALLED_X = $(shell if [ -d /usr/$(LIBDIR)/xorg ] ; then \
                   echo -n "/usr/$(LIBDIR)/xorg" ; \
                elif [ -d /usr/X11R6 ] ; then \
                   echo -n "/usr/X11R6/$(LIBDIR)" ; \
                fi )

RPMSOURCES=$(shell if [ -f /etc/redhat-release ] ; then \
                      echo -n "/usr/src/redhat/SOURCES/" ; \
                   elif [ -f /etc/SuSE-release ] ; then \
                      echo -n "/usr/src/packages/SOURCES/" ; \
                   fi )
TARFILE=$(RPMSOURCES)$(NAME).tar.gz

OSDISTRO=$(shell   if [ -f /etc/redhat-release ] ; then \
                      echo -n "redhat" ; \
                   elif [ -f /etc/SuSE-release ] ; then \
                      echo -n "SLES" ; \
                   fi )

OSRELEASE=$(shell  if [ -f /etc/redhat-release ] ; then \
                      sed -n -e "s/.*release \([1-9]\).*$\/\1/p" /etc/redhat-release; \
                   elif [ -f /etc/SuSE-release ] ; then \
                      grep "VERSION" /etc/SuSE-release | cut -d' ' -f3; \
                   fi )
STORDIR=$(OSDISTRO)-$(OSRELEASE)-$(ARCH)

ifeq ($(TOP),)
  # This hack attempts to check if the needed XFree86 header files are installed.
  # It checks for a needed XFree86 4.3.00 SDK header file that is not installed by
  # default. If it is present, then it assumes that all header files are present.
  # If it is not present, then it assumes that all header files are not present
  # and uses the local copy of the XFree86 4.2.0 header files.
  X_INCLUDES_ROOT = $(shell \
    if [ -f $(INSTALLED_X)/lib/Server/include/xisb.h ] ; then \
      echo -n $(INSTALLED_X) ; \
    elif [ -f /usr/include/xorg/xisb.h ] ; then \
      echo -n "/usr/include/xorg" ; \
    else \
      echo -n $(LOCAL_X) ; \
    fi )
  #The Xorg package in Redhat 5 moved to flat dir
  ifeq ($(X_INCLUDES_ROOT),/usr/include/xorg)
     ALLINCLUDES = -I. -I$(X_INCLUDES_ROOT)
  else
     ALLINCLUDES = -I. -I$(X_INCLUDES_ROOT)/include/X11 \
                   -I$(X_INCLUDES_ROOT)/include/X11/extensions \
                   -I$(X_INCLUDES_ROOT)/lib/Server/include
  endif
else
  SERVERSRC = $(TOP)/programs/Xserver
  ALLINCLUDES = -I. \
	-I$(SERVERSRC)/hw/xfree86/common \
	-I$(SERVERSRC)/hw/xfree86/os-support \
	-I$(SERVERSRC)/mi \
	-I$(SERVERSRC)/include \
	-I$(TOP)/include
  X_INCLUDES_ROOT = $(TOP)
endif

MODULE_DEFINES = -DIN_MODULE -DXFree86Module
PROTO_DEFINES = -DFUNCPROTO=15 -DNARROWPROTO

STD_DEFINES = -Dlinux -D_POSIX_C_SOURCE=199309L -D_POSIX_SOURCE -D_XOPEN_SOURCE -D_BSD_SOURCE -D_SVID_SOURCE  -D_GNU_SOURCE  -DSHAPE -DXINPUT -DXKB -DLBX -DXAPPGROUP -DXCSECURITY -DTOGCUP   -DDPMSExtension  -DPIXPRIV -DPANORAMIX  -DRENDER -DGCCUSESGAS -DAVOID_GLYPHBLT -DPIXPRIV -DSINGLEDEPTH -DXFreeXDGA -DXvExtension -DXFree86LOADER  -DXFree86Server -DXF86VIDMODE  -DSMART_SCHEDULE -DBUILDDEBUG -DX_BYTE_ORDER=X_LITTLE_ENDIAN -DNDEBUG $(ARCH_DEFINES)
ALLDEFINES = $(ALLINCLUDES) $(STD_DEFINES) $(PROTO_DEFINES) $(MODULE_DEFINES)

check_gcc = $(shell if $(CC) $(1) -S -o /dev/null -xc /dev/null > /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi ;)

CCOPTIONS := -pedantic -Wall -Wpointer-arith 
CCOPTIONS += $(call check_gcc,-fno-merge-constants,)
CCOPTIONS += $(call check_gcc,-fno-pic,)
CDEBUGFLAGS = -O2
DRVCFLAGS =  $(CDEBUGFLAGS) $(CCOPTIONS) $(ALLDEFINES) -DVERSION="\"$(VERSION)\""
CFLAGS = $(CDEBUGFLAGS) -Wl,--strip-all

CC = gcc

LDCOMBINEFLAGS = -r

HELPER=hpmouse-helper
DRV=$(NAME)_drv.o

SRCS = xf86Evdev.c 
OBJS = xf86Evdev.o

#.c.o:
#	$(RM) $@
#	$(CC) -c $(DRVCFLAGS) $(_NOOP_) $*.c
ALLPKGBINS=$(STORDIR)/$(DRV) $(STORDIR)/$(HELPER) 

all:: $(ALLPKGBINS) $(STORDIR)/md5sum

$(STORDIR)/md5sum: $(ALLPKGBINS)
	md5sum $(ALLPKGBINS) >$(STORDIR)/md5sum

install: $(INSTALLED_X)/modules/input/$(DRV) $(ROOTDIR)/$(HELPER)

remove uninstall:
	rm -f $(INSTALLED_X)/modules/input/$(DRV)
	rm -f $(ROOTDIR)/$(HELPER)

$(INSTALLED_X)/modules/input/$(DRV):  $(STORDIR)/$(DRV)
	install --mode=0644 -D $(STORDIR)/$(DRV) $@

$(ROOTDIR)/$(HELPER): $(STORDIR)/$(HELPER)
	install --mode=0755 -D $(STORDIR)/$(HELPER) $@

$(STORDIR)/$(DRV): $(SRCS)
	$(RM) $@
	$(CC) -c $(DRVCFLAGS) $(_NOOP_) $(SRCS) 
	$(LD) -r $(OBJS) -o $(DRV) 
	mv $(DRV) $(STORDIR)

$(STORDIR)/$(HELPER): $(HELPER).c
	$(RM) $(HELPER)
	$(CC) $(CFLAGS) -o $(HELPER) $(HELPER).c 
	mv $(HELPER) $(STORDIR)

clean::
	$(RM) *.CKP *.ln *.BAK *.bak *.o core errs ,* *~ *.a .emacs_* tags TAGS make.log MakeOut synclient syndaemon "#"* manpages/*~ evdev-$(VERSION).tar.bz2

cleandistro: 
	$(RM) $(ALLPKGBINS) $(STORDIR)/md5sum

touch:
	touch -c $(HELPER).c $(SRCS)
	touch $(STORDIR)/* 

tags::
	etags -o TAGS *.c *.h

distribution : evdev-$(VERSION).tar.bz2

rpm : clean $(NAME)-$(VERSION)-$(RELEASE).spec
	rm -f $(TARFILE)
	cp hprpm.xpm $(RPMSOURCES)
	tar --exclude "CVS" --exclude "build" --exclude "debian" --exclude BuildIt.Lx --exclude deliverables -C../ -czvf $(TARFILE) hpmouse 
	rpmbuild -ba --target noarch $(NAME)-$(VERSION)-$(RELEASE).spec

$(NAME)-$(VERSION)-$(RELEASE).spec: $(NAME).spec
	cat $(NAME).spec | sed -e 's/^Version[ \t]*: [0-9.]*/Version\t\t: $(VERSION)/' -e 's/^Release[ \t]*: [0-9.]*/Release\t\t: $(RELEASE)/' >$(NAME)-$(VERSION)-$(RELEASE).spec

.PHONY: FORCE
