#Makefile File
#
# ----------------------------------------------------------------------------

PWD=$(shell pwd)

include Makefile.in

CC=gcc
RM      = @rm -rf
JUNK	= *~ *.bak DEADJOE

KVER	?= $(shell uname -r)
KNAME	?= $(shell uname -s)
KDIR	=/lib/modules/$(shell uname -r)/build
KVERBOSE=0
MODTYPE=ko

VERBOSE		=0
PROD_DIR	=$(PWD)
SRCS_DIR	=$(PROD_DIR)/src

TARGETS=all_voicesync
CLEANTARGETS=clean_voicesync  
INSTALLTARGETS=install_voicesync  

EXTRA_CFLAGS=$(GLOBAL_CFLAGS) -I$(KDIR)/include/linux -I$(PROD_DIR)/include -I$(PROD_DIR)/src
EXTRA_CFLAGS+=-D__Linux__ -DVS_VERBOSE=$(VERBOSE) 
ifdef STATS
EXTRA_CFLAGS+=-DENABLE_STATS 
endif
ifdef LED
EXTRA_CFLAGS+=-DENABLE_LED 
endif

all: check-dahdi check-kernel $(TARGETS)

all_voicesync:
	$(MAKE) KBUILD_VERBOSE=$(KVERBOSE) -C $(KDIR) SUBDIRS=$(SRCS_DIR) EXTRA_FLAGS="$(EXTRA_CFLAGS)" modules

clean: 	$(CLEANTARGETS)
	@find . -name '*symvers*' | xargs rm -f

clean_voicesync:
	$(MAKE) -C $(KDIR) SUBDIRS=$(SRCS_DIR) clean

install: $(INSTALLTARGETS)
	$(shell depmod -a)

install_voicesync:
	$(MAKE) -C $(KDIR) SUBDIRS=$(SRCS_DIR) modules_install

boot:
	@./scripts/mkboot.sh

help:
	@echo
	@echo " +--------- VoiceSync Help --------------+"
	@echo

	@echo " -> make          #build voicesync kernel module";
	@echo " -> make install  #install voicesync into kernel modules "; 
	@echo " -> make boot     #install  voicesync into zaptel boot script"; 
	@echo
	@echo " -> /etc/init.d/zaptel start   #start zaptel with voicesync";
	@echo ;

#Check for linux headers
check-kernel: 
	@if [ ! -e $(KDIR) ]; then \
		echo "   Error linux headers/source not found: $(KDIR) !"; \
		echo ; \
		exit 1; \
	fi 
	@if [ ! -e $(KDIR)/.config ]; then \
		echo "   Error linux headers/source not configured: missing $(KDIR)/.config !"; \
		echo ; \
		exit 1; \
	fi 
	@if [ ! -e $(KDIR)/include ]; then \
		echo "   Error linux headers/source incomplete: missing $(KDIR)/include dir !"; \
		echo ; \
		exit 1; \
	fi

#Check for dahdi/zaptel
check-dahdi:
	@echo
	@echo " +--------- Checking Dahdi/Zaptel Sources --------------+"  
	@echo 
	@if [ ! -e $(ZAPDIR)/zaptel.h ] && [ ! -e $(ZAPDIR)/kernel/zaptel.h ] && [ ! -e $(ZAPDIR)/include/dahdi/kernel.h ] ; then \
		echo " Compiling VoiceSync without ZAPTEL/DAHDI Support!"; \
		echo "      Zaptel/Dahdi Dir: $(ZAPDIR)"; \
		ZAPDIR_PRIV=; \
	else \
		if [ -e $(ZAPDIR)/include/dahdi/kernel.h ]; then \
			echo "   Compiling VoiceSync with DAHDI Support!"; \
			echo "      Dahdi Dir: $(ZAPDIR)"; \
			echo; \
			ZAPDIR_PRIV=$(ZAPDIR); \
			if [ -f $(ZAPDIR)/drivers/dahdi/Module.symvers ]; then \
			  	cp -f $(ZAPDIR)/drivers/dahdi/Module.symvers $(SRCS_DIR)/; \
			elif [ -f $(ZAPDIR)/src/dahdi-headers/drivers/dahdi/Module.symvers ]; then \
			  	cp -f $(ZAPDIR)/src/dahdi-headers/drivers/dahdi/Module.symvers $(SRCS_DIR)/; \
			else \
				echo "Error: Dahdi source not compiled, missing Module.symvers file"; \
				echo "	     Please recompile Dahdi directory first"; \
			fi; \
		else \
			echo "   Compiling VoiceSync with ZAPTEL Support!"; \
			echo "      Zaptel Dir: $(ZAPDIR)"; \
			echo; \
			ZAPDIR_PRIV=$(ZAPDIR); \
			if [ -f $(ZAPDIR)/kernel/Module.symvers ]; then \
				cp -f $(ZAPDIR)/kernel/Module.symvers $(SRCS_DIR)/; \
			elif [ -f $(ZAPDIR)/Module.symvers ]; then \
				cp -f $(ZAPDIR)/Module.symvers $(SRCS_DIR)/; \
			else \
				echo "Error: Zaptel source not compiled, missing Module.symvers file"; \
				echo "       Please recompile zaptel directory first"; \
			fi; \
			echo ; \
			echo "Please recompile and reinstall ZAPTEL after installation"; \
		fi \
	fi
	@echo 
	@echo " +-------------------------------------------+" 
	@echo 
	@sleep 2;              
