ifeq ($(KERNELRELEASE),)
# --- External Build Phase ---

KVER           := $(shell uname -r)
KDIR           := /lib/modules/$(KVER)/build
KMISC          := /lib/modules/$(KVER)/kernel/drivers/net/

# Legacy logic for kernel version detection
KEXT           := $(shell echo $(KVER) | sed -ne 's/^2\.[567]\..*/k/p')o
KFLAG          := 2$(shell echo $(KVER) | sed -ne 's/^2\.[4]\..*/4/p')x

.PHONY: all modules clean distclean

all: modules

modules:
ifeq ($(KFLAG),24x)
	# Legacy: Build for Linux 2.4 kernels
	$(MAKE) -f Makefile_linux24x
else
	# Standard: Build for Linux 2.6 and newer (including 6.x)
	rm -f Module.symvers
	$(MAKE) -C $(KDIR) M=$(shell pwd) LDDINCDIR=$(shell pwd)/../include modules
endif

clean:
	rm -rf *.o *.ko *~ core* .dep* .*.d .*.cmd *.mod* *.a *.s .*.flags .tmp_versions Module.symvers

distclean:
	@if [ -e Makefile_linux24x ]; then      \
		rm -f Makefile_linux24x;                \
		rm -rf *.o *.ko *~ core* .dep* .*.d .*.cmd *.mod* *.a *.s .*.flags .tmp_versions;\
	else    \
		rm -rf *.o *.ko *~ core* .dep* .*.d .*.cmd *.mod* *.a *.s .*.flags .tmp_versions;\
	fi

else
# --- Internal Kbuild Phase (called by kernel build system) ---

# Use ccflags-y for modern kernels (fixes missing header errors in 6.x)
ccflags-y += -I$(src)
ccflags-y += -I$(src)/../include -std=gnu99

# Module object definitions
ixpci-objs       := _pci.o _proc.o
ixpcitmc12-objs  := _pcitmc12.o
ixpcip16x16-objs := _pcip16x16.o
ixpcip8r8-objs   := _pcip8r8.o
ixpci1800-objs   := _pci1800.o
ixpci1202-objs   := _pci1202.o
ixpci1002-objs   := _pci1002.o
ixpci1602-objs   := _pci1602.o
ixpcim512-objs   := _pcim512.o
ixpcifc16-objs   := _pcifc16.o
ixpcilanner-objs := _pcilanner.o
ixpci826-objs    := _pci826.o
ixpci822-objs    := _pci822.o
ixpcie8622-objs  := _pcie8622.o
ixpcid96-objs    := _pcid96_d128.o
ixpci2602u-objs  := _pci2602u.o

# Modules to be built
obj-m := ixpci.o ixpcitmc12.o ixpcip16x16.o ixpcip8r8.o \
         ixpci1800.o ixpci1202.o ixpci1002.o ixpci1602.o \
         ixpcim512.o ixpcifc16.o ixpcilanner.o ixpci826.o \
         ixpcie8622.o ixpci822.o ixpcid96.o ixpci2602u.o

endif # ifeq ($(KERNELRELEASE),)
