################################################################################
# chan_woomera: Woomera channel driver for Asterisk/CallWeaver
#
# Author:   Nenad Corbic <ncorbic@sangoma.com>
#	    Anthony Minessale II <anthmct@yahoo.com>
#
# Copyright:   (c) 2007 Nenad Corbic <ncorbic@sangoma.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version
# 2 of the License, or (at your option) any later version.
################################################################################

ifndef PBXDIR
	PBXDIR=/usr/src/asterisk
ifneq (,$(wildcard ./.pbxdir))
	PBXDIR=$(shell cat .pbxdir)
endif
endif

CHAN_NAME=WOOMERA

$(shell echo $(PBXDIR) > .pbxdir)	

PBXMODDIR=/usr/lib/asterisk/modules
PBXCFGDIR=/etc/asterisk
PBXDEFINES=

#Check for Asterisk AST_CONTROL_SRC feature
$(shell if [[ -f $(PBXDIR)/include/asterisk/frame.h ]]; then grep -c AST_CONTROL_SRC $(PBXDIR)/include/asterisk/frame.h > .ast_src_check; else echo 0 > .ast_src_check; fi;)
ifeq (1,$(shell cat .ast_src_check))
	PBXDEFINES=-DWOO_CONTROL_SRC_FEATURE
endif

ifneq (,$(wildcard $(PBXDIR)/include/callweaver.h))
	PBXFLAGS_EXTRA=-DCALLWEAVER 
	PBXMODDIR=/usr/local/lib/callweaver/modules
	PBXCFGDIR=/usr/local/etc/callweaver
endif

ifneq (,$(wildcard $(PBXDIR)/configure))
	PBXFLAGS_EXTRA=-DAST14 -DAST_MODULE=\"chan_woomera\"
endif	

CC = gcc

INCLUDES= -I/usr/include -I./

CFLAGS = -D__LINUX__ -D_REENTRANT -D_GNU_SOURCE -O6 
CCFLAGS = -Wall -Wstrict-prototypes -Wmissing-prototypes -g
LDFLAGS=-L lib/libteletone/.libs -L. -L/usr/local/lib -L ../../ssmg/libsangoma.trunk/.libs -lpthread -lsangoma -lm


PBXFLAGS= $(INCLUDES) -I$(PBXDIR) -I$(PBXDIR)/include -pipe  -Wall -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -g3 -Iinclude -I../include -D_REENTRANT  -DWOOMERA_CHAN_NAME=\"$(CHAN_NAME)\" -D_GNU_SOURCE $(PBXDEFINES)  -O6 -fomit-frame-pointer  -fPIC


all: chan_woomera.so 

chan_woomera.so: chan_woomera.o
	$(CC) -shared -Xlinker -x -o chan_woomera.so chan_woomera.o

chan_woomera.o: chan_woomera.c
	$(CC)  $(PBXFLAGS) $(PBXFLAGS_EXTRA) -c chan_woomera.c -o chan_woomera.o

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

distclean: clean
	@echo OK

install: all  
	@if [ ! -d $(INSTALLPREFIX)$(PBXMODDIR) ]; then \
		mkdir -p $(INSTALLPREFIX)$(PBXMODDIR); \
	fi; 
	install -D -m 755 chan_woomera.so $(INSTALLPREFIX)$(PBXMODDIR)/chan_woomera.so
	@if [ ! -d $(INSTALLPREFIX)$(PBXCFGDIR) ]; then \
		mkdir -p $(INSTALLPREFIX)$(PBXCFGDIR); \
	fi; 
	@if [ -f woomera.conf ] && [ ! -e $(INSTALLPREFIX)$(PBXCFGDIR)/woomera.conf ]; then \
		install -D -m 755 woomera.conf $(INSTALLPREFIX)$(PBXCFGDIR)/woomera.conf; \
	fi
	@echo "chan_woomera Installed"

