SHELL=/bin/bash
CMD_PATH="/bin /usr/bin /sbin /usr/sbin"
LSPCI=$(shell for i in "$(CMD_PATH)"; do if [ -x  $$i/lspci ]; then echo $$i; fi; done)
ifeq ($(MODEM_TYPE),)
MODEM_TYPE=$(shell $(LSPCI)/lspci -n | awk '{ \
	vendor_device_ids[0]="e159:0001";\
	vendor_device_ids[1]="1813:3052";\
	vendor_device_ids[2]="8086:1080";\
	vendor_device_ids[3]="1543:3052";\
	vendor_device_ids[4]="8086:1040";\
	vendor_device_ids[5]="10b9:5457";\
	vendor_device_ids[6]="1002:434D";\
	vendor_device_ids[7]="8086:2446";\
	vendor_device_ids[8]="8086:24c6";\
	vendor_device_ids[9]="8086:24d6";\
	vendor_device_ids[10]="10de:01c1";\
	vendor_device_ids[11]="1039:7013";\
	vendor_device_ids[12]="1106:3068";\
	for (i=0; (i in vendor_device_ids) && (vendor_device_ids[i] != $$3);){ \
	     i++ \
        } \
	if (i < 2) print "537"; \
	else if (i < 4) print "537EP"; \
	else if (i < 5) print "537SP"; \
	else if (i in vendor_device_ids) print "537EA";\
	}')
endif

all:
	@echo usage: make [clean] or [install] or [check] or [config_sync] or [uninstall] or [537]

clean:
	cd coredrv; make clean
	rm -f *.o *.ko

install:
	rm -f /usr/sbin/hamregistry.bin
	bash 537_inst

uninstall:
	@echo Uninstalling on system V boot like only.
	killall usrsound || echo
	killall hamregistry || echo
	rm -f /etc/rc.d/init.d/537_boot
	rm -f /etc/hamregistry.bin
	rm -f /usr/sbin/usrsound
	rm -f /usr/sbin/hamregistry
	rmmod Intel537
	rm -f `find /lib/modules -name Intel537.o`
	rm -f `find /lib/modules -name Intel537.ko`
	@echo Uninstalling done.

check:
	@bash config_check

config_sync:
	mv /lib/modules/`uname -r`/build/include/linux/version.h version.bak
	mv /lib/modules/`uname -r`/build/include/linux/autoconf.h autoconf.bak
	cp /boot/vmlinuz.version.h /lib/modules/`uname -r`/build/include/linux/version.h
	cp /boot/vmlinuz.autoconf.h /lib/modules/`uname -r`/build/include/linux/autoconf.h

537: 
	@[ -z $(MODEM_TYPE) ] && \
	echo "Modem type not determined." && \
        echo "$$ export MODEM_TYPE=<type>" && \
	echo "with type=537 or 537EP or 537SP or 537EA or 537AA" && \
	exit; \
	echo "Building for $(MODEM_TYPE)"; \
	uname -r|grep "2.6" && \
	cd coredrv && make INTEL_MODEM=$(MODEM_TYPE) 537=537 537core_26 && \
	cp Intel537.ko .. && cd .. && \
	strip --strip-debug Intel537.ko && \
	exit; \
	ls Intel537.ko >/dev/null 2>&1 || uname -r | grep "2.6" && echo "Failed to build driver" && exit; \
	if [ $(KERNEL_SOURCE_PATH) ]; then \
	cd coredrv; \
	make TARGET=TARGET_CATAWBA 537=537 INTEL_MODEM=$(MODEM_TYPE) KERNEL_SOURCE_PATH=$(KERNEL_SOURCE_PATH) "PSTN_DEF=-DTARGET_CATAWBA -DTARGET_LINUX -DLINUX" 537core; \
	else \
	cd coredrv; make TARGET=TARGET_CATAWBA INTEL_MODEM=$(MODEM_TYPE) 537=537 KERNEL_INCLUDES=/lib/modules/`uname -r`/build/include \
       "PSTN_DEF=-DTARGET_CATAWBA -DTARGET_LINUX -DLINUX" 537core; \
        fi ; \
	cp Intel537.o .. ; \
	if [ -a /boot/vmlinuz.version.h ]; then \
        cp /boot/vmlinuz.version.h /lib/modules/`uname -r`/build/include/linux/version.h;\
        fi
