# Fungimol - an extensible system for designing atomic-scale objects.
# Copyright (C) 2000 Tim Freeman
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
# 
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Library General Public License for more details.
# 
# You should have received a copy of the GNU Library General Public
# License along with this library in the file COPYING.txt; if not,
# write to the Free Software Foundation, Inc., 59 Temple Place -
# Suite 330, Boston, MA 02111-1307, USA
#
# The author can be reached by email at tim@fungible.com, or by
# paper mail at:
#
# Tim Freeman
# 381 North Fernwood Circle
# Sunnyvale, CA 94085-3059
#

# Edit mk/config.mk for configuration stuff.
include mk/config.mk

# Unlike the symbols above, nothing below is about the location of
# things.  In other words most people won't have to edit anything
# below.

# Note that install.pl pays attention to the RPM_BUILD_ROOT
# environment variable.
INSTALL:=perl $(shell pwd)/install.pl -v FUNGIMOL_VERSION=$(VERSION) \
 FUNGIMOL_BUILD_DATE=$(shell date +%Y-%m-%d) \
 BRENNERMD_VERSION=$(BRENNERMD_VERSION) \
 DEFAULT_FUNGIMOL_PATH=$(DEFAULT_FUNGIMOL_PATH)
export INSTALL

BRENNERMAKE=$(MAKE) -C brenner BRENNERMD_VERSION=$(BRENNERMD_VERSION) 

# The point behind the next one is that you can do "make QUIET=" to
# debug the commands that make would normally invoke but not echo.
QUIET:=@

USES_PACKAGES:=Util Hashtable String Configuration LinearAlgebra Graphics \
  Numanal NumanalTest Scene Physics Chemistry TestData TopLevel Actions XTop \
  Input MouseDrag
ifeq ($(LINUX_JOYSTICK), yes) 
   USES_PACKAGES+=LinuxJoystick
endif
USES_PACKAGES+=CursorBall Selection Transform App include Plugins
# Make TAGS before doing the real work so that I get a current TAGS
# table even if there is a compiler error.
# Except I haven't been using the tags table lately, so leave it out.
.PHONY:all
# Need to do configurationCheck after real-all, since if we do it in
# the other order there may be stale symlinks in the include directory 
# Likewise copyrightCheck.
all: build configurationCheck copyrightCheck
.PHONY:build
build: 
	$(QUIET)$(foreach p,$(USES_PACKAGES),echo Building $(p)... && \
	   $(MAKE) --no-print-directory -C $(p) && ) true

# All configurations should have a hasTheSameTypeAs method and a
# copy method.
.PHONY: configurationCheck
configurationCheck:
	-$(QUIET)rm -f /tmp/configurationCheck.txt
	$(QUIET)grep -L hasTheSameTypeAs */*Configuration.h > /tmp/configurationCheck.txt
	$(QUIET)grep -L copy */*Configuration.h >> /tmp/configurationCheck.txt
	$(QUIET)if [ -s /tmp/configurationCheck.txt ]; then\
	   echo Some files named '*Configuration.h' do not mention copy or hasTheSameTypeAs:;\
	   cat /tmp/configurationCheck.txt;\
	   exit 1;\
	fi
	-$(QUIET)rm -f /tmp/configurationCheck.txt
	$(QUIET)# The next command gets a bogus error if it doesn't find
	$(QUIET)# anything, so we need the "|| true".
	$(QUIET)grep "public *Configuration" `find . -name "*.h" -type f | grep -v Configuration` || true > /tmp/configurationCheck.txt
	$(QUIET)if [ -s /tmp/configurationCheck.txt ]; then\
	   echo Some classes inherit from Configuration but do not have Configuration in the name:;\
	   cat /tmp/configurationCheck.txt;\
	   exit 1;\
	fi
	$(QUIET)rm /tmp/configurationCheck.txt

# Exclude symlinks so we don't list things in the include directory.
ALLTEXT:=$(shell find . -type f '!' -type l \
			    '!' -perm +u+x \
                            '!' -path '*/tarhome/*' \
                            '!' -path '*/md/*' \
			    '!' -name '*.o' \
                            '!' -name '*.so' \
                            '!' -path '*/CVS*' '!' -name '*.dep' \
			    '!' -name '.cvsignore*' \
                            '!' -name '*~' '!' -name '*\#*' '!' -name '*.gif' \
			    '!' -name '*.pdb' '!' -name TAGS '!' -name '*.d' \
			    '!' -path '*/noship/*' '!' -path '*/shared/*' \
			    '!' -path '*/brenner/*' \
			    '!' -path '*/brennerc/*' \
			    '!' -name "fungimol.lsm" \
			    '!' -name '*.da' '!' -name '*.bb' '!' -name '*.bbg' \
			    '!' -name '*.gcov')

# Put a unique number into the name of the copyrightCheck.txt file so
# if root fails to run this script then normal users won't then
# immediately fail because the copyrigthCheck.txt file cannot be
# modified.  This problem actually happened.  Tim Freeman  9 Nov 2000.
COPYRIGHTTEMP:=/tmp/copyrightCheck.txt$(shell perl -e 'print $$$$')
.PHONY: copyrightCheck
copyrightCheck: 
	-$(QUIET)rm -f $(COPYRIGHTTEMP)
	$(QUIET)grep -L -i copyright $(ALLTEXT) > $(COPYRIGHTTEMP)
	$(QUIET)if [ -s $(COPYRIGHTTEMP) ]; then\
	   echo Some files have no copyright notice:; \
	   cat $(COPYRIGHTTEMP);\
	   exit 1;\
	fi;\
	rm $(COPYRIGHTTEMP)

# The --regex... helps us reliably recognize class names so we can
# complete them.
# Put the .h files first because I like to find things in the .h files first.
TAGS: $(ALLTEXT)
	etags '--regex=/[ \t]*class[\t]+\([A-Za-z][A-Za-z0-9_]*\)/\1/' \
	   --members $(ALLTEXT)

.PHONY:clean
# If I just have rm -f #*#, then it's interpreted as a gnu make
# comment and it is not cleaned.
clean:
	-rm TAGS
	$(MAKE) -C doc clean
	-rm -f *~ '#'*'#'
	$(QUIET)$(foreach p,$(USES_PACKAGES),echo Cleaning $(p)... && \
           $(MAKE) --no-print-directory -C $(p) clean NODS=yes && ) true
	$(MAKE) -C brennerc clean

# Clean everything.  The old clean target used to just clean debug or
# nodebug (whichever we would have built), but that seems to lead to
# operator error too often, so now clean does the same thing as
# fullclean.  Tim Freeman  3 Nov 2000.
.PHONY: fullclean
fullclean: clean

# Recompile from scratch.  No need to be root.
.PHONY: rebuild
rebuild:
	$(MAKE) fullclean
	$(MAKE) fullbuild

# Incrementally build for release.
.PHONY: fullbuild
fullbuild: install.pl
	$(MAKE) NDEBUG=yes EXEHOME=$(INSTALLHOME) \
		DEFAULT_FUNGIMOL_PATH=$(PLUGINHOME)
	$(BRENNERMAKE) fullbuild
	$(MAKE) -C brennerc FUNGIMOL=yes \
		FUNGIMOL_INCLUDE=$(shell pwd)/include \
		FUNGIMOL_MEMORY_DIR=$(shell pwd)/Util/nodebug \
		CC=$(CPLUSPLUS)

# In the source rpm, the shared directory doesn't exist.  Don't give
# errors in that case.
ifneq ($(wildcard shared/install.pl),)
install.pl: shared/install.pl
	cp shared/install.pl install.pl
endif

# Install just the executables and the documents.
# You should probably be root.
.PHONY: install
install: install.pl fullbuild
	$(MAKE) -C App install NDEBUG=yes EXEHOME=$(INSTALLHOME)
	$(MAKE) -C Plugins install
	$(MAKE) -C include install
	$(INSTALL) 0755 -d $(DOCHOME)
	$(INSTALL) 0444 COPYING.txt NOTICES.txt $(DOCHOME)
	$(MAKE) -C doc install
	$(BRENNERMAKE) install
	$(INSTALL) 0755 -d $(PLUGINHOME)
	$(INSTALL) 0555 brennerc/fungimol/BrennerAtom.so \
		$(PLUGINHOME)/BrennerAtom.so

# Uninstall.  Should be root.
# Note that if I said "rm -rf $(DOCHOME)/." instead, and DOCHOME were
# not set, then we'd do a "rm -rf /.", which is bad.  This version
# gets an error if DOCHOME is not set, which is better.
.PHONY:uninstall
uninstall:
	$(MAKE) -C App uninstall EXEHOME=$(INSTALLHOME)
	rm -rf $(DOCHOME)
	$(MAKE) -C include uninstall
	rm -rf $(PLUGINHOME)

# Put a tar file in ../fungimol-$(VERSION).tar.gz
# Note that Sunsite says to call it .tar.gz instead of .tgz.
# Need -follow to pick up the brennerc symlink.
.PHONY: tar
tar:
	$(INSTALL) 0755 -d $(TARHOME)
	$(BRENNERMAKE) tar TARHOME=$(TARHOME)
	$(MAKE) fullclean
	$(MAKE) install.pl
	find . -path './noship' -prune -type f -o \
               -path './shared' -prune -type f -o \
           -type f \
	   '!' -name '*.tar*' \
	   '!' -name '*.tgz' '!' -path '*/CVS/*' '!' -name '*~' \
           -follow \
	   > /tmp/fungimolfiles.txt
	$(QUIET)# Contort ourselves so that the file names in the archive
	$(QUIET)# start with fungimol-$(VERSION)/
	tar cTf /tmp/fungimolfiles.txt /tmp/fungimol-$(VERSION).tar
	rm /tmp/fungimolfiles.txt
	rm -rf fungimol-$(VERSION)
	mkdir fungimol-$(VERSION)
	(cd fungimol-$(VERSION); tar xf /tmp/fungimol-$(VERSION).tar)
	rm /tmp/fungimol-$(VERSION).tar
	tar czf $(TARHOME)/fungimol-$(VERSION).tar.gz fungimol-$(VERSION)
	rm -rf fungimol-$(VERSION)

# Make a source rpm.  Should be done as root.
# Do a "make tar" before "make srpm".
.PHONY: srpm
srpm: 
	$(BRENNERMAKE) srpm
	mkdir -p $(RPMHOME)/SOURCES $(RPMHOME)/SPECS
	cp $(TARHOME)/fungimol-$(VERSION).tar.gz $(RPMHOME)/SOURCES
	perl spec.pl $(VERSION) $(DOCHOME) $(INSTALLHOME) $(INCLUDEHOME) \
		$(SPLINEHOME) $(PLUGINHOME) > $(RPMHOME)/SPECS/fungimol.spec
	(cd $(RPMHOME)/SPECS; rpmbuild --target i386-pc-linux-gnu -ba fungimol.spec)

# Update my local copy of the global web pages.  Nobody but Tim
# Freeman should be doing this target.  I should do the following.
# The first check isn't necessary, but it's better to do it at the
# beginning than later.  Except "make onelump" is better.
#    make copyrightCheck
#    make tar
#    su root
#    make srpm
#    exit
#    make htmlupdate
HTMLHOME:=/home/tim/public_html/fungimol
.PHONY: htmlupdate
htmlupdate:
	rm -rf $(HTMLHOME)
	$(MAKE) -C doc install DOCHOME=$(HTMLHOME)
	$(INSTALL) 0444 $(TARHOME)/fungimol-$(VERSION).tar.gz \
	   $(RPMHOME)/RPMS/i386/fungimol-$(VERSION)-0.i386.rpm \
	   $(RPMHOME)/SRPMS/fungimol-$(VERSION)-0.src.rpm \
	   NOTICES.txt COPYING.txt \
	   $(HTMLHOME)
	$(BRENNERMAKE) htmlupdate
	cd $(HTMLHOME); md5sum *.rpm *.tar.gz | gpg --clearsign > MD5SUMS

SUCCESSTEMP:=/tmp/success$(shell perl -e 'print $$$$')
# Usage: su root -c make onelump
# Or even make onelump and it will figure out that we need to be root.
.PHONY: onelump
ifeq ($(shell whoami),root)
   # make srpm takes a long time then does make copyrightCheck.
   # Better to do a redundant copyrightCheck first so we don't wait a
   # long time for a failure.
   # Likewise for gpg misconfigurations.
   onelump:
	su $(USER) -c "make tar && touch $(SUCCESSTEMP)"
	perl -e '-e "$(SUCCESSTEMP)" || exit 1'
	rm $(SUCCESSTEMP)
	su $(USER) -c "gpg --list-key tim@fungible.com && touch $(SUCCESSTEMP)"
	perl -e '-e "$(SUCCESSTEMP)" || exit 1'
	rm $(SUCCESSTEMP)
	make copyrightCheck
	make srpm
	su $(USER) -c "make htmlupdate && touch $(SUCCESSTEMP)"
	perl -e '-e "$(SUCCESSTEMP)" || exit 1'
	rm $(SUCCESSTEMP)	
else
   onelump:
	su root -c "time make onelump"
endif
