
ifdef AWARE_HOME 
include $(AWARE_HOME)/config/Makefile.config
endif


# for archiving
TARROOT=aware-$(AW_VERSION)
SRC_TARFILE=$(TARROOT).src.tgz
BIN_TARFILE=$(TARROOT).bin.tgz

all: env version copyright clean dirs build epilogue

# create required directory structure for generated files
dirs::
	@ if test ! -d $(BINDIR) ; then mkdir -p $(BINDIR) ; fi
	@ if test ! -d $(LIBDIR) ; then mkdir -p $(LIBDIR) ; fi
	@ if test ! -d $(INCLUDEDIR) ; then mkdir -p $(INCLUDEDIR) ; fi
	@ if test ! -d $(DOCDIR) ; then mkdir -p $(DOCDIR) ; fi

config::
	./configure

cleanconfig::
	(cd config; make clean)

clean::
	@ echo ; echo Cleaning ; echo 
	$(RM) $(RMFILES) 
	$(RM) -rf $(BINDIR) $(LIBDIR) $(INCLUDEDIR)
	(cd $(SRCDIR) ; make clean )
	(cd $(EXAMPLEDIR) ; make clean )
	@ echo ; echo Done Cleaning ; echo 

squeakyclean: clean cleanconfig 
	$(RM) -rf $(DOCDIR) bin lib

env::
# check if AWARE_HOME is bound
ifndef AWARE_HOME 
	@echo "Environment variable AWARE_HOME not defined!"
	@echo "Please read INSTALL for instructions."
	@false
endif
# check if MACHTYPE is bound
ifndef MACHTYPE 
	@echo "Environment variable MACHTYPE not defined!"
	@echo "Please read INSTALL for instructions."
	@false
endif
# CPU is indicator that configure was run
ifndef CPU
	@echo "You have not run ./configure"
	@echo "Please run ./configure"
	@false
endif


version::
	@ echo ; echo "Aware Version $(AW_VERSION)" ; echo 

copyright::
	@ echo ; echo "$(AW_COPYRIGHT)" ; echo 

epilogue::
	@ echo ; echo 'Please read the COPYING file' ; echo 
	@ echo ; echo 'Thank you for using Aware' ; echo 

build::
	@ echo ; echo Building ; echo 
	(cd $(SRCDIR) ; make install )
	(cd $(EXAMPLEDIR) ; make install )
	@ echo ; echo Done Building for $(MACHTYPE); echo 

install::
	@ echo ; echo Installing ; echo 
	@ ( \
		umask 0022 ; \
		default_idir="/var/awared" ; \
		echo "Install into what directory? ($$default_idir): " ; \
		read idir ; \
		if [ -z "$$idir" ] ; then \
			idir=$$default_idir ; \
		fi ; \
		if [ -d $$idir ] ; then \
			echo $$idir 'exists! Delete? [y,n](y):' ; \
			read y_n ; \
			if [ "$$y_n" = "n" ] ; then \
				echo 'Install aborted' ; \
				exit ; \
			else \
				/bin/rm -rf $$idir ; \
			fi ; \
		fi; \
		if [ ! -d $$idir/bin ] ; then mkdir -p $$idir/bin ; fi ; \
		cp -rf $(BINDIR) $$idir/bin ; \
		if [ ! -d $$idir/lib ] ; then mkdir -p $$idir/lib ; fi ; \
		cp -rf $(LIBDIR) $$idir/lib ; \
		echo "Install documentation and examples? [y,n](n): " ; \
		read y_n ; \
		if [ "$$y_n" = "y" ] ; then \
			cp -rf $(DOCDIR) $$idir ; \
			cp -rf $(EXAMPLEDIR) $$idir ; \
		fi; \
		\
		echo 'Making awared suid root to allow: raw sockets, monitor all files' ; echo ;\
		chown root $$idir/bin/*/discover ; chmod u+s  $$idir/bin/*/awared ; \
		echo 'Making discover suid root to allow: raw sockets' ; echo ;\
		chown root $$idir/bin/*/discover ; chmod u+s  $$idir/bin/*/discover ; \
                if test ! -d /usr/local/man/man8 ; then mkdir -p /usr/local/man/man8 ; fi ; \
		cp -rf $(DOCDIR)/man/* /usr/local/man/man8 ; \
		master='' ; \
		echo "Install aware daemon as master (answer yes if you don't know)? [y,n](y): " ; \
		read y_n ; \
		if [ "$$y_n" = "n" ] ; then \
			echo 'Installing aware daemon as slave' ; \
			echo 'Please input the name/ip of master: ' ; \
			read master ; \
			hostname=`hostname`; \
			echo "Please input the name of slave wire file (defaultagent.wire): " ; \
			read slavewire ; \
			if [ -z "$$slavewire" ] ; \
			then \
				slavewire="defaultagent.wire" ; \
			fi ; \
		else \
			echo 'Installing aware daemon as master' ; \
	                if test ! -d $$idir/slaves ; then mkdir -p $$idir/slaves ; fi ; \
			echo '     Copying local example as slave defaultagent.wire' ; \
			echo '     NOTE: You should login as administrator/administrator and change the password!' ; \
			echo '' ; \
			cp $(AWARE_HOME)/examples/local/example.wire $$idir/slaves/defaultagent.wire ; \
		fi ; \
		cp $(BINDIR)/awared.sh $(BINDIR)/awared.sh.orig ; \
		cat $(BINDIR)/awared.sh.orig | \
			sed "s|AWARE_HOME=\(.*\)|AWARE_HOME=$$idir|" | \
			sed "s|MACHTYPE=\(.*\)|MACHTYPE=$(MACHTYPE)|" | \
			sed "s|master=\(.*\)|master=$$master|" | \
			sed "s|slave_wirefile=\(.*\)|slave_wirefile=$$slavewire|"  \
			> $$idir/bin/$(MACHTYPE)/awared.sh ;\
		\
		awared_start=$$idir/bin/$(MACHTYPE)/awared.sh ;\
		echo 'Do you want to have this run at system boot? [y,n](y): '; \
		read y_n ; \
		if [ -z "$$y_n" -o "$$y_n" == "y" ]; \
		then \
			 if [ -d /etc/rc.d/init.d ] ; \
			 then \
			  echo 'Updating /etc/rc.d/'; \
			  cp $$awared_start /etc/rc.d/init.d ; \
			  (cd /etc/rc.d/; \
			   for d in 1 2 3 4 5 6 ; \
			   do \
			    (cd rc$$d.d ; ln -f -s ../init.d/awared.sh K00awared); \
			   done ; \
			   for d in 3 4 5 ; \
			   do \
			    (cd rc$$d.d ; ln -f -s ../init.d/awared.sh S99awared); \
			   done ; \
			  ); \
			 else \
			  echo 'Unable to find expected rc directories.' ; \
			  echo "You should copy $$awared_start to your systems boot script area" ; \
			  echo ; \
			 fi ; \
		fi ;\
		\
		echo 'Do you want to start awared now? [y,n](y): '; \
		read y_n ; \
		if [ -z "$$y_n" -o "$$y_n" == "y" ]; \
		then \
			$$awared_start start ; \
		fi ; \
	)
	@ echo ; echo Done Installing ; echo 

# Various documentation generated
doc: doxygen

# Code documenation in HTML
doxygen::
	@ echo ; echo Running doxygen ; echo 
	$(DOXYGEN) 
	@ echo ; echo Done running Doxygen ; echo 

# Extract all the files for editing/compiling/installing
bk::
	@ bk -U sfiles | bk edit - ; bk unedit ChangeSet


# Tar up src, include generated files (e.g., dox files)
tar::
	make bk
	make squeakyclean
	make config
	make all 
	make doc 
	make clean 
	make cleanconfig
	( \
	echo Version: $(AW_VERSION) ; \
	echo Date: `date` ; \
	echo PackagedBy: `whoami` ; \
	) > PACKAGE_INFO
	cd .. ; tar  zcvf $(TARROOT)/$(SRC_TARFILE) \
		--exclude BitKeeper \
		--exclude SCCS \
		--exclude $(TARROOT)/$(BIN_TARFILE) --exclude $(TARROOT)/$(SRC_TARFILE) \
		$(TARROOT)
	rm PACKAGE_INFO
	md5sum < $(SRC_TARFILE) > $(SRC_TARFILE).md5









