################################################################################
# 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=
PBXFLAGS_EXTRA=

#Check for Asterisk AST_CONTROL_SRC feature
DUMMY:=$(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


#Check for Callweaver opbx_cli_entry
DUMMY:=$(shell if [[ -f $(PBXDIR)/include/callweaver/cli.h ]]; then grep -c opbx_cli_entry $(PBXDIR)/include/callweaver/cli.h > .opbx_cli_entry_check; else echo 0 > .opbx_cli_entry_check; fi;)

ifneq (0,$(shell cat .opbx_cli_entry_check))
        PBXFLAGS_EXTRA+=-DCALLWEAVER_OPBX_CLI_ENTRY
endif


#Check for callweaver cw_cli_entry
DUMMY:=$(shell if [[ -f $(PBXDIR)/include/callweaver/cli.h ]]; then grep -c cw_cli_entry $(PBXDIR)/include/callweaver/cli.h > .cw_cli_entry_check; else echo 0 > .cw_cli_entry_check; fi;)

ifneq (0,$(shell cat .cw_cli_entry_check))
        PBXFLAGS_EXTRA+=-DCALLWEAVER_CW_CLI_ENTRY
endif


#Check for callweaver MODULE_INFO
DUMMY:=$(shell if [[ -f $(PBXDIR)/include/callweaver/module.h ]]; then grep -c MODULE_INFO $(PBXDIR)/include/callweaver/module.h > .cw_module_info_check; else echo 0 > .cw_module_info_check; fi;)

ifneq (0,$(shell cat .cw_module_info_check))
        PBXFLAGS_EXTRA+=-DCALLWEAVER_MODULE_INFO
endif


ifneq (,$(wildcard $(PBXDIR)/include/callweaver/cwobj.h))
        PBXFLAGS_EXTRA+=-DCALLWEAVER_CWOBJ
endif


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

else 

#Check for Asterisk 1.6
DUMMY:=$(shell if [[ -f $(PBXDIR)/.version ]]; then grep -c ^1.6  $(PBXDIR)/.version > .ast16_check; else echo 0 > .ast16_check; fi;)
ifneq (0,$(shell cat .ast16_check))
        PBXFLAGS_EXTRA+=-DAST16
endif

DUMMY:=$(shell if [[ -f $(PBXDIR)/.version ]]; then grep -c ^1.4  $(PBXDIR)/.version > .ast14_check; else echo 0 > .ast14_check; fi;)
ifneq (0,$(shell cat .ast14_check))
        PBXFLAGS_EXTRA+=-DAST14
endif
endif



PBXFLAGS_EXTRA+= -DAST_MODULE=\"chan_woomera\"

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"

