# things that the GNU standards document suggests all makefiles
# should have.
SHELL=/bin/sh
VER=2.10.5
.SUFFIXES:
.SUFFIXES: .c .o .pl .pm .pod .html .man .wml .1 .txt

prefix = @prefix@
exec_prefix = @exec_prefix@
bindir = @bindir@
libdir = @libdir@
mandir = @mandir@
datadir = @datadir@

top_srcdir = @top_srcdir@

# Where is perl 5 on this machine
PERL = @PERL@
CC = @CC@
CFLAGS = @CFLAGS@ @DEFS@
CPPFLAGS = @CPPFLAGS@
LDFLAGS = @LDFLAGS@
GDFORM_EXT = @GDFORM_EXT@
LD_RUN_PATH = @LD_RUN_PATH@
LIBS = @LIBS@

INSTALL = @INSTALL@
MKINSTALLDIRS = $(SHELL) $(top_srcdir)/mkinstalldirs

all:	 bin/rateup subst

bin/rateup: bin/rateup.o
	LD_RUN_PATH=$(LD_RUN_PATH) $(CC) bin/rateup.o -o bin/rateup $(LDFLAGS)  $(LIBS) 

bin/rateup.o: $(top_srcdir)/src/rateup.c
	$(CC) $(CPPFLAGS) $(CFLAGS) -c $(top_srcdir)/src/rateup.c -o bin/rateup.o

subst:
	$(PERL) -0777 -p -i~ -e 's@^#!\s*/\S*perl@#! $(PERL)@' $(top_srcdir)/bin/cfgmaker $(top_srcdir)/bin/indexmaker $(top_srcdir)/bin/mrtg
	$(PERL) -0777 -p -i~ -e 's@GRAPHFMT="...";@GRAPHFMT="$(GDFORM_EXT)";@' $(top_srcdir)/bin/mrtg $(top_srcdir)/bin/indexmaker

clean:
	-rm bin/rateup
	-rm config.cache

install:
	$(MKINSTALLDIRS) $(bindir)
	for x in $(top_srcdir)/bin/mrtg $(top_srcdir)/bin/cfgmaker $(top_srcdir)/bin/indexmaker; do \
	  $(INSTALL) -m 755 $$x $(bindir); done
	for x in bin/rateup; do \
	  $(INSTALL) -m 755 $$x $(bindir); done
	$(MKINSTALLDIRS) $(libdir)/mrtg2/Pod
	for x in $(top_srcdir)/lib/mrtg2/*.pm; do \
	  $(INSTALL) -m 644 $$x $(libdir)/mrtg2; done
	for x in $(top_srcdir)/lib/mrtg2/Pod/*.pm; do \
	  $(INSTALL) -m 644 $$x $(libdir)/mrtg2/Pod; done
	$(MKINSTALLDIRS) $(datadir)/mrtg2/icons
	for x in $(top_srcdir)/images/*.gif $(top_srcdir)/images/*.png; do \
	  $(INSTALL) -m 644 $$x $(datadir)/mrtg2/icons; done
	$(MKINSTALLDIRS) $(datadir)/doc/mrtg2
	(cd $(top_srcdir); for x in COPYING COPYRIGHT README CHANGES THANKS doc/*.pod doc/*.html doc/*.wml doc/*.txt doc/*.png; do \
	  $(INSTALL) -m 644 $$x $(datadir)/doc/mrtg2; done)
	$(MKINSTALLDIRS) $(mandir)/man1
	for x in $(top_srcdir)/doc/*.1; do \
	  $(INSTALL) -m 644 $$x $(mandir)/man1; done

########### Distribution Tools

###
### Things you might NOT want to play with ... 
###

ARCHIVE = mrtg-$(VER)
DIRNAME = mrtg-$(VER)

POD = doc/faq.pod           doc/logfile.pod       doc/nt-guide.pod      doc/unix-guide.pod   \
      doc/forum.pod         doc/mibhelp.pod       doc/reference.pod     doc/webserver.pod    \
      doc/mrtg-rrd.pod      doc/squid.pod         doc/mrtg.pod       doc/contrib.pod \
      doc/cfgmaker.pod      doc/indexmaker.pod    doc/mrtglib.pod    doc/mrtg-ipv6.pod

MAN = $(POD:.pod=.1)
TXT = $(POD:.pod=.txt)
HTML = $(POD:.pod=.html) doc/index.html

getpod:
	-rm $(HTML) $(MAN) $(TXT)
	-rm doc/cfgmaker.pod; ln -s ../bin/cfgmaker doc/cfgmaker.pod
	-rm doc/indexmaker.pod;ln -s ../bin/indexmaker doc/indexmaker.pod
	-rm doc/mrtglib.pod;ln -s ../lib/mrtg2/MRTG_lib.pm doc/mrtglib.pod

.pod.1:
	pod2man --release=$(VER) --center=mrtg $<  > $@

.pod.html:
	( cd doc ; pod2html --infile=../$< --outfile=../$@ --noindex --htmlroot=. --podroot=. --podpath=. --title=$* )

.wml.html:
	wmk -f $<
	chmod u+x $@

.1.txt:
	@NROFF@ -man -Tascii $< > $@

man: $(MAN)

html: $(HTML)

txt: $(TXT)

versync:
	$(PERL) -i~ -p -e 's|VERSION\s*=\s*"\d+\.\d+\.\d+([a-z0-9]+)?"|VERSION = "$(VER)"|gi; s|mrtg-\d+\.\d+\.\d+([a-z0-9]+)?|mrtg-$(VER)|gi; s|MRTG\s\d+\.\d+\.\d+([a-z0-9]+)?|MRTG $(VER)|gi' ANNOUNCE bin/mrtg bin/cfgmaker bin/indexmaker lib/mrtg2/*.pm src/*.c doc/*.pod doc/*.wml doc/*.html
	cd translate && $(PERL) mergelocale.pl skeleton.pm0 *.pmd && cp locales_mrtg.pm ../lib/mrtg2

doc:    getpod versync man html txt
	(cd doc; lynx -dump index.html >index.txt)
	$(PERL) -p -e 's|"http://people.ee.ethz.ch/~oetiker/webtools/mrtg/([^"])|"$$1|g' doc/index.html >doc/www-index.html
	$(PERL) -p -e 's|"http://people.ee.ethz.ch/~oetiker/webtools/mrtg/([^"])|"$$1|g' doc/mrtg.html >doc/www-mrtg.html
	$(PERL) -i~ -p -e 's|\QHREF="././\E|HREF="|g' doc/*.html
	(cd doc; perl -i~ -p -e 's|<BODY>|<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#990200" VLINK="#006342"><P><IMG SRC="mrtg-l.png"><IMG SRC="mrtg-m.png"><IMG SRC="mrtg-r.gif"></P>|' *.html)


rateupexe:
	i386-mingw32msvc-gcc -Wall -Wno-format -Wpointer-arith -Wcast-align -Wmissing-declarations -Wnested-externs -O3 -mwindows -Ilibs-i386-mingw32msvc/zlib -Ilibs-i386-mingw32msvc/libpng -Ilibs-i386-mingw32msvc/gd -Llibs-i386-mingw32msvc/zlib -Llibs-i386-mingw32msvc/libpng -Llibs-i386-mingw32msvc/gd src/rateup.c -o bin/rateup.exe -lgd -lpng -lz
	i386-mingw32msvc-strip bin/rateup.exe

tar:	doc rateupexe
	autoconf
	gchmod -R u=rwX,go=rX .
	(cd .. ; ln -s AABM-mrtg $(DIRNAME))
	(cd .. ; sed -e "s/^/$(DIRNAME)\//" $(DIRNAME)/MANIFEST | gtar -czv --files-from=- -f $(DIRNAME)/archive/$(ARCHIVE).tar.gz --exclude='*~' --exclude="CVS")
	$(PERL) -0777 -pi -e 's@GRAPHFMT="...";@GRAPHFMT="png";@' bin/mrtg bin/indexmaker
	# build rateup for windows ...
	(cd .. ; touch $(DIRNAME)/contrib/a~;sed -e "s/^/$(DIRNAME)\//" $(DIRNAME)/MANIFEST | xargs zip -r $(DIRNAME)/archive/$(ARCHIVE).zip; zip $(DIRNAME)/archive/$(ARCHIVE).zip $(DIRNAME)/bin/rateup.exe; zip -d $(DIRNAME)/archive/$(ARCHIVE).zip '*~' 'CVS')
	-rm ../$(DIRNAME)

doc-dist: doc
	cp doc/*.html ${HOME}/public_html/webtools/mrtg/
	cp doc/www-index.html ${HOME}/public_html/webtools/mrtg/readme.html
	cp doc/www-mrtg.html ${HOME}/public_html/webtools/mrtg/mrtg.html

dist: doc-dist tar
	cp CHANGES archive/$(ARCHIVE).* ${HOME}/public_html/webtools/mrtg/pub/
	(cd ${HOME}/public_html/webtools/mrtg/pub; rm mrtg.tar.gz;ln -s $(ARCHIVE).tar.gz mrtg.tar.gz;gmd5sum $(ARCHIVE).tar.gz >$(ARCHIVE).tar.gz.md5;pgps  $(ARCHIVE).tar.gz.md5;chmod 644  $(ARCHIVE).tar.gz.md5*)

betadist: doc-dist tar
	cp CHANGES archive/$(ARCHIVE).* ${HOME}/public_html/webtools/mrtg/pub/beta
