ifneq ($(KERNELRELEASE),)

obj-m	:= dev_acpi.o

else

#  FOO := $(error subrev == ${NUM})

KDIR	:= /lib/modules/$(shell uname -r)/build
KSRC	:= /lib/modules/$(shell uname -r)/source
PWD	:= $(shell pwd)
INC	:= -I $(KSRC)/include -I $(KDIR)/include
EXECS   := hpacpitree hpacpiexec
FILES   := dev_acpi $(EXECS)
CC	:= gcc
MAJOR	:= $(shell expr `uname -r` : '\([0-9]*\)' )
MINOR	:= $(shell expr `uname -r` : '[0-9]*.\([0-9]*\)' )
SUBREV	:= $(shell expr `uname -r` : '[0-9]*.[0-9]*.\([0-9]*\)' )

ifeq (${MAJOR}.${MINOR}, 2.6)
  PATCH_NUM := $(shell [ ${SUBREV} -ge 18 ] && echo 18 || echo 9)
  PATCHFILE := acpihdr.26.${PATCH_NUM}.patch
else
  PATCHFILE := acpihdr.24.patch
endif

# BUILDFLAG setup checks to see if certain typedefs need to be defined in order to get ACPI commands built 


BUILDFLAG:=$(shell gcc -I ${KSRC}/include -I ${KSRC}/include/acpi/platform typetest.c > /dev/null 2>&1 || echo "-DACPITYPEDEFS"; rm -f a.out)
CFLAGS	:= $(DEBUG) ${BUILDFLAG}

all: patch $(FILES)

# patch modifies some broken acpi header files in the linux 
# kernel headers and copies and renames the following patched files:
#   acenv.h, aclinux.h and acgcc.h to /usr/local/include 
#   These patched ehader files are required to build ACPI-aware
#   userspace utilities
patch:
	@cat ${KSRC}/include/acpi/platform/acenv.h  | awk '{if ($$0 ~/aclinux/){print "#include \"fixaclinux.h\"";} else { print $$0 }}' > /usr/local/include/devacpi_acenv.h
	@cp ${KSRC}/include/acpi/platform/acgcc.h /usr/local/include/devacpi_acgcc.h
	@cat ${KSRC}/include/acpi/platform/aclinux.h | awk '{if ($$0 ~/acgcc/){print "#include \"devacpi_acgcc.h\"";} else { print $$0 }}' > /usr/local/include/fixaclinux.h
	@egrep "endif.*__KERNEL|return current" /usr/local/include/fixaclinux.h | awk '(NR == 2){print $$0;}' | grep "current" && patch -d /usr/local/include -p3 < ${PATCHFILE} || true


clean:
	@[ -f /opt/hp/hpmgmtbase/ACPI/modules/RedHat/local/include/fixaclinux.h ] && rm /usr/local/include/fixaclinux.h /usr/local/include/devacpi* || true
	@[ -f hpmgmtbase*tar ] && rm hpmgmtbase*tar || true
	@[ -f /usr/local/include/fixaclinux.h ] && rm /usr/local/include/fixaclinux.h /usr/local/include/devacpi* || true
ifneq ($(findstring 2.6,$(KDIR)),)
	$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) clean
else
	rm -f dev_acpi.o
endif
	rm -f ${EXECS}

dev_acpi:
ifneq ($(findstring 2.6,$(KDIR)),)
	$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
else
	$(CC) -D__KERNEL__ -DMODULE -I $(KDIR)/include -O2 -c dev_acpi.c
endif

hpacpitree: acpitree.c
	$(CC) $(CFLAGS) $(INC) -o $@ acpitree.c

acpiundock:
	$(CC) $(CFLAGS) $(INC) -o $@ acpiundock.c

hpacpiexec: acpiexec.c
	$(CC) $(CFLAGS) $(INC) -o $@ acpiexec.c

tar: all
	echo $(shell . /etc/opt/hp/hpmgmtbase.conf; mkdir -p "opt/ACPI/modules/$$DISTROVENDOR/$$DISTROVERSION/$$PLATFORM/update90"; if [ -f dev_acpi.ko ]; then cp dev_acpi.ko "opt/ACPI/modules/$$DISTROVENDOR/$$DISTROVERSION/$$PLATFORM/update90"; else cp dev_acpi.o "opt/ACPI/modules/$$DISTROVENDOR/$$DISTROVERSION/$$PLATFORM/update90"; fi)
	$(shell . /etc/opt/hp/hpmgmtbase.conf; echo "ACPI base modules from opt/ACPI/modules/$$DISTROVENDOR/$$DISTROVERSION/$$PLATFORM/update90/" > "opt/ACPI/modules/$$DISTROVENDOR/$$DISTROVERSION/$$PLATFORM/update90/FYI.ACPI"; echo "Build by development server `hostname` for deployment on hpmgmtbase unsupported kernel `uname -r`" >> "opt/ACPI/modules/$$DISTROVENDOR/$$DISTROVERSION/$$PLATFORM/update90/FYI.ACPI";)
	@tar -cvf hpmgmtbase_acpi_`uname -r`.tar opt
	@echo -e "\n Just created acpi driver update for hpmgmtbase for\n unsupported kernel `uname -r`\n\n  To add ACPI functionality to servers running unsupported\n kernels with hpmgmtbase installed, follow these\n simple steps as root:\n\n    1.copy hpmgmtbase_acpi_`uname -r`.tar to \"/\"\n     on the server lacking dev_acpi support for hpmgmtbase.\n    2. on the commandline of the target server: \n      cd /\n    3. tar -xvf hpmgmtbase_acpi_`uname -r`.tar\n    4. /etc/init.d/hpmgmtbase reconfigure \n    5. /etc/init.d/hpmgmtbase start     # hoping no ACPI errs\n    6. rm hpmgmtbase_acpi_`uname -r`.tar\n\n"
	
install: clean all 
	$(shell . /etc/opt/hp/hpmgmtbase.conf; if [ ! -d "$$OPTTOP/ACPI/modules/$$DISTROVENDOR/$$DISTROVERSION/$$PLATFORM/update90" ]; then mkdir "$$OPTTOP/ACPI/modules/$$DISTROVENDOR/$$DISTROVERSION/$$PLATFORM/update90"; fi; if [ -f dev_acpi.ko ]; then cp dev_acpi.ko "$$OPTTOP/ACPI/modules/$$DISTROVENDOR/$$DISTROVERSION/$$PLATFORM/update90"; else cp dev_acpi.o "$$OPTTOP/ACPI/modules/$$DISTROVENDOR/$$DISTROVERSION/$$PLATFORM/update90"; fi)

endif

