#
#  Authors:
#    Michael Mehl (mehl@dfki.de)
# 
#  Contributors:
#    Denys Duchier (duchier@ps.uni-sb.de)
#    Christian Schulte <schulte@ps.uni-sb.de>
# 
#  Copyright:
#    Michael Mehl, 1998
# 
#  Last change:
#    $Date$ by $Author$
#    $Revision$
# 
#  This file is part of Mozart, an implementation 
#  of Oz 3:
#     http://www.mozart-oz.org
# 
#  See the file "LICENSE" or
#     http://www.mozart-oz.org/LICENSE.html
#  for information on usage and redistribution 
#  of this file, and for a DISCLAIMER OF ALL 
#  WARRANTIES.

# !!! THIS MAKEFILE REQUIRES GNU MAKE !!!!

######################################################################
# path
######################################################################

BUILDTOP=	@BUILDTOP@
SRCTOP=		@SRCTOP@
SRCDIR=		@srcdir@
VPATH=		@srcdir@
PREFIX=		@prefix@
PLATFORM=	@PLATFORM@

HOMEURL=	@HOMEURL@
HOMECACHE=      @HOMECACHE@

BINDIR=		$(PREFIX)/bin
SHAREDIR=	$(PREFIX)/share
TOOLSDIR=	$(SHAREDIR)/tools
ELISPDIR=	$(SHAREDIR)/elisp
IMAGESDIR=	$(SHAREDIR)/images
CACHEDIR= 	$(PREFIX)/$(HOMECACHE)
CACHETOOLS= 	$(CACHEDIR)/tools
CACHEBIN=	$(CACHEDIR)/bin
SHARECACHE=	$(CACHEDIR)/share

######################################################################
# programs
######################################################################

MAKE=		@MAKE@
INSTALL=	@INSTALL@
INSTALL_BIN=	$(INSTALL) -m 555
INSTALL_FILE=	$(INSTALL) -m 444
INSTALL_DIR=	@INSTALL_DIR@

######################################################################
# other variables
######################################################################

OZVERSION=	$(SRCTOP)/OZVERSION
OZBUILD=        @OZBUILDDATE@
FILES=		README LICENSE LICENSE.html LICENSE.rtf
ALLFILES=	$(FILES:%=$(PREFIX)/%)

# sub directories for recursive makes
SUBDIRS=	@oz_subdirs@

######################################################################
# common targets
######################################################################

.PHONY: all bootstrap install clean veryclean distclean check

bootstrap::

all bootstrap clean veryclean depend distclean check install::
	dirs="$(SUBDIRS)"; \
	for i in $$dirs; do \
	  if (cd $$i && $(MAKE) $@); \
          then true; \
          else exit 1; \
          fi; \
        done

clean::
	-rm -f *~

veryclean::
	$(MAKE) clean SUBDIRS=

distclean::
	$(MAKE) veryclean SUBDIRS=
	-rm -f Makefile config.cache config.log config.status

install:: $(PREFIX)

$(PREFIX):
	$(INSTALL_DIR) $@

all:: Makefile

Makefile: Makefile.in ./config.status
	./config.status

./config.status: ./configure
	./config.status --recheck

install:: $(ALLFILES)

$(ALLFILES): $(PREFIX)/% : % $(PREFIX)
	$(INSTALL_FILE) $< $@

######################################################################
# other targets
######################################################################

PLATFORMS=	solaris-sparc aix3-rs600 freebsd-i486 hpux-700 \
		irix6-mips linux-i486 netbsd-i486 netbsd-m68k netbsd-sparc \
		osf1-alpha solaris-i486 sunos-sparc freebsdelf-i486
COMPRESS=	gzip -9
ZIP=		zip -r
TAR=		tar
PACKDIR=	/tmp/pack
MPD=		$(PACKDIR)/mozart
WIN32CACHEDIR=  $(MPD)/cache/$(HOMECACHE)
NOTDIRSRCTOP	= $(notdir $(SRCTOP))

.PHONY: src doc $(PLATFORMS)

ifeq ($(USER),root)
# make sure that what goes into the tar files for preparing the RPMs
# is actually owned by root, otherwise the owner and group of the file
# itself will be used, which aren't likely to exist on a user's machine.
TARTAR	=	$(TAR) --owner=root --group=root
else
TARTAR	=	$(TAR)
endif

src: $(PACKDIR)
	echo "[" $(NOTDIRSRCTOP) "]"
	test -n "$(NOTDIRSRCTOP)" || { echo "bad"; exit 1; }
	(cd $(PACKDIR); \
	 mkdir -p $(NOTDIRSRCTOP); \
	 cd $(NOTDIRSRCTOP); \
	 (cd $(SRCTOP); \
	  $(TAR) -cf - .) | $(TAR) -xf - ; \
	 cd ..; \
	 tbr=`find $(NOTDIRSRCTOP) -name CVS -type d 2>/dev/null` ; \
	 rm -rf $$tbr ; \
	 find $(NOTDIRSRCTOP) -name '.cvsignore' -type f -exec rm -f '{}' ';' ; \
	 mv $(NOTDIRSRCTOP) mozart-`$(OZVERSION)`.$(OZBUILD); \
	 $(TARTAR) -cf - mozart-`$(OZVERSION)`.$(OZBUILD) ) | \
	 $(COMPRESS) > mozart-`$(OZVERSION)`.$(OZBUILD)-src.tar.gz
	rm -rf $(PACKDIR)/mozart-`$(OZVERSION)`.$(OZBUILD)

doc: $(PACKDIR) $(PACKDIR)/mozart
	rm -f files.doc
	$(MAKE) files.doc
	rm -rf $(PACKDIR)/mozart
	$(INSTALL_DIR) $(PACKDIR)/mozart
	( (cd $(PREFIX); \
	   $(TAR) -cf - -T $(BUILDTOP)/files.doc ) | \
           (cd $(PACKDIR)/mozart; $(TAR) -xf - ) )
	(cd $(PACKDIR); $(TARTAR) -cf - mozart ) | \
	 $(COMPRESS) > mozart-`$(OZVERSION)`.$(OZBUILD)-doc.tar.gz
	rm -rf $(PACKDIR)/mozart
	rm -f files.doc

bin: $(PLATFORM)

$(PLATFORMS): $(PACKDIR) $(PACKDIR)/mozart
	rm -f files.base files.base-$@ files.contrib files.contrib-$@ 
	$(MAKE) files.base files.base-$@ files.contrib files.contrib-$@
	rm -f files.all
	cat files.base files.base-$@ files.contrib files.contrib-$@ > files.all
	rm -rf $(PACKDIR)/mozart
	$(INSTALL_DIR) $(PACKDIR)/mozart
	( (cd $(PREFIX); \
	   $(TAR) -cf - -T $(BUILDTOP)/files.all ) | \
           (cd $(PACKDIR)/mozart; $(TAR) -xf - ) )
	(cd $(PACKDIR); $(TARTAR) -cf - mozart ) | \
	 $(COMPRESS) > mozart-`$(OZVERSION)`.$(OZBUILD)-$@.tar.gz
	rm -rf $(PACKDIR)/mozart
	ldd $(PREFIX)/platform/$(PLATFORM)/emulator.exe \
	    $(PREFIX)/platform/$(PLATFORM)/tk.exe | \
	sed 's/^.*emulator.exe:/emulator.exe dynamically linked against:/' | \
	sed 's/^.*tk.exe:/tk.exe dynamically linked against:/' > README-`$(OZVERSION)`.$(OZBUILD)-$@
	rm -f files.base files.base-$@ files.contrib files.contrib-$@ files.all

$(PACKDIR) $(PACKDIR)/mozart:
	INSTALL_DIR_CHMOD=777; export INSTALL_DIR_CHMOD && $(INSTALL_DIR) $@

win32-i486-cygwin: $(PACKDIR) $(PACKDIR)/mozart
	( cd $(PREFIX) && \
	  $(TAR) c --exclude platform/win32-i486/oztool.sh -f - \
		   $(FILES) \
		   include share \
		   doc examples \
		   cache/x-oz/contrib cache/x-oz/system \
		   bin platform/win32-i486 \
	) | (cd $(MPD) && $(TAR) xf -)
	find $(MPD) -name '*.so-*' -a \! \( -name '*.so-win32-i486' \) \
		-exec rm -f '{}' ';'
	rm -rf mozart-win32
	$(INSTALL_DIR) mozart-win32
	(cd $(MPD) && \
	 $(TAR) cf - $(FILES) bin cache/x-oz/system cache/x-oz/contrib \
		     doc examples \
		     include share/Init.ozf share/elisp platform) | \
	 (cd mozart-win32 && $(TAR) xf -)
	$(INSTALL_DIR) mozart-win32/doc
	$(INSTALL) -m 644 $(SRCTOP)/doc/utilities/ozdoc-iexplorer.css \
			mozart-win32/doc/ozdoc.css
	rm -rf $(PACKDIR)/mozart
	rm -f mozart-win32.zip
	$(ZIP) mozart-win32.zip mozart-win32
	rm -rf mozart-win32

win32-i486-cross: $(PACKDIR) $(PACKDIR)/mozart
	( cd $(PREFIX); \
	  $(TAR) cf - \
		   $(FILES) \
		   include share \
		   doc examples \
		   cache/x-oz/contrib cache/x-oz/system \
		   winbin platform/win32-i486 \
	) | (cd $(MPD); $(TAR) xf -)
	find $(MPD) -name '*.so-*' -a \! \( -name '*.so-win32-i486' \) \
		-exec rm -f '{}' ';'
	for i in ozc ozd ozl; do \
	  cat $(MPD)/winbin/ozwrapper.bin \
	      $(PREFIX)/bin/$$i > $(MPD)/winbin/$$i.exe; \
	done
	for i in convertTextPickle; do \
	  if [ -r $(PREFIX)/bin/$$i ]; then \
	    cat $(MPD)/winbin/ozwrapper.bin \
		$(PREFIX)/bin/$$i > $(MPD)/winbin/$$i.exe; \
	  fi; \
	done
	mv $(MPD)/winbin $(MPD)/bin
	chmod -R a+x $(MPD)/bin
	rm -rf mozart-win32
	$(INSTALL_DIR) mozart-win32
	(cd $(MPD); \
	 $(TAR) cf - $(FILES) bin cache/x-oz/system cache/x-oz/contrib \
		     doc examples \
                     include share/Init.ozf share/elisp platform) | \
	 (cd mozart-win32; $(TAR) xf -)
	$(INSTALL_DIR) mozart-win32/doc/doc
	$(INSTALL_FILE) $(SRCTOP)/doc/utilities/ozdoc-iexplorer.css \
			mozart-win32/doc/doc/ozdoc.css
	rm -rf /tmp/pack/mozart
	rm -f mozart-win32.zip
	$(ZIP) -r mozart-win32.zip mozart-win32
	rm -rf mozart-win32

files.base:
	(cd $(PREFIX); \
         find . \( -type f -o -type l \) -a \( \
		! -path './platform/*' -a \
		! -path './winbin/*' -a \
		! -path './cache/x-oz/contrib/*' -a  \
		! -path './cache/x-oz/doc/*' -a  \
		! -path './doc/*' -a  \
		! -path './print/*' -a  \
		! -path './examples/*' -a  \
		! -path './cache/x-oz/system/gump/examples/*' -a  \
		! -path './share/doc/*' -a  \
		! -path './bin/ozdoc' -a \
		! -path './bin/convertTextPickle' -a \
		! -name '*.so-*' -a \
		! -path './share/elisp/Fontifier.el' \
	\) ) > $@


files.base-%:
	(cd $(PREFIX); \
         find . \( -type f -o -type l \) -a \
		\( -path './platform/$*/*' -o \
		   -path './cache/x-oz/boot/*.so-$*' -o \
		   -path './cache/x-oz/system/*.so-$*' \) \
	) > $@

files.contrib:
	(cd $(PREFIX); \
	 find . \( -type f -o -type l \) -a \( \
		\( -path './cache/x-oz/contrib/*' -a ! -name '*.so-*' \) -o \
		-path './bin/ozdoc' -o \
		-path './bin/convertTextPickle' -o \
		-path './share/doc/*' -o \
		-path './share/elisp/Fontifier.el' \
	\) ) > $@

files.contrib-%:
	(cd $(PREFIX); \
	 find . \( -type f -o -type l \) -a \
		-path './cache/x-oz/contrib/*' -a \
		-name '*.so-$*' \
	) > $@

files.doc:
	(cd $(PREFIX); \
	 find . \( -type f -o -type l \) -a \( \
		-path './doc/*' -o \
		-path './cache/x-oz/system/gump/examples/*' -o \
		-path './cache/x-oz/doc/*' -o \
		-path './examples/*' \
	\) -a \! \( -path './doc/Mozart.hh?' -o \
		    -path './doc/add-ons/*/*' \) ) > $@

