KDIR:=/lib/modules/$(shell  uname -r)/build/ 

DEBIAN_VERSION_FILE:=/etc/debian_version
DEBIAN_DISTRO:=$(wildcard $(DEBIAN_VERSION_FILE))
CURRENT=$(shell uname -r)
MAJORVERSION=$(shell uname -r | cut -d '.' -f 1)
MINORVERSION=$(shell uname -r | cut -d '.' -f 2)
SUBLEVEL=$(shell uname -r | cut -d '.' -f 3)

ifeq ($(MAJORVERSION),6)
MDIR=kernel/drivers/tty/serial
else
ifeq ($(MAJORVERSION),5)
MDIR=kernel/drivers/tty/serial
else
ifeq ($(MAJORVERSION),4)
MDIR=drivers/tty/serial
else
ifeq ($(MAJORVERSION),3)
MDIR=drivers/tty/serial
else
ifeq ($(MAJORVERSION),2)
ifneq (,$(filter $(SUBLEVEL),38 39))
MDIR=drivers/tty/serial
else
MDIR=drivers/serial
endif
else
MDIR=drivers/serial
endif
endif
endif
endif
endif




RM_PATH:= /lib/modules/$(shell uname -r)/$(MDIR)/ax99100x.ko
PP_LIB_DIR:= $(shell ls /lib/modules/$(shell uname -r)/kernel/drivers/ | grep parport)
UNAME := $(shell uname -m)

# Get PCI info for parport_pc if needed
PCI_INFO := $(shell lspci -vv | grep -A10 "Parallel controller: Asix Electronics Corporation AX99100" || true)

# Get PCI BAR0, BAR1, and IRQ
PARPORT_IO    := $(shell lspci -vv | grep -A10 "Parallel controller: Asix Electronics Corporation AX99100" | grep "Region 0:" | awk '{print $$6}' || true)
PARPORT_IO_HI := $(shell lspci -vv | grep -A10 "Parallel controller: Asix Electronics Corporation AX99100" | grep "Region 1:" | awk '{print $$6}' || true)
PARPORT_IRQ   := $(shell lspci -vv | grep -A10 "Parallel controller: Asix Electronics Corporation AX99100" | grep "Interrupt:" | awk '{print $$7}' || true)



# Check if this is an ARM platform
ifeq ($(findstring arm,$(UNAME)),arm)
    # ARM platform - use ax99100x_pp
    obj-m += ax99100x.o
    obj-m += ax99100x_pp.o
	obj-m += ax99100x_i2c.o
    ax99100x-objs := ax99100x_spi.o ax99100x_sp.o
else ifeq ($(findstring aarch64,$(UNAME)),aarch64)
    # ARM64 platform - use ax99100x_pp
    obj-m += ax99100x.o
    obj-m += ax99100x_pp.o
	obj-m += ax99100x_i2c.o
    ax99100x-objs := ax99100x_spi.o ax99100x_sp.o
else
    # x86/x64 or other platforms - use both parport_pc and ax99100x_pp
    obj-m += ax99100x.o
    obj-m += parport_pc.o
    # obj-m += ax99100x_pp.o
	obj-m += ax99100x_i2c.o
    ax99100x-objs := ax99100x_spi.o ax99100x_sp.o
endif



default:
	$(RM) *.mod.c *.o *.ko .*.cmd *.symvers
	make -C $(KDIR) M=$(PWD) modules
ifneq ($(findstring arm,$(UNAME))$(findstring aarch64,$(UNAME)),)
	@echo "Compiling for ARM platform with ax99100x_pp"
else
	@echo "Compiling parport_pc for non-ARM platform"
	@if [ ! -z "$(PARPORT_IO)" ] && [ ! -z "$(PARPORT_IO_HI)" ] && [ ! -z "$(PARPORT_IRQ)" ]; then \
		echo "Found parport_pc configuration:"; \
		echo "  IO: 0x$(PARPORT_IO)"; \
		echo "  IO_HI: 0x$(PARPORT_IO_HI)"; \
		echo "  IRQ: $(PARPORT_IRQ)"; \
	fi
endif
	gcc -pthread select_BR.c -o select_BR
	gcc -pthread advanced_BR.c -o advanced_BR
	gcc -pthread gpio_99100.c -o gpio_99100
	gcc -pthread -Wall 9bit_test.c -o 9bit_test
	gcc -I /usr/include spi_test.c -o spi_test
	$(RM) *.mod.c *.o .*.cmd *.symvers *.mod
	rm -rf .tmp_version* *~
	rm -rf Module.markers modules.*
	find . -name "*.o.d" -type f -delete

install: default
	@echo "Checking and unbinding driver..."
	@for dev in $$(lspci -n | grep "125b:9100" | cut -d' ' -f1); do \
		if [ -e "/sys/bus/pci/devices/0000:$$dev/driver" ]; then \
			driver_path=$$(readlink -f /sys/bus/pci/devices/0000:$$dev/driver); \
			driver=$$(basename $$driver_path); \
			echo "Found device $$dev using driver: $$driver"; \
			echo "0000:$$dev" > /sys/bus/pci/devices/0000:$$dev/driver/unbind; \
			echo "Unbound $$dev from $$driver"; \
		else \
			echo "No active driver found for device $$dev"; \
		fi \
	done
ifeq ($(MAJORVERSION),6)
	$(MAKE) -C $(KDIR) M=$(PWD) modules_install
	cp ax99100x.ko  /lib/modules/$(shell uname -r)/$(MDIR)
	cp parport_pc.ko  /lib/modules/$(shell uname -r)/kernel/drivers/parport
	su -c "/sbin/depmod -a"
	modprobe ax99100x
	modprobe -r lp
	modprobe -r parport_pc
	modprobe parport_pc io=0x$(PARPORT_IO) io_hi=0x$(PARPORT_IO_HI)
	modprobe lp
else ifeq ($(MAJORVERSION),5)
	$(MAKE) -C $(KDIR) M=$(PWD) modules_install
	cp ax99100x.ko  /lib/modules/$(shell uname -r)/$(MDIR)
	cp parport_pc.ko  /lib/modules/$(shell uname -r)/kernel/drivers/parport
	su -c "/sbin/depmod -a"
	modprobe ax99100x
	modprobe -r lp
	modprobe -r parport_pc
	modprobe parport_pc io=0x$(PARPORT_IO) io_hi=0x$(PARPORT_IO_HI)
	modprobe lp
else
	cp ax99100x.ko  /lib/modules/$(shell uname -r)/kernel/$(MDIR)
	cp parport_pc.ko  /lib/modules/$(shell uname -r)/kernel/drivers/parport
	depmod -A
	chmod +x ax99100x
	cp ax99100x /etc/init.d/
ifeq ($(DEBIAN_DISTRO), $(DEBIAN_VERSION_FILE))
	ln -s /etc/init.d/ax99100x /etc/rcS.d/Sax99100x || true
else
	ln -s /etc/init.d/ax99100x /etc/rc.d/rc3.d/Sax99100x || true  	
	ln -s /etc/init.d/ax99100x /etc/rc.d/rc5.d/Sax99100x || true
endif
	modprobe ax99100x
	modprobe -r lp
	modprobe -r parport_pc
	modprobe parport_pc io=0x$(PARPORT_IO) io_hi=0x$(PARPORT_IO_HI)
	modprobe lp
endif

uninstall:
ifeq ($(MAJORVERSION),5)
ifneq ($(shell lsmod | grep ax99100x),)
	modprobe -r ax99100x
	modprobe -r lp
	modprobe -r parport_pc
	rm -f /lib/modules/$(shell uname -r)/kernel/$(MDIR)/ax99100x.*
	rm -f /lib/modules/$(shell uname -r)/kernel/drivers/parport/parport_pc.*
endif	
else ifeq ($(MAJORVERSION),6)	
ifneq ($(shell lsmod | grep ax99100x),)
	modprobe -r ax99100x
	modprobe -r lp
	modprobe -r parport_pc
	rm -f /lib/modules/$(shell uname -r)/kernel/$(MDIR)/ax99100x.*
	rm -f /lib/modules/$(shell uname -r)/kernel/drivers/parport/parport_pc.*
endif
	rm -rf $(RM_PATH)
	su -c "/sbin/depmod -a"
else
	modprobe -r ax99100x
	modprobe -r lp
	modprobe -r parport_pc
	rm -f /lib/modules/$(shell uname -r)/kernel/$(MDIR)/ax99100x.*
	rm -f /lib/modules/$(shell uname -r)/kernel/drivers/parport/parport_pc.*
	depmod -A
	rm -f /etc/init.d/ax99100x
ifeq ($(DEBIAN_DISTRO), $(DEBIAN_VERSION_FILE))
	rm -f /etc/init.d/ax99100x /etc/rcS.d/Sax99100x || true
else
	rm -f /etc/rc.d/rc3.d/Sax99100x
	rm -f /etc/rc.d/rc5.d/Sax99100x
endif
endif

clean:
	$(RM) *.mod.c *.o *.o.* *.ko .*.cmd *.symvers *.o.ur-safe *.mod *.o.d *.bak
	rm -rf .tmp_version* *~
	rm -rf Module.markers modules.* .cache.*
	rm -f .ax99100x_spi.o.d
	rm -f select_BR advanced_BR gpio_99100 spi_test 9bit_test
	find . -name "*.o.d" -type f -delete
	rm -f *.deb

deb:
	@echo "Building deb package..."
	@chmod +x build_deb.sh
	@./build_deb.sh
