#
# ***** BEGIN LICENSE BLOCK *****
# 
# Copyright (C) 2009-2016 Olof Hagsand and Benny Holmgren
# Copyright (C) 2017-2019 Olof Hagsand
# Copyright (C) 2020-2022 Olof Hagsand and Rubicon Communications, LLC(Netgat)e
#
# This file is part of CLIXON
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Alternatively, the contents of this file may be used under the terms of
# the GNU General Public License Version 3 or later (the "GPL"),
# in which case the provisions of the GPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of the GPL, and not to allow others to
# use your version of this file under the terms of Apache License version 2, 
# indicate your decision by deleting the provisions above and replace them with
# the notice and other provisions required by the GPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the Apache License version 2 or the GPL.
#
# ***** END LICENSE BLOCK *****
#
# Note, for linkage=static, libclixon is linked twice:
# First static when building:
#    libclixon_backend, libclixon_cli, and libclixon_restconf
# Second in this Makefile dynamic using -lclixon
# This means global variables used in plugin code is in separate domains and will not work
# Dont know enough about dynamic/static linkage to fix it.
# One way is to not use global variables in libraries, but there are still some, eg in error
# and log modules

srcdir  	= .
top_srcdir  	= ../..
prefix 		= /usr/local
bindir 		= ${exec_prefix}/bin
includedir	= ${prefix}/include
sysconfdir      = ${prefix}/etc
datarootdir	= ${prefix}/share
localstatedir   = ${prefix}/var
libdir          = ${prefix}/lib

APPNAME	        = example
# Here is where example yang appears
YANG_INSTALLDIR  = /usr/local/share/clixon
# Install here if you want default clixon location:
CLIXON_DEFAULT_CONFIG = /usr/local/etc/clixon.xml

CC		= gcc
CFLAGS  	= -O2 -Wall -fPIC

ifeq ($(HOST_VENDOR),apple)
INSTALLFLAGS =
else
INSTALLFLAGS = -s
endif

with_restconf	= fcgi

INCLUDES 	= -I$(DESTDIR)$(includedir) 
LINKAGE         = dynamic
LDFLAGS 	= 
CPPFLAGS  	= -DHAVE_CONFIG_H 
ifeq ($(LINKAGE),dynamic)
CPPFLAGS       	+= -fPIC
endif

BE_PLUGIN       = $(APPNAME)_backend.so
BE2_PLUGIN      = $(APPNAME)_backend_nacm.so
CLI_PLUGIN      = $(APPNAME)_cli.so
NETCONF_PLUGIN  = $(APPNAME)_netconf.so
RESTCONF_PLUGIN = $(APPNAME)_restconf.so

PLUGINS = $(BE_PLUGIN) $(BE2_PLUGIN) $(CLI_PLUGIN) $(NETCONF_PLUGIN)
ifdef with_restconf
PLUGINS += $(RESTCONF_PLUGIN)
endif

.PHONY: all clean depend install 

all:	$(PLUGINS)

.SUFFIXES: .c .o

# implicit rule
.c.o:
	$(CC) $(INCLUDES) $(CPPFLAGS) $(CFLAGS) -c $<

CLISPECS = $(APPNAME)_cli.cli

YANGSPECS	= clixon-example@2022-11-01.yang

# Backend plugin
BE_SRC   = $(APPNAME)_backend.c
BE_OBJ   = $(BE_SRC:%.c=%.o)
$(BE_PLUGIN): $(BE_OBJ)
ifeq ($(LINKAGE),dynamic)
	$(CC) -Wall -shared $(LDFLAGS) -o $@ -lc $< -lclixon -lclixon_backend
else
	# can include -L in LDFLAGS?
	$(CC) -Wall -shared $(LDFLAGS) -o $@ -lc $< -lclixon -lclixon_backend
endif

# Secondary NACM backend plugin
BE2_SRC  = $(APPNAME)_backend_nacm.c
BE2_OBJ  = $(BE2_SRC:%.c=%.o)
$(BE2_PLUGIN): $(BE2_OBJ)
ifeq ($(LINKAGE),dynamic)
	$(CC) -Wall -shared $(LDFLAGS) -o $@ -lc $< -lclixon -lclixon_backend
else
	$(CC) -Wall -shared $(LDFLAGS) -o $@ -lc $< -lclixon -lclixon_backend
endif

# CLI frontend plugin
CLI_SRC  = $(APPNAME)_cli.c
CLI_OBJ  = $(CLI_SRC:%.c=%.o)
$(CLI_PLUGIN): $(CLI_OBJ)
ifeq ($(LINKAGE),dynamic)
	$(CC) -Wall -shared $(LDFLAGS) -o $@ -lc $^ -lclixon -lclixon_cli
else
	$(CC) -Wall -shared $(LDFLAGS) -o $@ -lc $^ -lclixon -L ../../apps/cli/ -lclixon_cli
endif

# NETCONF frontend plugin
NETCONF_SRC  = $(APPNAME)_netconf.c
NETCONF_OBJ  = $(NETCONF_SRC:%.c=%.o)
$(NETCONF_PLUGIN): $(NETCONF_OBJ)
ifeq ($(LINKAGE),dynamic)
	$(CC) -Wall -shared $(LDFLAGS) -o $@ -lc $^ -lclixon
else
	$(CC) -Wall -shared $(LDFLAGS) -o $@ -lc $^ -lclixon
endif

# See configure.ac
# RESTCONF frontend plugin
RESTCONF_SRC  = $(APPNAME)_restconf.c
RESTCONF_OBJ  = $(RESTCONF_SRC:%.c=%.o)
$(RESTCONF_PLUGIN): $(RESTCONF_OBJ)
ifeq ($(LINKAGE),dynamic)
	$(CC) -Wall -shared $(LDFLAGS) -o $@ -lc $^ -lclixon -lclixon_restconf
else
# Linkage -static:
	$(CC) -Wall -shared $(LDFLAGS) -o $@ -lc $^ -lclixon -L ../../apps/restconf/ -lclixon_restconf
#	$(CC) -Wall -shared $(LDFLAGS) -o $@ -lc $^ -lclixon -lclixon_restconf
endif

SRC     = $(BE_SRC) $(BE2_SRC) $(CLI_SRC) $(NETCONF_SRC)
SRC    +=  $(RESTCONF_SRC)

OBJS    = $(BE_OBJ) $(BE2_OBJ) $(CLI_OBJ) $(NETCONF_OBJ)
OBJS   += $(RESTCONF_OBJ)

clean:
	rm -f $(PLUGINS) $(OBJS) 
	rm -f *.gcda *.gcno *.gcov # coverage

distclean: clean
	rm -f Makefile *~ .depend

install: $(YANGSPECS) $(CLISPECS) $(PLUGINS) $(APPNAME).xml
	install -d -m 0755 $(DESTDIR)$(sysconfdir)
	install -m 0644 $(APPNAME).xml $(DESTDIR)$(sysconfdir)
#	install -m 0644 $(APPNAME).xml $(DESTDIR)$(CLIXON_DEFAULT_CONFIG)
	install -d -m 0755 $(DESTDIR)$(datarootdir)/$(APPNAME)/yang
	install -d -m 0755 $(DESTDIR)$(YANG_INSTALLDIR)
	install -m 0644 $(YANGSPECS) $(DESTDIR)$(YANG_INSTALLDIR)
	install -d -m 0755 $(DESTDIR)$(libdir)/$(APPNAME)/cli
	install -m 0644 $(INSTALLFLAGS) $(CLI_PLUGIN) $(DESTDIR)$(libdir)/$(APPNAME)/cli
	install -d -m 0755 $(DESTDIR)$(libdir)/$(APPNAME)/backend
	install -m 0644 $(INSTALLFLAGS) $(BE_PLUGIN) $(BE2_PLUGIN) $(DESTDIR)$(libdir)/$(APPNAME)/backend
	install -d -m 0755 $(DESTDIR)$(libdir)/$(APPNAME)/netconf
	install -m 0644 $(INSTALLFLAGS) $(NETCONF_PLUGIN) $(DESTDIR)$(libdir)/$(APPNAME)/netconf
ifdef with_restconf
	install -d -m 0755 $(DESTDIR)$(libdir)/$(APPNAME)/restconf
	install -m 0644 $(INSTALLFLAGS) $(RESTCONF_PLUGIN) $(DESTDIR)$(libdir)/$(APPNAME)/restconf
endif
	install -d -m 0755 $(DESTDIR)$(libdir)/$(APPNAME)/clispec
	install -m 0644 $(CLISPECS) $(DESTDIR)$(libdir)/$(APPNAME)/clispec
	install -d -m 0755 $(DESTDIR)$(localstatedir)/$(APPNAME) # This is for XMLDB_DIR
	install -d -m 0755 $(DESTDIR)$(localstatedir)/run # This is for PID and SOCK

uninstall: 
	rm -rf $(DESTDIR)$(sysconfdir)/$(APPNAME).xml
	rm -rf $(DESTDIR)$(datarootdir)/$(APPNAME)
	rm -rf $(DESTDIR)$(localstatedir)/$(APPNAME)
	rm -rf $(DESTDIR)$(libdir)/$(APPNAME)

install-include:	

depend:
	$(CC) $(DEPENDFLAGS) $(INCLUDES) $(CFLAGS) -MM $(SRC) > .depend

#include .depend

