# $Header: /home/amb/wwwoffle/RCS/Makefile 2.89 2001/05/21 18:44:37 amb Exp $
#
# WWWOFFLE - World Wide Web Offline Explorer - Version 2.6d.
#
# Program Makefile.
#
# Written by Andrew M. Bishop
#
# This file Copyright 1996,97,98,99,2000,01 Andrew M. Bishop
# It may be distributed under the GNU Public License, version 2, or
# any higher version.  See section COPYING of the GNU Public license
# for conditions under which this file may be redistributed.
#

########

# Default server.
LOCALHOST=localhost:8080

# For UNIX.
INSTDIR=/usr/local
SPOOLDIR=/var/spool/wwwoffle
CONFDIR=/var/spool/wwwoffle
MANDIR=$(INSTDIR)/man
DOCDIR=$(INSTDIR)/doc/wwwoffle

# For Cygwin (win32).
#INSTDIR=/wwwoffle
#SPOOLDIR=/wwwoffle/spool
#CONFDIR=/wwwoffle
#DOCDIR=$(INSTDIR)/doc

# For German language web pages and documentation.
#LANG=de

# For French language web pages and documentation.
#LANG=fr

# For Spanish language web pages and documentation.
#LANG=es

# For Russian language web pages and documentation.
#LANG=ru

# For Polish language web pages and documentation.
#LANG=pl

# For Italian language web pages and documentation.
#LANG=it

# For Dutch language web pages and documentation.
#LANG=nl

########

CC=gcc
CFLAGS=-O2 -Wall -g

# This is used in the FreeBSD port (http://www.freebsd.org/ports/).
#CFLAGS=-O2 -Wall

LD=gcc
LDFLAGS=-g

# For HP/UX this is a good idea.
#LDFLAGS=

# This is used in the FreeBSD port (http://www.freebsd.org/ports/).
#LDFLAGS=-s

# Optimal flex options for a very fast but large HTML parser.
LEX=flex -i -L -pp -B -F -8 -s

# Use this if you have flex but want a smaller but slower HTML parser.
#LEX=flex -i -L -pp -B

# Use this if you don't have flex.
#LEX=lex -i -L

########

# The option to use zlib for compression (may also require changes to INCLUDE and LIBRARY).
USE_ZLIB=1

# The option to not use zlib for compression.
#USE_ZLIB=0

# The option to use IPv6 sockets if required.
#USE_IPV6=1

# The option to use IPv6 sockets if required.
USE_IPV6=0

########

INCLUDE=
LIBRARY=-lz

# To compile without zlib
#LIBRARY=

# For zlib installed outside of the compiler path.
#INCLUDE=-I/opt/zlib/include
#LIBRARY=-L/opt/zlib/lib -lz

# For Solaris you need the following instead.
#LIBRARY=-lnsl -lsocket -lz

COMPILE=$(CC) -c $(CFLAGS)

LINK=$(LD) $(LDFLAGS)

INSTALL=install

# For Solaris you need the following instead.
#INSTALL=/usr/ucb/install

# For SGI you need the following
SHELL=/bin/sh

########

all : programs html

########

install       : install_binary \
		install_doc install_man \
		install_cache install_html \
		install_config

install-win32 : install_binary-win32 \
		install_doc \
		install_cache install_html \
		install_config \
		install_fixup-win32

########

clean :
	-rm -f wwwoffle.conf.install wwwoffle.conf.man.install
	-rm -f wwwoffle wwwoffled wwwoffle-tools convert-cache uncompress-cache
	-rm -f core *.o *~
	-rm -f html.c htmlmodify.c messages.c xml.c vrml.c
	cd testprogs && $(MAKE) clean

########

programs : wwwoffled wwwoffle wwwoffle-tools convert-cache uncompress-cache

########

test-programs : programs
	cd testprogs && $(MAKE)

########

DOC_PARSERS=document.o \
	    html.o xml.o vrml.o javaclass.o

####

WWWOFFLE_OBJ=wwwoffle.o \
	     http.o ftp.o finger.o ssl.o \
	     refresh.o messages.o parse.o spool.o \
	     $(DOC_PARSERS)\
	     configfile.o config.o errors.o io.o misc.o proto.o sockets.o md5.o

wwwoffle : $(WWWOFFLE_OBJ)
	$(LINK) $(WWWOFFLE_OBJ) -o $@ $(LIBRARY)

####

WWWOFFLED_OBJ=wwwoffled.o wwwoffles.o \
	      http.o ftp.o finger.o ssl.o \
	      $(DOC_PARSERS) \
	      gifmodify.o htmlmodify.o \
	      connect.o control.o configedit.o search.o index.o messages.o monitor.o parse.o purge.o refresh.o spool.o \
	      configfile.o config.o errors.o io.o misc.o proto.o sockets.o md5.o

wwwoffled : $(WWWOFFLED_OBJ)
	$(LINK) $(WWWOFFLED_OBJ) -o $@ $(LIBRARY)

####

WWWOFFLE_TOOLS_OBJ=wwwoffle-tools.o \
	           http.o ftp.o finger.o ssl.o \
	           spool.o \
		   messages.o parse.o \
	           configfile.o config.o errors.o io.o misc.o proto.o sockets.o md5.o

wwwoffle-tools : $(WWWOFFLE_TOOLS_OBJ)
	$(LINK) $(WWWOFFLE_TOOLS_OBJ) -o $@ $(LIBRARY)

####

CONVERT_OBJ=convert-cache.o \
	    http.o ftp.o finger.o ssl.o \
	    messages.o parse.o \
	    configfile.o config.o errors.o io.o misc.o proto.o sockets.o spool.o md5.o

convert-cache : $(CONVERT_OBJ)
	$(LINK) $(CONVERT_OBJ) -o $@ $(LIBRARY)

####

UNCOMPRESS_OBJ=uncompress-cache.o \
	       http.o ftp.o finger.o ssl.o \
	       messages.o parse.o \
	       configfile.o config.o errors.o io.o misc.o proto.o sockets.o spool.o md5.o

uncompress-cache : $(UNCOMPRESS_OBJ)
	$(LINK) $(UNCOMPRESS_OBJ) -o $@ $(LIBRARY)

####

%.o:%.c
	$(COMPILE) $< -o $@ $(INCLUDE) -DUSE_ZLIB=$(USE_ZLIB) -DUSE_IPV6=$(USE_IPV6)

wwwoffle.o   : wwwoffle.c   wwwoffle.h misc.h config.h errors.h sockets.h         document.h version.h
wwwoffled.o  : wwwoffled.c  wwwoffle.h misc.h config.h errors.h sockets.h                    version.h
wwwoffles.o  : wwwoffles.c  wwwoffle.h misc.h config.h errors.h sockets.h proto.h document.h

wwwoffle-tools.o : wwwoffle-tools.c wwwoffle.h misc.h config.h errors.h
	$(COMPILE) wwwoffle-tools.c -o $@ $(INCLUDE) -DSPOOL_DIR=\"$(SPOOLDIR)\" -DUSE_ZLIB=$(USE_ZLIB) -DUSE_IPV6=$(USE_IPV6)

convert-cache.o : convert-cache.c  wwwoffle.h misc.h config.h errors.h

uncompress-cache.o : uncompress-cache.c  wwwoffle.h misc.h config.h errors.h

http.o       : http.c       wwwoffle.h misc.h config.h errors.h sockets.h proto.h
ftp.o        : ftp.c        wwwoffle.h misc.h config.h errors.h sockets.h proto.h
finger.o     : finger.c     wwwoffle.h misc.h config.h errors.h sockets.h proto.h
ssl.o        : ssl.c        wwwoffle.h misc.h config.h errors.h sockets.h proto.h

document.o   : document.c   wwwoffle.h misc.h config.h errors.h           proto.h document.h
html.o       : html.c	    wwwoffle.h misc.h config.h errors.h                   document.h
xml.o        : xml.c	    wwwoffle.h misc.h          errors.h                   document.h
vrml.o       : vrml.c	    wwwoffle.h misc.h          errors.h                   document.h
javaclass.o  : javaclass.c  wwwoffle.h misc.h          errors.h                   document.h

gifmodify.o  : gifmodify.c  wwwoffle.h misc.h          errors.h                   document.h
htmlmodify.o : htmlmodify.c wwwoffle.h misc.h config.h errors.h                   document.h

configedit.o : configedit.c wwwoffle.h misc.h config.h errors.h
connect.o    : connect.c    wwwoffle.h misc.h config.h errors.h sockets.h
control.o    : control.c    wwwoffle.h misc.h config.h errors.h sockets.h
search.o     : search.c	    wwwoffle.h misc.h config.h errors.h           proto.h
index.o      : index.c      wwwoffle.h misc.h config.h errors.h           proto.h
messages.o   : messages.c   wwwoffle.h misc.h config.h errors.h                              version.h
monitor.o    : monitor.c    wwwoffle.h misc.h config.h
parse.o      : parse.c      wwwoffle.h misc.h config.h errors.h           proto.h
purge.o      : purge.c      wwwoffle.h misc.h config.h errors.h           proto.h
refresh.o    : refresh.c    wwwoffle.h misc.h config.h errors.h sockets.h         document.h
spool.o      : spool.c      wwwoffle.h misc.h config.h errors.h

configfile.o : configfile.c wwwoffle.h misc.h          errors.h                   configfile.h
config.o     : config.c     wwwoffle.h misc.h config.h errors.h sockets.h proto.h configfile.h
	$(COMPILE) config.c -o $@ $(INCLUDE) -DSPOOL_DIR=\"$(SPOOLDIR)\" -DCONF_DIR=\"$(CONFDIR)\" -DUSE_ZLIB=$(USE_ZLIB) -DUSE_IPV6=$(USE_IPV6)
errors.o     : errors.c                misc.h config.h errors.h
io.o         : io.c         wwwoffle.h misc.h          errors.h
misc.o       : misc.c                  misc.h config.h errors.h           proto.h md5.h
proto.o      : proto.c                 misc.h                             proto.h
sockets.o    : sockets.c                               errors.h sockets.h \
               sockets4.c sockets6.c

md5.o        : md5.c md5.h

####

html.c : html.l
	$(LEX) -Phtml_yy html.l
	@mv lex.html_yy.c html.c

htmlmodify.c : htmlmodify.l
	$(LEX) -Phtmlmodify_yy htmlmodify.l
	@mv lex.htmlmodify_yy.c htmlmodify.c

xml.c : xml.l
	$(LEX) -Pxml_yy xml.l
	@mv lex.xml_yy.c xml.c

vrml.c : vrml.l
	$(LEX) -Pvrml_yy vrml.l
	@mv lex.vrml_yy.c vrml.c

messages.c : messages.l
	$(LEX) -Pmsg_yy messages.l
	@mv lex.msg_yy.c messages.c

########

html : html/FAQ.html \
       html/README.CONF.html
	@true

html/FAQ.html : FAQ FAQ-html.pl
	-perl FAQ-html.pl < FAQ > FAQ.html
	-[ -f FAQ.html -a -s FAQ.html ] && mv FAQ.html html/FAQ.html
	[ ! -f FAQ.html ] || rm FAQ.html

html/README.CONF.html : README.CONF README-CONF-html.pl
	-perl README-CONF-html.pl < README.CONF > README.CONF.html
	-[ -f README.CONF.html -a -s README.CONF.html ] && mv README.CONF.html html/README.CONF.html
	[ ! -f README.CONF.html ] || rm README.CONF.html

########

install_binary : programs
	[ -x $(INSTDIR)/bin ] || $(INSTALL) -d -m 755 $(INSTDIR)/bin
	$(INSTALL) -c -m 755 wwwoffle $(INSTDIR)/bin
	$(INSTALL) -c -m 755 wwwoffle-tools $(INSTDIR)/bin
	ln -sf wwwoffle-tools $(INSTDIR)/bin/wwwoffle-ls
	ln -sf wwwoffle-tools $(INSTDIR)/bin/wwwoffle-mv
	ln -sf wwwoffle-tools $(INSTDIR)/bin/wwwoffle-rm
	ln -sf wwwoffle-tools $(INSTDIR)/bin/wwwoffle-read
	ln -sf wwwoffle-tools $(INSTDIR)/bin/wwwoffle-write
	[ -x $(INSTDIR)/sbin ] || $(INSTALL) -d -m 755 $(INSTDIR)/sbin
	$(INSTALL) -c -m 755 wwwoffled $(INSTDIR)/sbin

install_binary-win32 : programs
	[ -x $(INSTDIR)/bin ] || $(INSTALL) -d -m 755 $(INSTDIR)/bin
	$(INSTALL) -c -m 755 wwwoffle.exe $(INSTDIR)/bin
	$(INSTALL) -c -m 755 wwwoffle-tools.exe $(INSTDIR)/bin
	$(INSTALL) -c -m 755 wwwoffle-tools.exe $(INSTDIR)/bin/wwwoffle-ls.exe
	$(INSTALL) -c -m 755 wwwoffle-tools.exe $(INSTDIR)/bin/wwwoffle-mv.exe
	$(INSTALL) -c -m 755 wwwoffle-tools.exe $(INSTDIR)/bin/wwwoffle-rm.exe
	$(INSTALL) -c -m 755 wwwoffle-tools.exe $(INSTDIR)/bin/wwwoffle-read.exe
	$(INSTALL) -c -m 755 wwwoffle-tools.exe $(INSTDIR)/bin/wwwoffle-write.exe
	$(INSTALL) -c -m 755 wwwoffled.exe $(INSTDIR)/bin
	$(INSTALL) -c -m 755 upgrade-config.pl $(INSTDIR)/bin
	@[ -f cygwin1.dll -o -f $(INSTDIR)/bin/cygwin1.dll ] || \
	 (echo "WWWOFFLE: " ;\
	  echo "WWWOFFLE: You need to copy the cygwin1.dll into '$(INSTDIR)/bin'." ;\
	  echo "WWWOFFLE: " )
	[ ! -f cygwin1.dll ] || $(INSTALL) -c -m 755 cygwin1.dll $(INSTDIR)/bin

install_man :
	[ -x $(MANDIR) ] || $(INSTALL) -d -m 755 $(MANDIR)
	[ -x $(MANDIR)/man1 ] || $(INSTALL) -d -m 755 $(MANDIR)/man1
	$(INSTALL) -c -m 644 wwwoffle.man $(MANDIR)/man1/wwwoffle.1
	[ -x $(MANDIR)/man5 ] || $(INSTALL) -d -m 755 $(MANDIR)/man5
	sed -e 's%SPOOLDIR%$(SPOOLDIR)%' -e 's%CONFDIR%$(CONFDIR)%' < wwwoffle.conf.man > wwwoffle.conf.man.install
	$(INSTALL) -c -m 644 wwwoffle.conf.man.install $(MANDIR)/man5/wwwoffle.conf.5
	[ -x $(MANDIR)/man8 ] || $(INSTALL) -d -m 755 $(MANDIR)/man8
	$(INSTALL) -c -m 644 wwwoffled.man $(MANDIR)/man8/wwwoffled.8

install_doc :
	[ -x $(DOCDIR) ] || $(INSTALL) -d -m 755 $(DOCDIR)
	for file in CHANGES.CONF CONVERT COPYING FAQ INSTALL NEWS README README.* ; do \
	  $(INSTALL) -c -m 644 $$file $(DOCDIR)/$$file ;\
	done
	[ "x$(LANG)" = "x" ] || [ ! -d lang-$(LANG) ] || ( cd lang-$(LANG) && \
	  for file in CHANGES.CONF CONVERT COPYING FAQ INSTALL NEWS README README.* ; do \
	    [ ! -f $$file ] || $(INSTALL) -c -m 644 $$file $(DOCDIR)/$$file-$(LANG) ;\
	  done )

install_cache :
	@[ ! -d $(SPOOLDIR) ] || [ ! -d $(SPOOLDIR)/outgoing ] || [ -d $(SPOOLDIR)/prevtime1 ] || \
	 (echo "WWWOFFLE: " ;\
	  echo "WWWOFFLE: Your existing cache looks earlier than version 2.2, cannot continue." ;\
	  echo "WWWOFFLE: Read the file NEWS for details." ;\
	  echo "WWWOFFLE: " ;\
	  exit 1 )
	[ -x $(SPOOLDIR) ] || $(INSTALL) -d -m 750 $(SPOOLDIR)
	[ -x $(SPOOLDIR)/http ] || $(INSTALL) -d -m 750 $(SPOOLDIR)/http
	[ -x $(SPOOLDIR)/outgoing ] || $(INSTALL) -d -m 750 $(SPOOLDIR)/outgoing
	[ -x $(SPOOLDIR)/monitor ] || $(INSTALL) -d -m 750 $(SPOOLDIR)/monitor
	[ -x $(SPOOLDIR)/lasttime ] || $(INSTALL) -d -m 750 $(SPOOLDIR)/lasttime
	[ -x $(SPOOLDIR)/prevtime1 ] || $(INSTALL) -d -m 750 $(SPOOLDIR)/prevtime1
	for file in outgoing/* ; do \
	  $(INSTALL) -c -m 644 $$file $(SPOOLDIR)/outgoing ;\
	done

install_html : html
	@[ ! -x $(SPOOLDIR)/html.old ] || \
	 (echo "WWWOFFLE: " ;\
	  echo "WWWOFFLE: There is already a directory $(SPOOLDIR)/html.old." ;\
	  echo "WWWOFFLE: Remove it and re-run make." ;\
	  echo "WWWOFFLE: " ;\
	  exit 1 )
	[ ! -x $(SPOOLDIR)/html ] || mv $(SPOOLDIR)/html $(SPOOLDIR)/html.old
	[ -x $(SPOOLDIR)/html ] || mkdir $(SPOOLDIR)/html
	tar cf $(SPOOLDIR)/html.tar html && cd $(SPOOLDIR) && tar xpf html.tar && rm html.tar
	[ "x$(LANG)" = "x" ] || [ ! -d lang-$(LANG) ] || ( cd lang-$(LANG) && \
	  tar cf $(SPOOLDIR)/html.tar html && cd $(SPOOLDIR) && tar xpf html.tar && rm html.tar )
	cd $(SPOOLDIR)/html && ./fixup-install.sh $(SPOOLDIR) $(LOCALHOST) && rm ./fixup-install.sh
# Now fix the permissions that tar preserved, we needed to use 'tar xpf' to get round root's umask.
# These two will fail unless you are root, that is OK because the owner is already you.
	-chown -R 0 $(SPOOLDIR)/html > /dev/null 2>&1
	-chgrp -R 0 $(SPOOLDIR)/html > /dev/null 2>&1

install_config :
	sed -e 's%SPOOLDIR%$(SPOOLDIR)%' -e 's%CONFDIR%$(CONFDIR)%' < wwwoffle.conf > wwwoffle.conf.install
	[ -x $(CONFDIR) ] || $(INSTALL) -d -m 750 $(CONFDIR)
	@[ ! -f $(CONFDIR)/wwwoffle.conf ] || \
	 (echo "WWWOFFLE: " ;\
	  echo "WWWOFFLE: There is already a config file $(CONFDIR)/wwwoffle.conf." ;\
	  echo "WWWOFFLE: Run 'upgrade-config.pl $(CONFDIR)/wwwoffle.conf' to upgrade it." ;\
	  echo "WWWOFFLE: " )
	[  ! -f $(CONFDIR)/wwwoffle.conf ] || $(INSTALL) -c -m 640 wwwoffle.conf.install $(CONFDIR)/wwwoffle.conf.install
	[    -f $(CONFDIR)/wwwoffle.conf ] || $(INSTALL) -c -m 640 wwwoffle.conf.install $(CONFDIR)/wwwoffle.conf

install_fixup-win32:
	for file in $(DOCDIR)/* ; do \
	  [ -f $$file ] && awk '{print $$0 "\r";}' < $$file > $$file.txt && rm -f $$file ;\
	done
	rm -f $(SPOOLDIR)/html/index.html && ln $(SPOOLDIR)/html/Welcome.html $(SPOOLDIR)/html/index.html

########
