DIRS=src patch
TOPDIR=$(shell pwd)
OBJDIR=$(TOPDIR)/objs
SCRIPTSDIR=$(TOPDIR)/scripts
INC=$(TOPDIR)/src/includes
VERSION=2.1.1
ID:=$(shell id -u)
tmpDIR:=/tmp/atm$(shell date +%S.%s)
tmpDES:=$(tmpDIR)/atmelwlandriver


export INC TOPDIR OBJDIR

.PHONY: dummy clean realclean all install config check help dist

help:
	  @echo "Pick one of the following targets:"
		@echo -e "\tmake clean\t\t- Remove all object files"
		@echo -e "\tmake realclean\t\t- Remove all object files and .config"
		@echo -e "\tmake config\t\t- Configure and check system setup"
		@echo -e "\tmake all\t\t- compile modules and programs"
		@echo -e "\tmake install\t\t- install modules and programs"
		@echo -e "\tmake ftags\t\t- build TAGS"


all:check 
	@if [ ! -d objs ]; then mkdir objs;fi
	@echo Building $(BUILD-y)
	@for i in $(BUILD-y); do make -C $$i all; done

ifeq (.config, $(wildcard .config))
include .config
check:dummy
else
check:config
endif

BUILD-y:=
BUILD-n:=
BUILD- :=
ifeq ('$(CONFIG_PCMCIA)', 'y')
BUILD-$(CONFIG_PCMCIA) += src/Pcmcia_Pci
ifeq ($(CONFIG_PCI), 'y')
endif
else
BUILD-$(CONFIG_PCI) += src/Pcmcia_Pci
endif
BUILD-$(CONFIG_USB) += src/usb
BUILD-$(CONFIG_CMD_LINE) += src/apps/cmd_line
BUILD-$(CONFIG_XAPP) += src/apps/XWlan

clean:
	find -name '*.o' -o -name '*.map' |xargs rm -f

realclean:clean
	rm -f .config
	@echo -e "Now run make config"

config:
	@$(CONFIG_SHELL) scripts/Config.sh
	@echo "Finished. Now run make clean, all, install"

install: 
	set -x
ifeq '$(ID)' '0'
	@if (ls $(OBJDIR)/pcm*.o >/dev/null 2>&1); then install -m 644 $(OBJDIR)/pcm*.o $(PCMCIA_DES); fi
	@if (ls $(OBJDIR)/pci*.o >/dev/null 2>&1); then install -m 644 $(OBJDIR)/pci*.o $(PCI_DES); fi
	@if (ls $(OBJDIR)/usb*.o >/dev/null 2>&1); then install -m 644 objs/usb*.o  $(USB_DES); fi
	@if [ -f $(OBJDIR)/lvnet ]; then install -m 755 $(OBJDIR)/lvnet /bin/; fi
	@if [ -f $(OBJDIR)/xvnet ]; then install -m 755 $(OBJDIR)/xvnet /usr/X11R6/bin/; fi
	@if [ -f $(SCRIPTSDIR)/fastvnet.sh ]; then install -m 755 $(SCRIPTSDIR)/fastvnet.sh /bin/; fi
	@bash $(SCRIPTSDIR)/append.sh
	@install -m 644 man/*.gz $(MAN_PATH)
	depmod -aq
	@echo OK
else
	@echo -ne "\033[0;31m"
	@echo "You must have root privileges to install the driver"
	@echo -ne "\033[1;37m"
endif

dist:
	@mkdir $(tmpDIR)
	@cp -R $(TOPDIR) $(tmpDES)
	@find $(tmpDES) -name CVS|xargs rm -rf
	@find $(tmpDES) -name '*.o'|xargs rm -f
	@rm -rf $(tmpDES)/html
	@rm -f $(tmpDES)/.config
	@if [ -f ../atmelwlandriver.$(VERSION).tar.bz2 ]; then rm -f ../atmelwlandriver.$(VERSION).tar.bz2; fi
	@cd $(tmpDIR); tar cjvf atmelwlandriver.$(VERSION).tar.bz2 atmelwlandriver >& /dev/null
	@mv $(tmpDIR)/*.bz2 ../
	@rm -rf $(tmpDIR)

ftags:
	ctags -R
	cd $(TOPDIR)/src/usb ; ctags -R . ../includes/usb
	cd $(TOPDIR)/src/Pcmcia_Pci ; ctags -R . ../includes/pcmcia
dummy:
