SHELL=/bin/sh

ALLDIRS=play gist yorick matrix fft doc
CONFIGDIRS=play yorick
PKGDIRS=

all: yorexe gistexe docs

yorexe: Make.cfg
	@cd yorick; $(MAKE)

gistexe: Make.cfg
	@cd gist; $(MAKE) gist

docs:
	@cd doc; $(MAKE) docs

WITH_GLX=
Make.cfg:
	WITH_GLX=$(WITH_GLX) ./configure $(PKGDIRS)

config: distclean
	@$(MAKE) "WITH_GLX=$(WITH_GLX)" Make.cfg

prefix=.
Y_PLATFORM=.
Y_SITE=.
Y_HOME=.
ysite:
	if S="s?^prefix=.*?prefix=$(prefix)?;\
	s?^Y_PLATFORM=.*?Y_PLATFORM=$(Y_PLATFORM)?;\
	s?^Y_SITE=.*?Y_SITE=$(Y_SITE)?;\
	s?^Y_HOME=.*?Y_HOME=$(Y_HOME)?";\
	sed -e "$$S" ysite.sh >ysite.sh1; then mv ysite.sh1 ysite.sh; fi

clean::
	@rm -f Make.del
	@if test ! -r Make.cfg; then touch Make.cfg Make.del; fi
	@for d in $(ALLDIRS); do ( cd $$d; $(MAKE) clean; ); done
	@if test -r Make.del; then rm -f Make.cfg Make.del; fi
	rm -f *~ '#'* *.o cfg* ysite.sh? core a.out

distclean::
	@touch Make.cfg
	@for d in $(ALLDIRS); do ( cd $$d; $(MAKE) distclean; ); done
	rm -f *~ '#'* *.o cfg* Make.* ysite.sh? core a.out


check:
	@cd yorick; $(MAKE) check

INSTALL_ROOT=
Y_BINDIR=
Y_DOCDIR=
install: yorexe gistexe docs
	./install.sh +both "$(INSTALL_ROOT)" "$(Y_BINDIR)" $(Y_DOCDIR)

install1: yorexe gistexe
	./install.sh +home "$(INSTALL_ROOT)" "$(Y_BINDIR)" $(Y_DOCDIR)

uninstall:
	./install.sh -both "$(INSTALL_ROOT)" "$(Y_BINDIR)" $(Y_DOCDIR)

uninstall1:
	./install.sh -home "$(INSTALL_ROOT)" "$(Y_BINDIR)" $(Y_DOCDIR)

dist: distclean
	@$(MAKE) prefix=. Y_PLATFORM=. Y_SITE=. Y_HOME=. ysite
	W=`pwd`;N=`basename $$W`;R=`tail -1 VERSION`;cd ..;\
	tar cvf - $$N|gzip - >$$N.$$R.tgz;

YGROUP=yorick
sharable:
	chgrp -R $(YGROUP) .
	chmod -R g+w .
	find . -type d | xargs chmod g+s

relocatable:
	$(MAKE) Y_HOME=relocate ysite
	$(MAKE) install
	cp install.rel relocate/README
	cp emacs/yorick.el relocate
	mkdir relocate/contrib
	W=`pwd`;N=`basename $$W`;R=`tail -1 VERSION`;\
	mv relocate $$N-$$R;tar cvf - $$N-$$R|gzip - >$$N-$$R.tgz;\
	rm -rf $$N-$$R

# targets for yorgl package
yorgl:
	@if test -r Make.cfg && test -z "`grep ^GINCS= Make.cfg`"; then \
	    rm -f Make.cfg; fi
	@$(MAKE) WITH_GLX=1 yorexe
	@cd yorgl; $(MAKE)

# targets for ./configure
echocc:
	echo "$(CC)" >cfg.tmp
echorl:
	echo "$(RANLIB)" >cfg.tmp
echoar:
	echo "$(AR)" >cfg.tmp
echoml:
	echo "$(MATHLIB)" >cfg.tmp
pkgconfig:
	@for d in $(CONFIGDIRS); do ( cd $$d; $(MAKE) config; ); done

yorclean:
	@:
