#   src/sil-sa-app directory
#
# Sample/Dummy application to show how SIL-SA and YControl
# libraries are used in a polling loop in an application

# make all --> sil-sa-app  [sample SIL-SA test program]

ifdef MAC
 INSTALL_PREFIX ?= /usr/local
else
 INSTALL_PREFIX ?= /usr
endif

############### SOURCE PROFILE ##############################

PROJECT_NM=yumapro
SUBDIR_NM=sil-sa-app
SUBDIR_CPP=-I../subsys-pro -I../ycontrol -I../sil-sa

SRC_INST=$(DESTDIR)$(INSTALL_PREFIX)/share/yumapro/src/$(SUBDIR_NM)

# define to use a static SIL-SA library
# EXAMPLE: DO NOT USE IN REAL BUILD
# STATIC_SILSA=-L /home/andy/silsa -ltest2_sa

############### TARGET PROFILE ##############################

TARGET=$(TBASE)/$(SUBDIR_NM)
BIN_INST=$(TBASE)/bin
REAL_INST=$(DESTDIR)$(INSTALL_PREFIX)/bin
PROG=sil-sa-app

SUBSYS_TARGET=$(TBASE)/subsys-pro

AGT_TARGET=$(TBASE)/agt

STATIC_YANGLIB = -L../../../libietf-yang-library/lib -lietf-yang-library
STATIC_RESTMON = -L../../../libietf-restmonitoring/lib -lietf-restconf-monitoring

STATIC_LIBCOAP= -L../../../libcoap/.libs -lcoap-1
STATIC_LIBSNMP= -lnetsnmp


##################### LIBRARIES ########################

# The order of these LIBS matters!
#
# If the linker can't find external symbols you know should
# be there and you get an Unresolved External error
#   file foo : unresolved external to bar
# Then put the library that contains foo BEFORE the one
# that contains bar.

STATIC_LIBDICT = -L../../../libdict/bin -ldict

ifdef MAC
LFIRST=-L/sw/lib/x86_64
else
LFIRST=
endif

# all build variants use shared ncx library
# if FULL_STATIC ever supported again
#DLIBS = -l:$(LBASE)/libyumapro_ncx.a

ifdef MAC
CRYPT=
else
CRYPT=-lcrypt
endif

LIBS = -lyumapro_sil-sa -lyumapro_agt \
	-lyumapro_ycontrol -lsendbuff \
	-lyumapro_ncx

ifdef MAC
COAP_LIB=../../../libcoap/.libs/libcoap-1.a
SNMP_LIB=netsnmp
else
COAP_LIB=-l:../../../libcoap/.libs/libcoap-1.a
SNMP_LIB=-lnetsnmp
endif # MAC

# for now using stock libcoap default in /usr/local/lib
ifdef WITH_COAP
LIBS += $(COAP_LIB)
endif # WITH_COAP

ifdef WITH_SNMP
LIBS += $(SNMP_LIB)
endif # WITH_SNMP

ifdef WITH_YANG_CBOR
LIBS += -lcbor
endif # WITH_YANG_CBOR

ifndef FREEBSD
LIBS += -ldl
endif

# add the mandatory-to-implement RESTCONF libraries even if
# building STATIC version of the server
ifdef STATIC
LIBS += $(STATIC_LIBDICT) $(STATIC_YANGLIB)

 ifdef WITH_RESTCONF
LIBS += $(STATIC_RESTMON)
 else
  ifdef EVERYTHING
LIBS += $(STATIC_RESTMON)
  endif # EVERYTHING
 endif # WITH_RESTCONF

 ifdef WITH_COAP
LIBS += $(STATIC_LIBCOAP)
 endif # WITH_COAP

 ifdef WITH_SNMP
LIBS += $(STATIC_LIBSNMP)
 endif # WITH_SNMP

endif # STATIC

LIBCURL=
ifdef WITH_CURL
LIBCURL=-lcurl
else
 ifdef EVERYTHING
LIBCURL=-lcurl
 endif
endif

LIBSSL=
ifdef WITH_OPENSSL
LIBSSL=-lssl -lcrypto
else
 ifdef EVERYTHING
LIBSSL=-lssl -lcrypto
 endif
endif

# reorder the libs as per linker processing order
LIBS +=	-lxml2 $(LIBSSL) -lz -lm $(CRYPT) $(LIBCURL)

LIBS += $(STATIC_SILSA)

LOBJS = $(SUBSYS_TARGET)/subsystem.o
LOBJS += $(SUBSYS_TARGET)/subsys_util.o

LIBTARGS= $(LBASE)/libyumapro_sil-sa.$(LIBNCXSUFFIX) \
	$(LBASE)/libyumapro_ycontrol.$(LIBNCXSUFFIX) \
	$(LBASE)/libyumapro_agt.$(LIBNCXSUFFIX) \
	$(LBASE)/libyumapro_ncx.$(LIBNCXSUFFIX)

############################# MAKE RULES ##################

ALL_TARGETS=sil-sa-app

all: $(ALL_TARGETS)

#################### PLATFORM DEFINITIONS ############
include ../platform/platform.profile
include ../platform/mac.config

################ DEPENDENCIES #########################
# depend rule must be included after the 'all' make rule

include ../platform/platform.profile.depend

clean:
	rm -f $(OBJS) $(BIN_INST)/$(PROG)


superclean:
	rm -f *~ *.D dependencies $(OBJS) $(BIN_INST)/$(PROG)

test:

SOURCES=
ifndef BASIC
SOURCES=sources
endif

TARGETS=

ifdef DEBIAN
  ifeq "$(PACKAGE)" "SERVER"
    TARGETS=installdir
  else
    ifeq "$(PACKAGE)" "TOOLS"
    TARGETS=$(SOURCES)
    else
    TARGETS=installdir $(SOURCES)
    endif  # TOOLS
  endif # SERVER
else
  TARGETS=installdir $(SOURCES)
endif  # DEBIAN

# probably needs to be done by root
install: $(TARGETS)

installdir:
	mkdir -p $(REAL_INST)
	install $(OWNER) $(GRP) $(BIN_INST)/$(PROG) $(REAL_INST)

sources:
	mkdir -p $(SRC_INST)
	cp Makefile $(SRC_INST)/
	cp main.c $(SRC_INST)/

uninstall: superclean
	rm -f  $(REAL_INST)/$(PROG)
	rm -rf $(SRC_INST)

distclean: superclean

# this real rule keeps make from deleting the $(OBJS) as
# intermediate files
ifdef MAC
sil-sa-app: $(OBJS) $(LIBTARGS)
	LIBRARY_PATH=$(LIBRARY_PATH) $(LINK) $(LN_EXTRA) $(CFLAGS) $(CPPFLAGS) $(LFLAGS) $(OBJS) $(LOBJS) -o $(BIN_INST)/$(PROG) $(LPATH) $(FPATH) $(LIBS)

else
sil-sa-app: $(OBJS) $(LIBTARGS)
	$(LINK) $(LN_EXTRA) $(CFLAGS) $(CPPFLAGS) $(LFLAGS) $(OBJS) $(LOBJS) -o $(BIN_INST)/$(PROG) $(LPATH) $(FPATH) $(LIBS)

endif


.PHONY: sil-sa-app installdir sources

# prevent the make program from choking on all the symbols
# that get generated from autogenerated make rules
.NOEXPORT:

include ./dependencies
