#
# E.g. setenv MACHINE rs ; make -e
#
# See src/Makefile for the supported MACHINEs.
#
#  2006/Dec/22
#
#  On Solaris, the value of PWD does not change when make is invoked
#  recursively.  Consequently, it is necessary to explicitly set it
#  at the start of the makefile.
#
PWD = $(shell pwd)

#  2006/Dec/22
#
#  On Solaris, the value of PWD does not change when make is invoked
#  recursively.  Consequently, it is necessary to explicitly set it
#  at the start of the makefile.
#
PWD = $(shell pwd)

ifeq "$(MACHINE)" "sun4os57g"
	SHELL=/usr/bin/bash
else
	SHELL=/bin/sh
endif

Z0FILES = Makefile Makefile.win32 makeon Current TsFrozen TsCheck Ports Timings Version ToDo
Z1DIRS = src subcmd tools
Z2DIRS = lib contrib

OKFILES = $(Z0FILES) $(Z1DIRS) $(Z2DIRS)

#MAKEFLAGS = --no-print-directory
MAKEFLAGS =

# 'make VER=r1-0-3 zips' will create r1-0-3p[012].zip.
VER = aldor
ZIP = zip
ZIPFLAGS = -rq

#TIMER=
TIMER=time

# ----------------------------------------------------------------
# MAIN TARGETS
# ----------------------------------------------------------------

all:
	@echo "Usage:"
	@echo "   make compiler => build and install the compiler"
	@echo "   make remove    => removes the installed compiler"

compiler: build install

remove: removedistrib

# ----------------------------------------------------------------
# BUILD
# ----------------------------------------------------------------

build: envcheck
	$(SHELL) ./makeon $(MACHINE) alldirs

# Make sure that we have been invoked with the correct parameters.
envcheck:
	-@ if [ "x$(ALDORROOT)" = "x" ] ;     then echo "*** ALDORROOT unset"; fi
	-@ if [ "x$(MACHINE)" = "x" ] ;       then echo "*** MACHINE unset"; fi
	-@ if [ "x$(ALDOR_VERSION)" = "x" ] ; then echo "*** ALDOR_VERSION unset"; fi
	-@ if [ "x$(ALDOR_INSTALL)" = "x" ] ; then echo "*** ALDOR_INSTALL unset"; fi
	-@ if [ "x$(GmpDir)" = "x" ] ;        then echo "*** GmpDir unset"; fi
	@ [ "x$(MACHINE)" != "x" -a "x$(ALDORROOT)" != "x" \
	    -a "x$(ALDOR_VERSION)" != "x" -a "x$(ALDOR_INSTALL)" != "x" \
	    -a "x$(GmpDir)" != "x" ]

install:
	cp tools/unix/dolatex $(ALDORROOT)/bin
	cp tools/unix/dopdflatex $(ALDORROOT)/bin
	cp $(ALDORROOT)/toolbin/testaldor $(ALDORROOT)/bin
	chmod a+x $(ALDORROOT)/bin/dolatex $(ALDORROOT)/bin/dopdflatex
	rm -f $(ALDORROOT)/src 
	rm -rf $(ALDORROOT)/srclib
	rm -rf $(ALDORROOT)/share
	rm -rf $(ALDORROOT)/mabin
	rm -rf $(ALDORROOT)/toolbin
	rm -rf $(ALDORROOT)/makeon.log

alldirs: envcheck toolsdir subcmddir srcdir libdir contribdir junk

mostdirs: envcheck toolsdir subcmddir srcdir libdir junk

somedirs: envcheck toolsdir subcmddir

clean::
	@ rm -f *~
	$(SHELL) tools/unix/maclean $(MACHINE)

junk::	
	@ ls -d $(OKFILES) * | sort | grep -v CVS | uniq -u


toolsdir:  envcheck
	-(cd tools ; $(TIMER) $(MAKE) all)
clean::
	-(cd tools ; $(MAKE) clean)
junk::
	-(cd tools ; $(MAKE) junk)


srcdir: envcheck
	-(cd src ; $(TIMER) $(MAKE) all)
clean::
	-(cd src ; $(MAKE) clean)
junk::
	-(cd src ; $(MAKE) junk)


subcmddir: envcheck
	-(cd subcmd ; $(TIMER) $(MAKE) all)
clean::
	-(cd subcmd ; $(MAKE) clean)
junk::
	-(cd subcmd ; $(MAKE) junk)


libdir: envcheck
	-(cd lib ; $(TIMER) $(MAKE) all)
clean::
	-(cd lib ; $(MAKE) clean)
junk::
	-(cd lib ; $(MAKE) junk)


contribdir: envcheck
	-(cd contrib ; $(TIMER) $(MAKE) all)
clean::
	-(cd contrib ; $(MAKE) clean)
junk::
	-(cd contrib ; $(MAKE) junk)


# ----------------------------------------------------------------
# CLEAN
# ----------------------------------------------------------------

removedistrib:
	@echo "Removing distribution"
	@rm -rf $(ALDORROOT)/bin
	@rm -rf $(ALDORROOT)/lib
	@rm -rf $(ALDORROOT)/include

cleansources:
	@(cd src; make clean)

# ----------------------------------------------------------------
# Specific targets
# ----------------------------------------------------------------

new:
	@ find . -type f -newer TsFrozen -print

newer:
	@ find . -type f -newer TsCheck -print

print:
	-(cd doc ; $(MAKE) print)

cleandoc:
	-(cd doc ; $(MAKE) clean)

unlock:
	rm -f $(ALDORROOT)/lock

strip:
	strip `find $(ALDORROOT)/bin -type f \! -name axlbug -print`

doscrlf:
	find . -type f -exec dosfile '{}' ';' -print

zips:
	$(ZIP) $(ZIPFLAGS) $(VER)p0.zip `find . -name '*.ao' -print`
	$(ZIP) $(ZIPFLAGS) -l $(VER)p1.zip $(Z0FILES) $(Z1DIRS)
	$(ZIP) $(ZIPFLAGS) -l $(VER)p2.zip $(Z2DIRS) \
		-x `find . -name '*.ao' -print | sed s:\./::`
#	$(ZIP) $(ZIPFLAGS) -l $(VER)p3.zip $(Z3DIRS)
	@ ls -l $(VER)p?.zip
