#
# Makefile for octave
#
# John W. Eaton
# jwe@bevo.che.wisc.edu
# University of Wisconsin-Madison
# Department of Chemical Engineering

TOPDIR = .

srcdir = @srcdir@
top_srcdir = @top_srcdir@
VPATH = @srcdir@

include $(TOPDIR)/Makeconf

INSTALL = @INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_DATA = @INSTALL_DATA@

CONF_DISTFILES = Makefile.in octMakefile.in Makeconf.in configure \
	configure.in config.guess config.sub aclocal.m4 config.h.in \
	acconfig.h config.h.bot install-sh

DISTFILES = $(CONF_DISTFILES) \
	BUGS COPYING INSTALL INSTALL.OCTAVE NEWS PROJECTS README \
	README.NLP ROADMAP SENDING-PATCHES THANKS move-if-change \
	octave.sh octave-bug.in doinstall.sh mkinstalldirs mkoctfile.in \
	MAKEINFO.PATCH ChangeLog ChangeLog.[0-9] 

# Complete directory trees to distribute.
DISTDIRS = emacs glob kpathsea make # plplot

# Subdirectories in which to run `make all'.
SUBDIRS = @INFO_DIR@ @PLPLOT_DIR@ @READLINE_DIR@ @DLFCN_DIR@ glob \
	kpathsea libcruft liboctave src scripts doc examples

# Subdirectories in which ot run `make conf-dist'.
CONF_DISTSUBDIRS = src

# Subdirectories in which to run `make dist'.
DISTSUBDIRS = libcruft liboctave info readline dlfcn src scripts \
	test doc examples 

BINDISTFILES = COPYING ChangeLog NEWS README THANKS INSTALL.OCTAVE \
	BUGS PROJECTS octave-bug octave.sh doinstall.sh \
	mkinstalldirs

# Subdirectories in which to run `make dist'.
BINDISTSUBDIRS = emacs scripts doc

DIRS_TO_MAKE = $(bindir) $(libdir) $(octincludedir) $(fcnfiledir) \
  $(octfiledir) $(archlibdir) \
  `echo $(localfcnfilepath) | awk -F: '{for (i=1; i<=NF; i++) print $i}'` \
  `echo $(localoctfilepath) | awk -F: '{for (i=1; i<=NF; i++) print $i}'`

all: octave-bug mkoctfile $(SUBDIRS)
.PHONY: all

octave-bug: octave-bug.in Makeconf octMakefile
	@$(do-subst-config-vals)

mkoctfile: mkoctfile.in Makeconf octMakefile
	@$(do-subst-config-vals)

$(SUBDIRS):
	echo making all in $@
	cd $@ ; $(MAKE) all
.PHONY: $(SUBDIRS)

check:
	cd test; $(MAKE) check
.PHONY: check

octave.info:
	cd doc/interpreter ; $(MAKE) octave.info
.PHONY: octave.info

INSTALL.OCTAVE:
	cd doc ; $(MAKE) ../INSTALL.OCTAVE
.PHONY: INSTALL.OCTAVE

BUGS:
	cd doc ; $(MAKE) ../BUGS
.PHONY: BUGS

install::
	$(top_srcdir)/mkinstalldirs $(DIRS_TO_MAKE)
	$(INSTALL_PROGRAM) octave-bug $(bindir)/octave-bug
	$(INSTALL_PROGRAM) mkoctfile $(bindir)/mkoctfile
	$(INSTALL_DATA) config.h $(octincludedir)/config.h

maintainer-clean::
	@echo ""
	@echo "************************************************************"
	@echo "*                                                          *"
	@echo "* This command is intended for maintainers to use; it      *"
	@echo "* deletes files that may require special tools to rebuild. *"
	@echo "*                                                          *"
	@echo "************************************************************"
	@echo ""

install uninstall tags TAGS::
	@$(subdir-for-command)
.PHONY: install uninstall tags

clean mostlyclean distclean maintainer-clean::
	$(foreach d, $(SUBDIRS) test, $(do-subdir-for-command))
.PHONY: clean mostlyclean distclean maintainer-clean

maintainer-clean distclean::
	rm -f octMakefile octave-bug Makefile Makeconf config.cache \
	config.h config.log config.status Makerules.f77 mk-oct-links

# Now that things are under RCS control, we need to do the recursive
# chmod so that the distributed files end up with reasonable modes.

# Shared rules for making clean tar files.

clean-tar:
	echo octave-$(version) > .fname
	tar xf `cat .fname`.tar
	find `cat .fname` \( \( -name RCS -a -type d \) \
	  -o \( -name OLD -a -type d \) -o -name "=*" \
	  -o -name '*~' -o -name '#*#' -o -name config.log \
	  -o -name config.status -o -name Makefile \
	  -o -name c-auto.h \) -print | xargs rm -rf
	rm -f `cat .fname`/test/octave.test/*.m
	rm -rf `cat .fname`/test/octave.test/npsol
	rm -rf `cat .fname`/test/octave.test/qpsol
	chmod -R a+rwX `cat .fname`
	tar cf `cat .fname`.tar `cat .fname`
	rm -rf `cat .fname` .fname
.PHONY: clean-tar

dist-z:
	echo octave-$(version) > .fname
	rm -f `cat .fname`.tar.gz
	gzip --best `cat .fname`.tar
	rm -f .fname
.PHONY: dist-z

# Rules for making a source distribution.

links-for-dist: octave.info INSTALL.OCTAVE BUGS
	echo octave-$(version) > .fname
	rm -rf `cat .fname`
	mkdir `cat .fname`
	ln $(DISTFILES) `cat .fname`
	for dir in $(DISTDIRS); do ln -s ../$$dir `cat .fname`; done
	for dir in $(DISTSUBDIRS); do mkdir `cat .fname`/$$dir; cd $$dir; $(MAKE) dist; cd ..; done
.PHONY: links-for-dist

dist-tar: links-for-dist
	tar chf `cat .fname`.tar `cat .fname`
	rm -rf `cat .fname`
.PHONY: dist-tar

clean-dist-tar: dist-tar
	$(MAKE) clean-tar
.PHONY: clean-dist-tar

dist: clean-dist-tar
.PHONY: dist

# Rules for making a binary distribution.

links-for-bin-dist: octave.info INSTALL.OCTAVE BUGS
	echo octave-$(version) > .fname
	rm -rf `cat .fname`
	mkdir `cat .fname`
	ln $(BINDISTFILES) `cat .fname`
	for dir in $(BINDISTSUBDIRS); do mkdir `cat .fname`/$$dir; cd $$dir; $(MAKE) dist; cd ..; done
.PHONY: links-for-dist

bin-dist-tar: links-for-bin-dist
	-strip src/octave
	echo octave-$(version) > .fname
	ln src/octave `cat .fname`/octave
	-strip scripts/image/octtopnm
	ln scripts/image/octtopnm `cat .fname`/octtopnm
	tar chf `cat .fname`.tar `cat .fname`
	rm -rf `cat .fname`
.PHONY: bin-dist-tar

clean-bin-dist-tar: bin-dist-tar
	$(MAKE) clean-tar
.PHONY: clean-bin-dist-tar

binary-dist: clean-bin-dist-tar
.PHONY: binary-dist

# Rules for making a distribution of the docs.

doc-dist-tar:
	echo octave-$(version) > .fname
	echo octave-doc-$(version).tar > .tarfname
	rm -rf `cat .fname`
	mkdir `cat .fname`
	ln README.doc COPYING `cat .fname`
	mkdir `cat .fname`/doc; cd doc; $(MAKE) doc-dist
	tar chf `cat .tarfname` `cat .fname`
	rm -rf `cat .fname`
.PHONY: doc-dist-tar

doc-dist: doc-dist-tar
.PHONY: doc-dist

# Rules for making a distribution for local use.

local-dist-tar: octave.info INSTALL.OCTAVE BUGS
	echo octave-local-$(version) > .fname
	rm -rf `cat .fname`
	mkdir `cat .fname`
	ln $(DISTFILES) `cat .fname`
	for dir in $(DISTDIRS); do ln -s ../$$dir `cat .fname`; done
	for dir in $(DISTSUBDIRS); do mkdir `cat .fname`/$$dir; cd $$dir; $(MAKE) $@; cd ..; done
	tar chf `cat .fname`.tar `cat .fname`
	rm -rf `cat .fname`
.PHONY: local-dist-tar

clean-local-dist-tar: local-dist-tar
	$(MAKE) clean-tar
.PHONY: clean-local-dist-tar

local-dist: clean-local-dist-tar
.PHONY: local-dist

# Rules for splitting a distribution.

split-dist:
	echo octave-$(version) > .fname
	split -b 1350k `cat .fname`.tar.gz `cat .fname`.tar.gz-
	rm -f .fname
.PHONY: split-dist

split-local-dist:
	echo octave-local-$(version) > .fname
	split -b 1350k `cat .fname`.tar.gz `cat .fname`.tar.gz-
	rm -f .fname
.PHONY: split-local-dist

# Rules for making a dist of just the stuff needed to run configure.

conf-dist:
	echo config-dist-$(version) > .fname
	rm -rf `cat .fname`
	mkdir `cat .fname`
	ln $(CONF_DISTFILES) `cat .fname`
	for dir in $(CONF_DISTSUBDIRS); do mkdir `cat .fname`/$$dir; cd $$dir; $(MAKE) conf-dist; cd ..; done
	tar chf `cat .fname`.tar `cat .fname`
	rm -rf `cat .fname`
	gzip --best `cat .fname`.tar
	rm -f .fname
.PHONY: conf-dist

# Rules for making a snapshot.

snapshot-z: snapshot-version
	$(MAKE) dist
	$(MAKE) dist-z
.PHONY: snapshot-z

snapshot: snapshot-z
.PHONY: snapshot

snapshot-version:
	@echo "creating src/version.h"
	@gawk '/#define OCTAVE_VERSION[ \t]*/ { \
	  datestring = strftime("%y%m%d", systime()); \
	  printf("#define OCTAVE_VERSION \"ss-%s\"\n", datestring); \
	  next; \
	} { print $$0 }' src/version.h > src/version.h.new
	@$(top_srcdir)/move-if-change src/version.h.new src/version.h
.PHONY: snapshot-version
