#
# 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_SCRIPT = @INSTALL_SCRIPT@
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 NEWS.[0-9] PROJECTS \
	README README.Linux README.NLP README.Windows ROADMAP \
	SENDING-PATCHES THANKS move-if-change octave-sh octave-bug.in \
	install-octave mkinstalldirs mkoctfile.in texi2dvi INFO.PATCH \
	MAKEINFO.PATCH ChangeLog ChangeLog.[0-9]

# Complete directory trees to distribute.
DISTDIRS = 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 to run `make conf-dist'.
CONF_DISTSUBDIRS = src

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

XBINDISTFILES = BUGS COPYING INSTALL INSTALL.OCTAVE NEWS NEWS.[0-9] \
	PROJECTS README README.Linux README.NLP README.Windows \
	SENDING-PATCHES THANKS octave-sh install-octave mkinstalldirs \
	ChangeLog ChangeLog.[0-9]

BINDISTFILES = $(addprefix $(srcdir)/, $(XBINDISTFILES)) \
	octave-bug VERSION ARCH

# Subdirectories in which to run `make bin-dist'.
BINDISTSUBDIRS = libcruft liboctave src kpathsea readline info \
	scripts doc emacs examples

# Subdirectories in which to run clean targets.
CLEANSUBDIRS = $(DISTSUBDIRS) glob kpathsea

DIRS_TO_MAKE = $(bindir) $(libdir) $(octincludedir) $(fcnfiledir) \
  $(octfiledir) $(archlibdir) $(localarchlibdir) \
  `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)
	chmod a+rx $@

mkoctfile: mkoctfile.in Makeconf octMakefile
	@$(do-subst-config-vals)
	chmod a+rx $@

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

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

kpathsea.info:
	cd kpathsea ; $(MAKE) kpathsea.info
.PHONY: kpathsea.info

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_SCRIPT) octave-bug $(bindir)/octave-bug
	$(INSTALL_SCRIPT) 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, $(CLEANSUBDIRS), $(do-subdir-for-command))
.PHONY: clean mostlyclean distclean maintainer-clean

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

# Rules for making a source distribution.

dist-info-files: INSTALL.OCTAVE BUGS
.PHONY: dist-info-files

dist: dist-info-files
	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
	tar chf `cat .fname`.tar `cat .fname`
	rm -rf `cat .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`
	gzip --best `cat .fname`.tar
	rm -f .fname
.PHONY: dist

# Rules for making a 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

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

# 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 binary distribution.

bin-dist-type:
	@if [ -z "$(DIST_TYPE)" ]; then \
	  echo "***" ; \
	  echo "*** Must specify a value for DIST_TYPE (static or dynamic)" ; \
	  echo "***" ; \
	  exit 1 ; \
	else \
	  exit 0 ; \
	fi
.PHONY: bin-dist-type

VERSION:
	echo $(version) > VERSION
.PHONY: VERSION

ARCH:
	echo $(target_host_type) > ARCH
.PHONY: ARCH

binary-dist: bin-dist-type VERSION ARCH octave-bug dist-info-files
	echo octave-$(version)-$(target_host_type)-$(DIST_TYPE) > .fname
	rm -rf `cat .fname`
	mkdir `cat .fname`
	ln $(BINDISTFILES) `cat .fname`
	for dir in $(BINDISTSUBDIRS); do \
	  mkdir `cat .fname`/$$dir ; \
	  cd $$dir ; \
	  $(MAKE) bin-dist ; \
	  cd .. ; \
	done
	tar chf `cat .fname`.tar `cat .fname`
	rm -rf `cat .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+rw `cat .fname`
	find `cat .fname` \( -perm 766 -o -perm 676 -o -perm 667 \
	  -o -perm 776 -o -perm 677 -o -perm 767 \) -print | xargs chmod a+x
	tar cf `cat .fname`.tar `cat .fname`
	rm -rf `cat .fname`
	gzip --best `cat .fname`.tar
	rm -f .fname
.PHONY: binary-dist
