# 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@infoscreen.com, or by
# paper mail at:
#
# Tim Freeman
# 655 S. FairOaks Ave., Apt B-316
# Sunnyvale, CA 94086
#

# The directory to put the tar files into for "make tar".
TARHOME:=/home/tim
export TARHOME

# The version number for Fungimol.
VERSION:=0.3.4
export VERSION

# The version number for brennermd.  
BRENNERMD_VERSION=0.1.1

# We don't export BRENNERMD_VERSION.  Instead, we pass it in by using
# BRENNERMAKE to do a build in the brenner directory.  Search for the
# definition of BRENNERMAKE below to find out more.  I (Tim Freeman
# as of 6 Jul 2000) don't remember why I treat this differently from
# the Fungimol version.

# Where to install the binaries.
INSTALLHOME:=/usr/bin

# Where to install the documentation.  Beware that when we do an
# uninstall, this entire directory will be rm -rf'd, so don't use a
# directory that will have other stuff in it.  (This does not apply if
# you're using rpm to uninstall.)
DOCHOME:=/usr/doc/fungimol-$(VERSION)
export DOCHOME

# Where to put the include files.  Treated like DOCHOME when we do an
# uninstall, so don't use a directory that has anything else important
# in it.
INCLUDEHOME:=/usr/include/fungimol
export INCLUDEHOME

# Where the Fungimol executable will reside.  
# In the fullbuild and install targets, a recursive make is used to
# set this to INSTALLHOME.
ifndef EXEHOME
   EXEHOME:=$(shell pwd)/App/nodebug
endif
export EXEHOME

# Where sources for source RPM's typically reside.  The maximum RPM
# document says /usr/src/redhat, but on my system it's /usr/src/RPM.
# The trend seems to be for Red Hat to take their name off of RPM
# (presumably to make it a widely-accepted standard), so I'm guessing
# that /usr/src/RPM is right in the long term.
RPMHOME:=/usr/src/RPM

# Where to put the splines for Brenner's molecular dynamics program.
# Unfortunately this has to match strings in the source code for
# brennermd; we don't automatically edit that source code to match
# what appears here.
SPLINEHOME=/usr/share/brennermd
export SPLINEHOME

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

INSTALL:=perl $(shell pwd)/install.pl FUNGIMOL_VERSION=$(VERSION) FUNGIMOL_BUILD_DATE=$(shell date +%Y-%m-%d) BRENNERMD_VERSION=$(BRENNERMD_VERSION)
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 Physics Scene Chemistry TestData TopLevel Actions XTop Input \
  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 '!' -name '*.o' \
                            '!' -name '*.so' '!' -name 'fungimol' \
			    '!' -name 'fungimol-16bpp' \
                            '!' -path '*/CVS*' '!' -name '*.dep' \
			    '!' -name '.cvsignore' \
                            '!' -name '*~' '!' -name '*\#*' '!' -name '*.gif' \
			    '!' -name '*.pdb' '!' -name TAGS '!' -name '*.d' \
			    '!' -path '*/noship/*' '!' -path '*/shared/*' \
			    '!' -path '*/brenner/*' \
			    '!' -name "fungimol.lsm" \
			    '!' -name '*.da' '!' -name '*.bb' '!' -name '*.bbg' \
			    '!' -name '*.gcov')
.PHONY: copyrightCheck
copyrightCheck: 
	-$(QUIET)rm -f /tmp/copyrightCheck.txt
	$(QUIET)grep -L -i copyright $(ALLTEXT) > /tmp/copyrightCheck.txt
	$(QUIET)if [ -s /tmp/copyrightCheck.txt ]; then\
	   echo Some files have no copyright notice:; \
	   cat /tmp/copyrightCheck.txt;\
	   exit 1;\
	fi;\
	rm /tmp/copyrightCheck.txt

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

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

# Clean everything.
.PHONY: fullclean
fullclean:
	-rm TAGS
	$(MAKE) -C doc clean
	$(MAKE) clean NDEBUG=yes
	$(MAKE) 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)
	$(BRENNERMAKE) fullbuild

# 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
	$(MAKE) -C App install NDEBUG=yes EXEHOME=$(INSTALLHOME)
	$(MAKE) -C Plugins install
	$(MAKE) -C include install
	install -d $(DOCHOME)
	install -m ugo+r COPYING.txt NOTICES.txt $(DOCHOME)
	$(MAKE) -C doc install
	$(BRENNERMAKE) install

# 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

# Put a tar file in ../fungimol-$(VERSION).tar.gz
# Note that Sunsite says to call it .tar.gz instead of .tgz.
.PHONY: tar
tar:
	$(BRENNERMAKE) tar TARHOME=$(TARHOME)
	$(MAKE) fullclean
	cp shared/install.pl install.pl
	find . -type f '!' -path '*/noship/*' '!' -path '*/shared/*' \
	   '!' -name '*.tar*' \
	   '!' -name '*.tgz' '!' -path '*/CVS/*' '!' -name '*~' \
	   > /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) > $(RPMHOME)/SPECS/fungimol.spec
	(cd $(RPMHOME)/SPECS; rpm --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:
#    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

# 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)
   onelump:
	su $(USER) -c "make tar"
	make srpm
	su $(USER) -c "make htmlupdate"
else
   onelump:
	su root -c "make onelump"
endif
