################################################################
# SWI-Prolog `HTTP' package
# Author:    Jan Wielemaker. J.Wielemaker@cs.vu.nl
# Copyright: LGPL (see COPYING or www.gnu.org
################################################################

PACKAGE=http
DOC=http
include @srcdir@/common.mk
CFLAGS+= -I.
.SUFFIXES: .txt

include ../Makefile.defs

LIBDIR=$(PLLIBDIR)/http
EXDIR=$(PKGEXDIR)/http

STREAMOBJ=	http_stream.o
JSONOBJ=	json.o
SOLIBS=		http_stream.@SO@ json.@SO@

all:		$(SOLIBS)

http_stream.@SO@:	$(STREAMOBJ)
		$(LD) $(LDSOFLAGS) -o $@ $(STREAMOBJ) @LIBS@ $(LIBPLSO)
json.@SO@:	$(JSONOBJ)
		$(LD) $(LDSOFLAGS) -o $@ $(JSONOBJ) @LIBS@ $(LIBPLSO)

http_stream.o:	$(srcdir)/http_error.c $(srcdir)/http_chunked.c $(srcdir)/cgi_stream.c $(srcdir)/stream_range.c

install:	$(LIBSRCPL) $(SOLIBS)
		mkdir -p $(DESTDIR)$(SOLIBDIR)
		for f in $(SOLIBS); do \
                  $(INSTALL_PROGRAM) $$f $(DESTDIR)$(SOLIBDIR); \
                done
		rm -f $(DESTDIR)$(PLBASE)/$(SOLIB)/$(INSTALL_PLARCH)/http_chunked.@SO@
		mkdir -p $(DESTDIR)$(LIBDIR)
		mkdir -p $(DESTDIR)$(LIBDIR)/web/icons
		mkdir -p $(DESTDIR)$(LIBDIR)/web/css
		$(INSTALL_DATA) $(LIBSRCPL) $(DESTDIR)$(LIBDIR)
		$(INSTALL_DATA) $(srcdir)/README $(DESTDIR)$(LIBDIR)/README$(TXTEXT)
		$(INSTALL_DATA) $(srcdir)/web/icons/*.png $(DESTDIR)$(LIBDIR)/web/icons
		$(INSTALL_DATA) $(srcdir)/web/css/*.css $(DESTDIR)$(LIBDIR)/web/css
		$(MKPKGINDEX)
		$(MKINDEX)

ln-install:	$(LIBPL)
		$(MAKE) INSTALL_DATA="../ln-install" INSTALL_PROGRAM="../ln-install" install

rpm-install:	install

pdf-install:	install-examples
		$(INSTALL_DATA) $(DOC).pdf $(DESTDIR)$(PKGDOC)

html-install:	install-examples
		$(INSTALL_DATA) $(DOC).html httpserver.gif $(DESTDIR)$(PKGDOC)

install-examples::
		mkdir -p $(DESTDIR)$(EXDIR)
		mkdir -p $(DESTDIR)$(EXDIR)/pwp
		(cd $(srcdir)/examples && $(INSTALL_DATA) $(EXAMPLES) $(DESTDIR)$(EXDIR))
		(cd $(srcdir)/examples/pwp && $(INSTALL_DATA) *.pwp *.pl $(DESTDIR)$(EXDIR)/pwp)
		(cd $(srcdir)/examples && $(INSTALL_PROGRAM) $(EXAMPLEEXE) $(DESTDIR)$(EXDIR))

uninstall::
		(cd $(LIBDIR) && rm -f $(LIBPL))
		$(PL) -f none -g make -t halt

check::
		$(PL) -q -f test_http.pl -g test_http,halt -t 'halt(1)'
		$(PL) -q -f test_json.pl -g test_json,halt -t 'halt(1)'
		$(PL) -q -f test_cgi_stream.pl -g true -t test_cgi_stream

################################################################
# Documentation
################################################################

TEXEXTRA=	post.tex json.tex httplog.tex httppath.tex htmlhead.tex \
		httpsession.tex httpdispatch.tex httpdirindex.tex httppwp.tex \
		httpopen.tex httphost.tex httpopenid.tex jswrite.tex \
		httpfiles.tex
$(TEX):		$(TEXEXTRA) figs/httpserver.eps figs/httpserver.pd

figs/httpserver.eps:
		mkdir -p figs
		cp $(srcdir)/figs/httpserver.eps figs

figs/httpserver.pd:
		mkdir -p figs
		cp $(srcdir)/figs/httpserver.pd figs

json.tex:	$(srcdir)/json.pl $(srcdir)/json_convert.pl $(srcdir)/http_json.pl

httplog.tex:	$(srcdir)/http_log.pl
		$(PLTOTEX) --subsection 'library(http/http_log)'

httphost.tex:	$(srcdir)/http_host.pl
		$(PLTOTEX) --subsection 'library(http/http_host)'

httppath.tex:	$(srcdir)/http_path.pl
		$(PLTOTEX) --subsection 'library(http/http_path)'

httpopen.tex:	$(srcdir)/http_open.pl
		$(PLTOTEX) --subsection 'library(http/http_open)'

httpfiles.tex:	$(srcdir)/http_files.pl
		$(PLTOTEX) --subsection 'library(http/http_files)'

htmlhead.tex:	$(srcdir)/html_head.pl
		$(PLTOTEX) --subsection 'library(http/html_head)'

httpsession.tex: $(srcdir)/http_session.pl
		$(PLTOTEX) --subsection 'library(http/http_session)'

httpcors.tex: $(srcdir)/http_cors.pl
		$(PLTOTEX) --subsection 'library(http/http_cors)'

httpdispatch.tex: $(srcdir)/http_dispatch.pl
		$(PLTOTEX) --subsection 'library(http/http_dispatch)'

httpopenid.tex:	$(srcdir)/http_openid.pl
		$(PLTOTEX) --subsection 'library(http/http_openid)'

httpdirindex.tex: $(srcdir)/http_dirindex.pl
		$(PLTOTEX) --subsection 'library(http/http_dirindex)'

httppwp.tex:	$(srcdir)/http_pwp.pl
		$(PLTOTEX) --subsection 'library(http/http_pwp)'

jswrite.tex:	$(srcdir)/js_write.pl
		$(PLTOTEX) --subsection 'library(http/js_write)'


################################################################
# Clean
################################################################

clean:
		rm -f $(STREAMOBJ) $(JSONOBJ)
		rm -f *~ *% config.log
		rm -f $(TEX) $(SUBTEX)
		$(RUNTEX) --clean $(DOC)
		rm -rf html

distclean:	clean
		rm -f $(SOLIBS) config.cache config.status config.h Makefile
