# what compiler
CC=gcc -Wall

# where is the perl binary **PRETTY IMPORTANT**
PERL        =/usr/local/bin/perl
PERL_LIBDIR =-L$(shell perl -MConfig -e 'print $$Config{archlib}')/CORE
PERL_LIBS   =$(shell perl -MConfig -e 'print $$Config{libs}')

# where is the asterisk source tree
ASTSRC = /usr/src/asterisk

LIBS = $(PERL_LIBS)


PWD=$(shell pwd)

#CFLAGS += $(shell if [ -f "$(ASTSRC)/res/res_config.c" ]; then echo "-DHAVE_RES_CONFIG"; fi)
CFLAGS += -DHAVE_AST_CUST_CONFIG



#####################################################################################
# copy these from the asterisk top level Makefile
INSTALL_PREFIX=
ASTLIBDIR=$(INSTALL_PREFIX)/usr/lib/asterisk
ASTVARLIBDIR=$(INSTALL_PREFIX)/var/lib/asterisk
ASTETCDIR=$(INSTALL_PREFIX)/etc/asterisk
ASTSPOOLDIR=$(INSTALL_PREFIX)/var/spool/asterisk
ASTLOGDIR=$(INSTALL_PREFIX)/var/log/asterisk
ASTHEADERDIR=$(INSTALL_PREFIX)/usr/include/asterisk
ASTCONFPATH=$(ASTETCDIR)/asterisk.conf
ASTBINDIR=$(INSTALL_PREFIX)/usr/bin
ASTSBINDIR=$(INSTALL_PREFIX)/usr/sbin
ASTVARRUNDIR=$(INSTALL_PREFIX)/var/run
MODULES_DIR=$(ASTLIBDIR)/modules
AGI_DIR=$(ASTVARLIBDIR)/agi-bin


CFLAGS +=  $(INCLUDES)
CFLAGS +=  -DAST_INSTALL_PREFIX=\"$(INSTALL_PREFIX)\" -DASTETCDIR=\"$(ASTETCDIR)\" -DASTLIBDIR=\"$(ASTLIBDIR)\"
CFLAGS +=  -DASTVARLIBDIR=\"$(ASTVARLIBDIR)\" -DASTVARRUNDIR=\"$(ASTVARRUNDIR)\" -DASTSPOOLDIR=\"$(ASTSPOOLDIR)\" -DASTLOGDIR=\"$(ASTLOGDIR)\"
CFLAGS +=  -DASTCONFPATH=\"$(ASTCONFPATH)\" -DASTMODDIR=\"$(MODULES_DIR)\" -DASTAGIDIR=\"$(AGI_DIR)\" 
CFLAGS +=  -DMULTIPLICITY $(shell $(PERL) -MExtUtils::Embed -e ccopts) 
CFLAGS +=  -D_GNU_SOURCE

LDFLAGS  =  $(shell $(PERL) -MExtUtils::Embed -e ldopts)
LDFLAGS +=  $(shell perl -MConfig -e 'print $$Config{libs}')
INCLUDES =  -I$(ASTSRC) -I$(ASTSRC)/include -I.

OBJS=perlxsi.o res_perl.o apihelp.o




all: res_perl.so api

.perlok:
	@(${PERL} -V | grep -i usemultiplicity=define >/dev/null && echo Phew, You have the right perl.) || ((echo Sorry, you need to compile perl with threads and multiplicity. && echo If you are lazy run buildperl to auto-download and install the correct version && echo Make sure you dont have any exta libperl\*.so\* anywhere. && exit 1))
	@touch .perlok

uninstall:
	@/bin/rm -fr $(ASTETCDIR)/perl $(MODULES_DIR)/res_perl.so
	@echo OK ITS GONE \(not the ast patch! =\>\)

new_config: wipe_config install

wipe_config:
	/bin/rm -fr $(ASTETCDIR)/perl

AstAPI_wrap.c:
	/bin/cat AstAPIBase.c | grep -v AST_MUTEX_DEFINE_STATIC > AstAPIBase2.c
	swig -DMULTIPLICITY -perl5 -module AstAPIBase AstAPIBase2.c
	/bin/rm AstAPIBase2.c
	/bin/mv AstAPIBase2_wrap.c AstAPIBase_wrap.c
perlxsi.c:
	perl -MExtUtils::Embed -e xsinit


AstAPIBase_wrap.o: 
	@echo
	@echo THIS FILE IS AUTO GENERATED BY SWIG SO IT IS GONNA GENERATE COMPILER WARNINGS
	@echo =============================================================================
	$(CC) -fPIC $(CFLAGS) -c -o $@ AstAPIBase_wrap.c
	@echo =============================================================================
	@echo

%.o:  %.c
	$(CC) -fPIC $(CFLAGS) -c -o $@ $< 


res_perl.so: .perlok $(OBJS) AstAPIBase.o
	$(CC) -fPIC -shared -Xlinker -x -o $@ $(OBJS) $(LDFLAGS) $(PERL_LINKED) AstAPIBase.o

install_api: all
	/bin/cp -p AstAPIBase.so $(ASTETCDIR)/perl
	/bin/cp -p INC/AstAPIBase.pm INC/AstAPI.pm $(ASTETCDIR)/perl

install: all 
	@/bin/cp -p -u res_perl.so $(MODULES_DIR)
	@if [ ! -d $(ASTETCDIR)/perl ] ; then mkdir $(ASTETCDIR)/perl ; fi
	@/bin/cp -p AstAPIBase.so $(ASTETCDIR)/perl
	@/bin/cp -p INC/AstAPIBase.pm INC/AstAPI.pm $(ASTETCDIR)/perl
	@/bin/cp -r -p -i --reply=no INC/* $(ASTETCDIR)/perl
	@/bin/rm -fr `find $(ASTETCDIR)/perl -type d -name CVS`
	@echo OK INSTALL COMPLETE

clean:
	rm -f *.o *.so *~ perlxsi.c

swigclean: clean
	rm -f *_wrap.c *.pm

distclean: clean
	rm -f *.cache


reswig: swigclean AstAPI_wrap.c
	/bin/mv AstAPIBase.pm INC 


AstAPIBase.so: AstAPIBase_wrap.c AstAPIBase.o AstAPIBase_wrap.o  perlxsi.o
	$(CC) -fPIC -shared -Xlinker -x apihelp.o AstAPIBase.o AstAPIBase_wrap.o res_perl.o perlxsi.o  $(LDFLAGS) -o AstAPIBase.so


api: AstAPIBase.so 

perl: 
	./buildperl

everything: patch asterisk clean all install


patch:
	@echo applying the necessary ast patch for res_perl
	@(/bin/grep -q perlxsi.o $(ASTSRC)/Makefile && echo Patch Already Applied Don\'t Fret!) || (cd $(ASTSRC) && \
	perl -MExtUtils::Embed -e xsinit && patch -N -t -p0 < $(PWD)/astmake.diff || echo Patch Already Applied Don\'t Fret!)


asterisk:
	@cd $(ASTSRC) && /bin/rm -f asterisk && make install


update:
	@echo "Updating from CVS"
	@cvs update -d
