##
##  Makefile -- Build procedure for mod_layout Apache module
##
builddir=$(shell pwd)

#   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 = 5.0
DISTNAME = mod_layout
DISTVNAME = $(DISTNAME)-$(VERSION)

SRC = mod_layout.c utility.c layout.c
OBJ = ${SRC:.c=.o}
SOBJ = ${SRC:.c=.lo}

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
mod_layout.so: $(SRC)
	$(APXS) -c $(INCLUDES) $(LIBS) $(SRC)

mod_layout.o : mod_layout.c
	$(APXS) -c mod_layout.c 

utility.o : utility.c
	$(APXS) -c utility.c

origin.o : origin.c
	$(APXS) -c origin.c

layout.o : layout.c
	$(APXS) -c layout.c

header.o : header.c
	$(APXS) -c header.c
#   install the shared object file into Apache 
install: 
	$(APXS) -iac $(INCLUDES) $(LIBS) $(SRC)
	@echo "+--------------------------------------------------------+"; \
	echo "| All done.                                              |"; \
	echo "|                                                        |"; \
	echo "| Thanks for installing mod_layout.                      |"; \
	echo "+--------------------------------------------------------+"; \

rpm: dist
	cp $(DISTVNAME).tar$(SUFFIX) /usr/src/redhat/SOURCES
	rpmbuild -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

#   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/
