################################################################################
# Sangoma MGD
#
# Author:   Anthony Minessale II <anthmct@yahoo.com>
#           Nenad Corbic <ncorbic@sangoma.com>
#
# Copyright:   (c) 2005 Anthony Minessale II
#
# 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.
################################################################################

SMG_DTMF=YES

#Default kernel directory to be overwritten by user
#Kernel version and location
ifndef KVER
       KVER=$(shell uname -r)
endif
ifndef KMOD
       KMOD=/lib/modules/$(KVER)
endif
ifndef KDIR
       KDIR=$(KMOD)/build
endif
ifndef KINSTDIR
       KINSTDIR=$(KMOD)/kernel
endif
ifndef ASTDIR
       ASTDIR=/usr/src/asterisk
endif


CC = gcc

INCLUDES = -I$(KDIR)/include -I ../../ssmg/libsangoma.trunk -I. -I ../../patches/kdrivers/include -I ../../patches/kdrivers/wanec/oct6100_api/include -I ../../patches/kdrivers/wanec -I/usr/local/include -I../../patches/kdrivers/include -I/usr/include/wanpipe -Ilib/libteletone/src

CFLAGS = -D__LINUX__ -D_REENTRANT -D_GNU_SOURCE -O6 -march=i686
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

ASTFLAGS=-I$(ASTDIR) -I$(ASTDIR)/include  -pipe  -Wall -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -g3 -Iinclude -I../include -D_REENTRANT -D_GNU_SOURCE  -O6 -march=i686 -fomit-frame-pointer  -fPIC

ifeq "${SMG_DTMF}" "YES"
LDFLAGS+= -lteletone
CFLAGS+= -DSMG_DTMF_ENABLE
endif


all: sangoma_mgd  chan_woomera.so 

smg: sangoma_mgd

woomera: chan_woomera.so

switch_buffer.o: switch_buffer.c switch_buffer.h
	$(CC) $(CCFLAGS) $(INCLUDES) $(CFLAGS)  -c -o switch_buffer.o switch_buffer.c

call_signal.o: call_signal.c call_signal.h
	$(CC) $(CCFLAGS) $(INCLUDES) $(CFLAGS)  -c -o call_signal.o call_signal.c

sangoma_mgd: sangoma_mgd.o call_signal.o switch_buffer.o sigboost.h
	rm -fr core*
	$(CC) $(CCFLAGS) $(INCLUDES) $(CFLAGS) -o sangoma_mgd sangoma_mgd.o switch_buffer.o call_signal.o $(LDFLAGS)

sangoma_mgd.o: sangoma_mgd.c sangoma_mgd.h sigboost.h
	$(CC) $(CCFLAGS) $(INCLUDES) $(CFLAGS) -c -o sangoma_mgd.o sangoma_mgd.c



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

chan_woomera.o: chan_woomera.c
	$(CC)  $(ASTFLAGS) -c chan_woomera.c -o chan_woomera.o



clean:  old_cleanup
	find . -name '*.o' | xargs rm -f
	rm -fr sangoma_mgd pritest *.o *.so *~ *core* *.so* *.a

distclean: clean
	@echo OK

install: all install_smg old_cleanup

install_smg: old_cleanup 
	install -D -m 755 sangoma_mgd $(INSTALLPREFIX)/usr/sbin/sangoma_mgd
	@if [ ! -e $(INSTALLPREFIX)/etc/sangoma_mgd.conf ]; then \
		install -D -m 755 sangoma_mgd.conf.sample $(INSTALLPREFIX)/etc/sangoma_mgd.conf; \
	fi
	install -D -m 755 smg_ctrl /usr/sbin/smg_ctrl
	install -D -m 755 scripts/init.d/smgss7_init_ctrl /etc/init.d
	@echo "sangoma_mgd Installed"

old_cleanup:
	./scripts/old_cleanup.sh


install_woomera:
	install -D -m 755 chan_woomera.so $(INSTALLPREFIX)/usr/lib/asterisk/modules/chan_woomera.so
	@if [ ! -e $(INSTALLPREFIX)/etc/asterisk/woomera.conf ]; then \
		install -D -m 755 woomera.conf $(INSTALLPREFIX)/etc/asterisk/woomera.conf; \
	fi
	@echo "chan_woomera Installed"


install_all: all install_smg install_woomera

uninstall: 
	/bin/rm /usr/sbin/sangoma_mgd /etc/sangoma_mgd.conf
