#---------------------------------------------------------------------------#
# Copyright (C) 1995 University of Melbourne.
# This file may only be copied under the terms of the GNU General
# Public License - see the file COPYING in the Mercury distribution.
#---------------------------------------------------------------------------#

# This Mmake file is used to build a binary distribution.
# You need to have already installed the Mercury compiler.

MERCURY_DIR=..
include $(MERCURY_DIR)/Mmake.common

MERCURY_VERSION	= mercury-0.6.$(FULLARCH)

INSTALL_LIB_DIR	= $(INSTALL_PREFIX)/lib

LIB_FILES	= bin doc html inc ints lib modules $(NUPROLOG) $(SICSTUS)

UTILS		= $(INSTALL_PREFIX)/bin/mkinit \
		  $(INSTALL_PREFIX)/bin/mdemangle \
		  $(INSTALL_PREFIX)/bin/mtags

README_ETC	= ../COPYING ../COPYING.LIB ../HISTORY ../LIMITATIONS \
		  ../NEWS ../RELEASE_NOTES ../BUGS

SCRIPT_FILES	= ../scripts/*.in ../scripts/Mmake.rules

CONFIG_FILES	= ../config.sub ../config.guess ../install-sh

MISC_FILES	= INSTALL README README.MS-Windows \
		  build_vars Makefile.in configure configure.in

#-----------------------------------------------------------------------------#

DEFAULT_TARGET=bindist

.PHONY: bindist
bindist: binary_distribution

.PHONY: binary_distribution
binary_distribution: bindist.configure bindist.build_vars
	# Make a quick check that there is a mercury installation in
	# $(INSTALL_DIR) by looking for mc and mercury_compile.
	test -f $(INSTALL_BINDIR)/mc
	test -f $(INSTALL_MERC_BIN_DIR)/mercury_compile
	# create a temporary directory in which to assemble the distribution
	test -d $(MERCURY_VERSION) || mkdir $(MERCURY_VERSION)
	# tar it up
	# (XXX don't use redirection or pipes, because on gnu-win32
	# stdout defaults to text mode, which breaks tar and gzip)
	(dir=`pwd` && cd $(INSTALL_LIB_DIR)/mercury && \
		tar -cf $$dir/$(MERCURY_VERSION)/lib.tar $(LIB_FILES) && \
		gzip $$dir/$(MERCURY_VERSION)/lib.tar)
	(dir=`pwd` && cd $(INSTALL_INFO_DIR)/.. && \
		tar -cf $$dir/$(MERCURY_VERSION)/info.tar info && \
		gzip $$dir/$(MERCURY_VERSION)/info.tar)
	test -d $(MERCURY_VERSION)/scripts || mkdir $(MERCURY_VERSION)/scripts
	cp $(SCRIPT_FILES) $(MERCURY_VERSION)/scripts
	test -d $(MERCURY_VERSION)/util || mkdir $(MERCURY_VERSION)/util
	cp $(UTILS) $(MERCURY_VERSION)/util
	cp $(CONFIG_FILES) $(MERCURY_VERSION)
	cp $(README_ETC) $(MERCURY_VERSION)
	cd ../samples; mmake realclean
	cd ../samples/diff; mmake realclean
	cd ../samples/c_interface; mmake realclean
	cd ../samples/c_interface/c_calls_mercury; mmake realclean
	cd ../samples/c_interface/mercury_calls_c; mmake realclean
	cp -r ../samples $(MERCURY_VERSION)/samples
	for file in $(MISC_FILES); do \
		cp bindist.$$file $(MERCURY_VERSION)/$$file || exit 1; \
	done
	# (XXX don't use redirection or pipes, because on gnu-win32
	# stdout defaults to text mode, which breaks tar and gzip)
	tar -cf ../$(MERCURY_VERSION).tar $(MERCURY_VERSION)
	gzip ../$(MERCURY_VERSION).tar
	rm -rf $(MERCURY_VERSION)

bindist.configure: bindist.configure.in
	autoconf bindist.configure.in > bindist.configure

bindist.build_vars: bindist.build_vars.in
	CONFIG_FILES=bindist.build_vars CONFIG_HEADERS= \
	$(MERCURY_DIR)/config.status

#-----------------------------------------------------------------------------#

realclean:
	rm -f bindist.configure bindist.build_vars

#-----------------------------------------------------------------------------#
