include ../../.config
TARGET=ssi
SUBDIR=lib core ext
BUILTIN_OBJS=$(addsuffix /built-in.o, $(SUBDIR))
TOPDIR=$(shell pwd)

#SOURCES:= firmware_check.o iprange.o libdb.o libssc.o public.o virtual_file.o
SOURCES:= iprange.o public.o virtual_file.o
ifeq ($(CONFIG_GPL_CGI), y)
SOURCES +=  GPL_public.o
endif
ifeq ($(CONFIG_MP_TFTP),y)
SOURCES += mp_method.o
endif

#export LDFLAGS += -Wall -lnvram
export CFLAGS += -I$(TOPDIR)/../include -Wall

#CFLAGS +=-I../include -Wall
#LDFLAGS += -L. -lssi

all: $(TARGET)

$(TARGET):$(SUBDIR) $(SOURCES)
	$(CC) $(LDFLAGS) -lnvram -lm $(BUILTIN_OBJS) $(SOURCES) -o $@ ../misc/libcgitool.a $(CGILIB)

.c.o:
	@$(CC) $(CFLAGS) -fPIC -c $*.c -o $*.o

$(SUBDIR):
	@$(MAKE) -C $@

clean:
ifeq ($(CONFIG_GPL_SDK),y)
ifeq ($(CONFIG_GPL_CGI), y)
	@echo ""
	@echo "CONFIG_GPL_SDK=y  and CONFIG_GPL_CGI=y so just delete  $(TARGET)"
	@echo ""
	@rm -f $(TARGET)
else #CONFIG_GPL_CGI
	@for i in $(SUBDIR); do make -C $$i $@; done
	@rm -f $(TARGET) *.o
endif
else #CONFIG_GPL_SDK
ifeq ($(GENERATING_GPL),y)
ifeq ($(CONFIG_GPL_CGI), y)
	@echo ""
	@echo "GENERATING_GPL=y and CONFIG_GPL_CGI=y so just delete  $(TARGET)"
	@echo ""
	@rm -f $(TARGET)
else #CONFIG_GPL_CGI
	@for i in $(SUBDIR); do make -C $$i $@; done
	@rm -f $(TARGET) *.o
endif
else #GENERATING_GPL
	@for i in $(SUBDIR); do make -C $$i $@; done
	@rm -f $(TARGET) *.o
endif
endif


install:
	#cp ssi $(INSTALLDIR)/bin
	#-$(STRIP) $(INSTALLDIR)/bin/ssi
	install -d $(INSTALLDIR)/www/cgi
	install -d $(INSTALLDIR)/https/cgi
	install -d $(INSTALLDIR)/www/tmp/cgi
	if [ -h $(INSTALLDIR)/www/cgi/ssi ]; then \
		install $(TARGET) $(INSTALLDIR)/www/cgi/$(TARGET).exe; \
	else \
		install $(TARGET) $(INSTALLDIR)/www/cgi; \
	fi
	ln -sf ../../cgi/$(TARGET) $(INSTALLDIR)/www/tmp/cgi/$(TARGET)
	ln -sf ../../www/cgi/$(TARGET) $(INSTALLDIR)/https/cgi/$(TARGET)
	$(STRIP) $(INSTALLDIR)/www/cgi/$(TARGET)
.PHONY: $(SUBDIR)
