# Makefile
#
# Copyright (C) 2005, Ranch Networks, Inc.
#
# 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.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
#
# Author : Srivatsa Chivukula <srivatsa@ranchnetworks.com>
#

CC=gcc
LDCONFIG = /sbin/ldconfig

OSARCH=$(shell uname -s)
PROC=$(shell uname -m)

CFLAGS=-Wall -Wstrict-prototypes -Wmissing-prototypes -D_GNU_SOURCE -DMIDCOM #-Werror 
SOFLAGS = -Xlinker -x -lstdc++ -lssl -lcrypto

INSTALL_PREFIX=
INSTALL_BASE=/usr

DESTDIR=
ASTLIBDIR=$(INSTALL_PREFIX)/usr/lib/asterisk
ASTHEADERDIR=$(INSTALL_PREFIX)/usr/include/asterisk
MODULES_DIR=$(ASTLIBDIR)/modules

ifeq (${OSARCH},Linux)
LDCONFIG_FLAGS=-n
else
ifeq (${OSARCH},FreeBSD)
LDCONFIG_FLAGS=-m
endif
endif
ifeq (${OSARCH},SunOS)
LDCONFIG = 
LDCONFIG_FLAGS = \# # Trick to comment out the period in the command below
SOSLINK = ln -sf res_netsec.so.1.0 res_netsec.so.1
#INSTALL_PREFIX = /opt/asterisk  # Uncomment out to install in standard Solaris location for 3rd party code
endif

#The problem with sparc is the best stuff is in newer versions of gcc (post 3.0) only.
#This works for even old (2.96) versions of gcc and provides a small boost either way.
#A ultrasparc cpu is really v9 but the stock debian stable 3.0 gcc doesnt support it.
ifeq ($(PROC),sparc64)
PROC=ultrasparc
CFLAGS += -mtune=$(PROC) -O3 -pipe -fomit-frame-pointer -mcpu=v8
endif

STAT_OBJS=$(shell find . ! -name "res_netsec.o" -a -name "*.o") ./res_netsec.o 
DYN_OBJS=$(shell find . ! -name "res_netsec.lo" -a -name "*.lo") ./res_netsec.lo 
STAT_LIBRARY=res_netsec.a
DYN_LIBRARY=res_netsec.so

SUBDIRS=midcom timer

all: subdirs $(STAT_LIBRARY) $(DYN_LIBRARY)

subdirs: 
	for x in $(SUBDIRS); do $(MAKE) -C $$x || exit 1; done

update:
	@if [ -d CVS ]; then \
		echo "Updating from CVS..." ; \
		cvs -q -z3 update -Pd; \
	else \
		echo "Not CVS";  \
	fi

$(STAT_LIBRARY): $(STAT_OBJS)
	$(AR) rcs $(STAT_LIBRARY) $(STAT_OBJS)
	ranlib $(STAT_LIBRARY)

$(DYN_LIBRARY) : $(DYN_OBJS)
	$(CC) -shared $(SOFLAGS) -o $@ $(DYN_OBJS)

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

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

install: all $(STAT_LIBRARY) $(DYN_LIBRARY)
	mkdir -p $(DESTDIR)$(MODULES_DIR)
	mkdir -p $(DESTDIR)$(ASTHEADERDIR)
	install -m 644 res_netsec.h $(DESTDIR)$(ASTHEADERDIR)
	install -m 755 $(DYN_LIBRARY) $(DESTDIR)$(MODULES_DIR)
	install -m 644 $(STAT_LIBRARY) $(DESTDIR)$(MODULES_DIR)

uninstall:
	@echo "Removing Libnetsec"
	rm -f $(DESTDIR)$(MODULES_DIR)/res_netsec.so
	rm -f $(DESTDIR)$(MODULES_DIR)/res_netsec.a
	rm -f $(DESTDIR)$(ASTHEADERDIR)/res_netsec.h

clean:
	rm -f *.o *.a *.so *.lo *.so.1 *.so.1.0
	rm -f $(STAT_LIBRARY) $(DYN_LIBRARY)
	rm -f $(STAT_OBJS) $(DYN_OBJS) 
	for x in $(SUBDIRS); do $(MAKE) -C $$x clean || exit 1; done
