PKGNAME = voltha
TARGET = voltha.la

# Various configurable paths (remember to edit Makefile.in, not Makefile)
prefix = /usr/local
exec_prefix = ${prefix}
datarootdir = ${prefix}/share
datadir = ${datarootdir}
bindir = ${exec_prefix}/bin
includedir = ${prefix}/include
libdir =  ${exec_prefix}/lib
mandir = ${datarootdir}/man
libtool = ./libtool

CC = gcc
INSTALL = /bin/install -c
INSTALL_PROGRAM = ${INSTALL}
INSTALL_DATA = ${INSTALL} -m 644
LIBS = -lvoltha-netconf-model -lvoltha -lxml2 -lz -lm -ldl -lnetconf 
CFLAGS = -I/usr/include/libxml2  -O3
CPPFLAGS = 
LDFLAGS = 
LIBTOOL = $(libtool) --tag=CC --quiet

NETOPEER_MANAGER = /bin/netopeer-manager
NETOPEER_DIR = ${prefix}/etc/netopeer

MODEL = voltha.yin \
        voltha-config.rng \
        voltha-gdefs-config.rng \
        voltha-schematron.xsl

SRCS = voltha.c
OBJDIR = .obj
LOBJS = $(SRCS:%.c=$(OBJDIR)/%.lo)

all: $(TARGET)

$(TARGET): $(LOBJS)
	$(LIBTOOL) --mode=link $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(LIBS) -avoid-version -module -shared -export-dynamic --mode=link -o $@ $^ -rpath $(libdir)

$(OBJDIR)/%.lo: %.c
	@[ -d $$(dirname $@) ] || \
		(mkdir -p $$(dirname $@))
	$(LIBTOOL) --mode=compile $(CC) $(CFLAGS) $(CPPFLAGS) -fPIC -shared -c $< -o $@

.PHONY: install
install: $(TARGET)
	$(INSTALL) -m 775 -d $(DESTDIR)/$(libdir)
	$(LIBTOOL) --mode=install cp $(TARGET) $(DESTDIR)/$(libdir)/;
	$(INSTALL) -d $(NETOPEER_DIR)/voltha/
	@for i in $(MODEL); do \
		$(INSTALL_DATA) -m 600 $$i $(NETOPEER_DIR)/$(PKGNAME)/; \
	done
	if test -n "$(NETOPEER_MANAGER)"; then \
		if test -n "`$(NETOPEER_MANAGER) list | grep "^$(PKGNAME) ("`"; then \
			$(NETOPEER_MANAGER) rm --name $(PKGNAME); \
		fi; \
		$(NETOPEER_MANAGER) add --name $(PKGNAME) \
			--model $(NETOPEER_DIR)/$(PKGNAME)/$(PKGNAME).yin \
			--transapi $(DESTDIR)/$(libdir)/$(PKGNAME).so \
			--datastore $(NETOPEER_DIR)/$(PKGNAME)/datastore.xml; \
	fi

.PHONY: uninstall
uninstall:
	$(LIBTOOL) --mode=uninstall rm -rf $(DESTDIR)/$(libdir)/$(TARGET);
	rm -rf $(NETOPEER_DIR)/$(PKGNAME)/
	if test -n "$(NETOPEER_MANAGER)"; then \
		if test -n "`$(NETOPEER_MANAGER) list | grep "^$(PKGNAME) ("`"; then \
			$(NETOPEER_MANAGER) rm --name $(PKGNAME); \
		fi; \
	fi

.PHONY: clean
clean:
	$(LIBTOOL) --mode clean rm -f $(LOBJS)
	$(LIBTOOL) --mode clean rm -f $(TARGET)
	rm -rf $(TARGET) $(OBJDIR)
