##
##  Makefile -- Build procedure for mod_layout Apache module
##

#   the used tools
APXS=apxs
APACHECTL=apachectl
CC=`apxs -q CC`
INC=-I`apxs -q INCLUDEDIR` `$(APXS) -q CFLAGS`  #-DLAYOUT_FILEOWNER_NAME
LD_SHLIB=`apxs -q LDFLAGS_SHLIB`
VERSION = 3.2
DISTNAME = mod_layout
DISTVNAME = $(DISTNAME)-$(VERSION)

SRC = mod_layout.c utility.c origin.c layout.c 
OBJ = mod_layout.o utility.o origin.o layout.o 

SHELL = /bin/sh
PERL = perl
NOOP = $(SHELL) -c true
RM_RF = rm -rf
SUFFIX = .gz
COMPRESS = gzip --best
TAR  = tar
TARFLAGS = cvf
PREOP = @$(NOOP)
POSTOP = @$(NOOP)
TO_UNIX = @$(NOOP)

#   additional defines, includes and libraries
# Apache itself won't compile with it this strict
#DEF = -Wall -DDEBUG -Wtraditional -Wshadow -Wid-clash-len -Wpointer-arith -Wcast-qual -Wcast-align -Wwrite-strings-Wconversion -Waggregate-return -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls -Wlong-long -Winline -Werror
#DEF = -Wall -DDEBUG 

#   the default target
all: mod_layout.so

#   compile the shared object file
mod_layout.so: $(OBJ)  Makefile
	$(APXS) -c $(LIB) $(OBJ)

mod_layout.o : mod_layout.c
	$(CC) $(INC) -c mod_layout.c $(DEF) $(LIB)

utility.o : utility.c
	$(CC) $(INC) -c utility.c $(DEF) $(LIB)

origin.o : origin.c
	$(CC) $(INC) -c origin.c $(DEF) $(LIB)

layout.o : layout.c
	$(CC) $(INC) -c layout.c $(DEF) $(LIB)

header.o : header.c
	$(CC) $(INC) -c header.c $(DEF) $(LIB)
#   install the shared object file into Apache 
install: all
	strip mod_layout.so
	$(APXS) -i -a -n 'layout' mod_layout.so

rpm: #all dist
	cp $(DISTVNAME).tar$(SUFFIX) /usr/src/redhat/SOURCES
	rpm -ba mod_layout.spec
	cp /usr/src/redhat/RPMS/i386/$(DISTVNAME)*.i386.rpm .

#   cleanup
clean:
	rm -f mod_layout.o mod_layout.so $(OBJ) *.gz *rpm

#   simple test
test: reload
	lynx http://localhost/

#   install and activate shared object by reloading Apache to
#   force a reload of the shared object file
reload: install stop start

#   the general Apache start/restart/stop
#   procedures
start:
	$(APACHECTL) start
restart:
	$(APACHECTL) restart
stop:
	$(APACHECTL) stop

dist: version all $(DISTVNAME).tar$(SUFFIX) rpm

version:
	echo $(VERSION) > VERSION
	echo "#define VERSION \"$(VERSION)\"" > version.h
	cat mod_layout.spec | $(PERL) -e 'while (<STDIN>) { push @file, $$_} open(FILE, ">mod_layout.spec"); for(@file) { if (/^Version/) { print FILE "Version: ${VERSION}\n" } else { print FILE $$_}}'

$(DISTVNAME).tar$(SUFFIX) : distdir
	$(PREOP)
	$(TO_UNIX)
	$(TAR) $(TARFLAGS) $(DISTVNAME).tar $(DISTVNAME)
	$(RM_RF) $(DISTVNAME)
	$(COMPRESS) $(DISTVNAME).tar
	$(POSTOP)

distdir :
	$(RM_RF) $(DISTVNAME)
	$(PERL) -MExtUtils::Manifest=manicopy,maniread \
	-e "manicopy(maniread(),'$(DISTVNAME)', '$(DIST_CP)');"

push :
	if [ -F *.gz ] ; then \
		rm *.gz; \
	fi
	make dist
	scp $(DISTVNAME).tar$(SUFFIX) root@www.tangent.org:/usr/local/slash/site/www.tangent.org/htdocs/download
	scp $(DISTVNAME).tar$(SUFFIX) root@ftp.tangent.org:/var/ftp/pub/apache
	scp $(DISTVNAME)*.i386.rpm root@www.tangent.org:/usr/local/slash/site/www.tangent.org/htdocs/download
	scp $(DISTVNAME)*i386.rpm root@ftp.tangent.org:/var/ftp/pub/apache
	scp faq.html root@www.tangent.org:/usr/local/slash/site/www.tangent.org/htdocs/faqs/mod_layout.html
	scp directives/*.html root@www.tangent.org:/usr/local/slash/site/www.tangent.org/htdocs/faqs/directives/
