
include ${LIBRESWANSRCDIR}/Makefile.ver
include ${LIBRESWANSRCDIR}/Makefile.inc

LEX=flex
BISON=bison
RM=rm

INCLUDES+=-I${LIBRESWANSRCDIR} -I${KLIPSINC} -I${LIBRESWANSRCDIR}/include ${NSSFLAGS}

CFLAGS+=-pthread

CFLAGS+=${USERCOMPILE} ${PORTINCLUDE} ${INCLUDES} ${CROSSFLAGS}

ifeq ($(USE_LIBCURL),true)
CFLAGS+=-DLIBCURL
endif

ifeq ($(USE_LDAP),true)
CFLAGS+=-DLDAP_VER=3
endif

ifeq ($(USE_SAREF_KERNEL),true)
CFLAGS+=-DSAREF_SUPPORTED
endif

ifeq ($(USE_KLIPS),true)
CFLAGS+= -DKLIPS
endif

LIBS?=${PROGLIBS} ${LSWLOGLIB} ${LIBRESWANLIB} ${CRYPTOLIBS} 

CFLAGS+=-DIPSEC_CONFDIR=\"$(FINALCONFDIR)\"
CFLAGS+=-DIPSEC_CONFDDIR=\"$(FINALCONFDDIR)\"
CFLAGS+=-DIPSEC_CONF=\"$(FINALCONFFILE)\"
CFLAGS+=-DIPSEC_VARDIR=\"$(FINALVARDIR)\"

CFLAGS+= ${WERROR}

ifneq ($(LD_LIBRARY_PATH),)
LDFLAGS+=-L$(LD_LIBRARY_PATH)
endif

ifeq ($(USE_DMALLOC),true)
CFLAGS+=-DDMALLOC
LIBS+= -ldmalloc
endif

MANDIR8=$(MANTREE)/man8
MANDIR5=$(MANTREE)/man5

ifndef PROGRAMDIR
PROGRAMDIR=${LIBEXECDIR}
endif

ifndef MANPROGPREFIX
MANPROGPREFIX=ipsec_
endif

ifndef CONFDSUBDIR
CONFDSUBDIR=.
endif

ifndef SOURCEDIR
SOURCEDIR=${PROGRAM}
endif

# the list of stuff to be built for "make programs"
MANDEFAULTLIST=$(addsuffix .8, $(PROGRAM))
MANLIST=$(MANDEFAULTLIST) $(EXTRA8MAN) $(EXTRA5MAN) $(EXTRA5PROC) 
CONFIGLIST=$(CONFFILES) $(CONFDFILES)
PROGRAMSLIST=${PROGRAM} $(MANLIST) $(CONFIGLIST)

# we can't do tests with two conditions, but we can concatenate the strings
SHOULDWERESTART=${USE_OBJDIR}$(wildcard ../Makefile.program)
ifeq ($(SHOULDWERESTART),true../Makefile.program)
all programs config man clean install:
	cd ${LIBRESWANSRCDIR} && cd ${OBJDIRTOP}/programs/${SOURCEDIR} && $(MAKE) $@
else
all: $(PROGRAMSLIST)
programs: all
man: $(MANLIST)
config: $(CONFIGLIST)
clean:	cleanall

install: doinstall

endif

ifneq ($(PROGRAM),check)
check: $(PROGRAM)
endif


ifneq ($(NOINSTALL),true)

doinstall:: $(PROGRAMLIST) $(LIBFILES)
	@mkdir -p $(PROGRAMDIR) $(MANDIR8) $(MANDIR5) $(LIBDIR) $(CONFDIR) $(CONFDDIR) $(CONFDDIR)/$(CONFDSUBDIR) $(EXAMPLECONFDIR)
	@if [ -n "$(PROGRAM)" ]; then $(INSTALL) $(INSTBINFLAGS) $(PROGRAM) $(PROGRAMDIR); fi
	@$(foreach f, $(addsuffix .8, $(PROGRAM)), \
		g=`if [ -r $f ]; then echo $f; else echo ${srcdir}/$f; fi`; \
		$(INSTALL) $(INSTMANFLAGS) $$g $(MANDIR8)/$(MANPROGPREFIX)$f || exit 1; \
	)
	@$(foreach f, $(EXTRA8MAN), \
		g=`if [ -r $f ]; then echo $f; else echo ${srcdir}/$f; fi`; \
		$(INSTALL) $(INSTMANFLAGS) $$g $(MANDIR8)/ipsec_$f || exit 1; \
	)
	@$(foreach f, $(EXTRA5MAN), \
		g=`if [ -r $f ]; then echo $f; else echo ${srcdir}/$f; fi`; \
		$(INSTALL) $(INSTMANFLAGS) $$g $(MANDIR5)/$f || exit 1 ;\
	)
	@$(foreach f, $(EXTRA5PROC), \
		g=`if [ -r $f ]; then echo $f; else echo ${srcdir}/$f; fi`; \
		$(INSTALL) $(INSTMANFLAGS) $$g $(MANDIR5)/ipsec_$f || exit 1 ;\
	)
	@$(foreach f, $(LIBFILES), \
		g=`if [ -r $f ]; then echo $f; else echo ${srcdir}/$f; fi`; \
		$(INSTALL) $(INSTCONFFLAGS) $$g $(LIBDIR)/$f || exit 1 ;\
	)
	@$(foreach f, $(CONFFILES), \
		g=`if [ -r $f ]; then echo $f; else echo ${srcdir}/$f; fi`; \
		if [ ! -f $(CONFDIR)/$f ]; then $(INSTALL) $(INSTCONFFLAGS) $$g $(CONFDIR)/$f || exit 1; fi;\
		$(INSTALL) $(INSTCONFFLAGS) $$g $(EXAMPLECONFDIR)/$f-sample || exit 1; \
	)
	@$(foreach f, $(EXCONFFILES), \
		g=`if [ -r $f ]; then echo $f; else echo ${srcdir}/$f; fi`; \
		$(INSTALL) $(INSTCONFFLAGS) $$g $(EXAMPLECONFDIR)/$f-sample || exit 1; \
	)
	@$(foreach f, $(CONFDFILES), \
		g=`if [ -r $f ]; then echo $f; else echo ${srcdir}/$f; fi`; \
		if [ ! -f $(CONFDDIR)/$(CONFDSUBDIR)/$f ]; then $(INSTALL) $(INSTCONFFLAGS) $$g $(CONFDDIR)/$(CONFDSUBDIR)/$f || exit 1; fi;\
	)

install_file_list::
	@if [ -n "$(PROGRAM)" ]; then echo $(PROGRAMDIR)/$(PROGRAM); fi
	@$(foreach f, $(addsuffix .8, $(PROGRAM)), \
		echo $(MANDIR8)/${MANPROGPREFIX}$f; \
	)
	@$(foreach f, $(EXTRA8MAN), \
		echo $(MANDIR8)/ipsec_$f; \
	)
	@$(foreach f, $(EXTRA5MAN), \
		echo $(MANDIR5)/$f;\
	)
	@$(foreach f, $(EXTRA5PROC), \
		echo $(MANDIR5)/ipsec_$f; \
	)
	@$(foreach f, $(LIBFILES), \
		echo $(LIBDIR)/$f;\
	)
	@$(foreach f, $(CONFFILES), \
		echo $(CONFDIR)/$f;\
		echo $(EXAMPLECONFDIR)/$f-sample;\
	)
	@$(foreach f, $(EXCONFFILES), \
		echo $(EXAMPLECONFDIR)/$f-sample;\
	)
	@$(foreach f, $(CONFDFILES), \
		echo $(CONFDDIR)/${CONFDSUBDIR}/$f;\
	)

endif

# set values for implicit rules.
LOADLIBS=${OBJS} 

LDLIBS=${LIBS} ${USERLINK} ${LIBS} ${EXTRALIBS} -lgmp ${NSSLIBS}


%: %.o $(OBJS) ${LIBS}
	$(CC) $(CFLAGS) -o $@ $@.o ${OBJS} $(LDFLAGS) $(LDLIBS) $(USERLINK) 

# cancel direct version
%: %.c

# cancel direct version
%: %.c

%.o: ${srcdir}%.c
	${CC} -c ${CFLAGS} $<

include ${LIBRESWANSRCDIR}/programs/Makefile.manpages

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

%: ${srcdir}%.in ${LIBRESWANSRCDIR}/Makefile.inc ${LIBRESWANSRCDIR}/Makefile.ver ${LIBRESWANSRCDIR}/Makefile.top
	@echo  'IN' $< '->' $@
	${TRANSFORM_VARIABLES} < $< > $@
	@if [ -x $< ]; then chmod +x $@; fi
	@if [ "${PROGRAM}.in" = $< ]; then chmod +x $@; fi

%: ${srcdir}%.pl ${LIBRESWANSRCDIR}/Makefile.inc ${LIBRESWANSRCDIR}/Makefile.ver
	@echo  'PL' $< '->' $@
	@${TRANSFORM_VARIABLES} < $< > $@
	@if [ -x $< ]; then chmod +x $@; fi
	@if [ "${PROGRAM}.pl" = $< ]; then chmod +x $@; fi

distclean: clean

mostlyclean: clean

realclean: clean

cleanall::
ifneq ($(strip $(PROGRAM)),)
	@if [ -r ${srcdir}$(PROGRAM).in ]; then rm -f $(PROGRAM); fi
	@if [ -r ${srcdir}$(PROGRAM).pl ]; then rm -f $(PROGRAM); fi
	@if [ -r ${srcdir}$(PROGRAM).c ];  then rm -f $(PROGRAM); fi
	@if [ -n "$(OBJS)" ];     then rm -f $(PROGRAM); fi
endif
	@rm -f *.o

checkprograms:

LIBRESWANSRCDIRREL?=../..
