all:		mmrp mmdiscover mmborder mmtodot 

include		depends.mk

.SUFFIXES:	.C .o .c

BACKUP =	*.[Ch] Makefile

CXX = g++

CXXFLAGS +=	-g
CXXFLAGS +=	-Wall
CXXFLAGS +=	-I. -Iinclude-fixes
#CXXFLAGS +=	-O2
CXXFLAGS +=   -D_GNU_SOURCE

# LDFLAGS  +=	-static -lpcap

define doit
	$(RM) $@
	$(CXX) $(CXXFLAGS) -o $@ $^ $(LDFLAGS)
     endef


# Dependencies
.PHONY:		depends
depends:	*.[Ch]
		$(CXX) -M $(CXXFLAGS) *.C > depends.mk

# Lose all unimportant cruft...
.PHONY:		clean
clean:
		$(RM) *.o *~ man/*~ \#*\#
		echo " " > depends.mk

# Make a backup tar file of current stuff...
.PHONY:		backup
backup:		$(BACKUP)
		@dir=`pwd` ; dir=`basename $$dir` ;     \
		file=`date +%y%m%d.%H%M`.tgz ;          \
		file="$$dir-$$file";                    \
		echo Making backup $$file ;             \
		tar -cvzphf ./$$file $^;		\
		if [ -d backup ];  then                 \
			mv $$file backup;               \
			echo Moved to backup directory; \
		fi;

srcdir  = .
distdir = mobilemesh-1.0

SCRIPTS = mmrpviz mmtunnel 
MANFILES = man/*.8
DISTFILES = *.[hC] Makefile man *.debug *.conf README INSTALL LICENSE FAQ depends.mk $(SCRIPTS) include-fixes InternetDrafts

ETCDIR  = /etc/mobilemesh
RUNDIR  = /var/run/mobilemesh
BINDIR  = /usr/bin
MANDIR  = /usr/man/man8

PROGS = mmdiscover mmrp mmborder mmtodot $(SCRIPTS)

.PHONY:	install
install:
	install -d $(ETCDIR) 
	install -d $(RUNDIR) 
	install -m 644 *.conf *.debug $(ETCDIR)
	install -m 550 $(PROGS) $(BINDIR)

.PHONY:	install-man
install-man:
	install -m 644 $(MANFILES) $(MANDIR) 

# Make a distribution
dist: clean distdir
	-chmod -R a+r $(distdir)
	tar chzf $(distdir).tar.gz $(distdir)
	-rm -rf $(distdir)
distdir: $(DISTFILES)
	-rm -rf $(distdir)
	mkdir $(distdir)
	-chmod 777 $(distdir)
	@for file in $(DISTFILES); do \
	  d=$(srcdir); \
	  if test -d $$d/$$file; then \
	    cp -pr $$d/$$file $(distdir)/$$file; \
	  else \
	    test -f $(distdir)/$$file \
	    || ln $$d/$$file $(distdir)/$$file 2> /dev/null \
	    || cp -p $$d/$$file $(distdir)/$$file || :; \
	  fi; \
	done

# Make pdfs of man pages
PDFS := $(addsuffix .pdf, $(basename $(wildcard $(MANFILES) ) ) )

pdf:	$(PDFS)

%.ps: %.8
	groff -mandoc $< > $@


%.pdf: %.ps
	gs -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=$@ $< quit.ps


# Make Internet Drafts from nroff source
IDS := $(addsuffix .txt, $(basename $(wildcard id/*.nroff ) ) )

id:   $(IDS)

%.txt: %.nroff
	  nroff -ms  $< | sed -f ./id/fix2.sh | ./id/pg -n5 > $@


# The executables 
mmrp:   	MmMain.o UtReport.o UtString.o UtDebug.o
		$(doit)

mmdiscover:   	LnMain.o UtReport.o UtString.o UtDebug.o
		$(doit)

mmborder:   	BoMain.o UtReport.o UtString.o UtDebug.o
		$(doit)

mmtodot:   	ToMain.o UtReport.o UtString.o UtDebug.o
		$(doit)

dbChild:   	dbChild.o UtReport.o UtString.o UtDebug.o
		$(doit)

dbEtherTap:   	dbEtherTap.o UtReport.o UtString.o UtDebug.o
		$(doit)

dbUnix:   	dbUnix.o UtReport.o UtString.o
		$(doit)


# Debug files tailored to each executable
.PHONY: flags
flags: mmborder-flags mmrp-flags mmdiscover-flags mmtodot-flags

.PHONY: mmborder-flags
mmborder-flags:   Bo*.[hC]
		perl UtDebug.pl Bo*.[hC] > mmborder.debug

.PHONY: mmrp-flags
mmrp-flags:     Mm*[hC]
		perl UtDebug.pl Mm*.[hC] > mmrp.debug

.PHONY: mmdiscover-flags
mmdiscover-flags: Ln*[hC]
		perl UtDebug.pl Ln*.[hC] > mmdiscover.debug

.PHONY: mmtodot-flags
mmtodot-flags: Ln*[hC]
		perl UtDebug.pl To*.[hC] > mmtodot.debug







