# Copyright 1997,1998 Alexandre Oliva <oliva@dcc.unicamp.br>

# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

GUARANA_VERSION = 1.4.1
KAFFE_TGZDIR = .

KAFFE_VERSION = 1.0.b1
KAFFE_SNAPSHOT_SUFFIX = _1998-09-10
KAFFE_TGZ_BASENAME = kaffe-$(KAFFE_VERSION)$(KAFFE_SNAPSHOT_SUFFIX)
KAFFE_DIR = $(KAFFE_TGZ_BASENAME)
KAFFE_TGZ = $(KAFFE_TGZDIR)/$(KAFFE_TGZ_BASENAME).tar.gz
PATCH_FILE = $(KAFFE_TGZ_BASENAME).patch
GUARANA_KAFFE_BRANCH =

KAFFE_OLD_VERSION = 0.10.1
KAFFE_OLD_SNAPSHOT_SUFFIX = _1998-07-13
KAFFE_OLD_TGZ_BASENAME = kaffe-$(KAFFE_OLD_VERSION)$(KAFFE_OLD_SNAPSHOT_SUFFIX)
KAFFE_OLD_DIR = $(KAFFE_OLD_TGZ_BASENAME)
KAFFE_OLD_TGZ = $(KAFFE_TGZDIR)/$(KAFFE_OLD_TGZ_BASENAME).tar.gz
OLD_PATCH_FILE = $(KAFFE_OLD_TGZ_BASENAME).patch
GUARANA_KAFFE_OLD_BRANCH = _kaffe_`echo $(KAFFE_OLD_VERSION) | sed 's/\./-/g'`

FLAGS_FOR_OLD = "KAFFE_VERSION=$(KAFFE_OLD_VERSION)" \
		"KAFFE_SNAPSHOT_SUFFIX=$(KAFFE_OLD_SNAPSHOT_SUFFIX)" \
		"KAFFE_DIR=$(KAFFE_OLD_DIR)" \
		"KAFFE_TGZ_BASENAME=$(KAFFE_OLD_TGZ_BASENAME)" \
		"KAFFE_TGZ=$(KAFFE_OLD_TGZ)" \
		"KAFFE_TGZDIR=$(KAFFE_TGZDIR)" \
		"PATCH_FILE=$(OLD_PATCH_FILE)" \
		"GUARANA_KAFFE_BRANCH=$(GUARANA_KAFFE_OLD_BRANCH)"

.PHONY: all all-$(KAFFE_VERSION) old all-old all-$(KAFFE_OLD_VERSION)
all all-$(KAFFE_VERSION): decompress-stmp patch-stmp

old all-old all-$(KAFFE_OLD_VERSION):
	$(MAKE) $(FLAGS_FOR_OLD) all

decompress-stmp:
	test -d $(KAFFE_DIR) || \
	gzip -dc $(KAFFE_TGZ) | tar -xf -
	echo decompressed > $@

patch-stmp:
	(cd $(KAFFE_DIR) && patch -p1) < $(PATCH_FILE)
	cp -R guarana-kaffe $(KAFFE_DIR)/kaffe/kaffevm/guarana/
	echo patched > $@

.PHONY: clean
clean:
	if [ -f $(KAFFE_DIR)/Makefile ]; then \
	    cd $(KAFFE_DIR) && make clean; \
	fi

.PHONY: distclean
distclean:
	-rm -f patch-stmp decompress-stmp
	-rm -rf $(KAFFE_DIR)


# to be used by the maintainer only

CVS=cvs

GUARANA_DIR = guarana-$(GUARANA_VERSION)
DIST_TAR_FILE = $(GUARANA_DIR).tar.gz

.PHONY: dist maintclean
dist: $(DIST_TAR_FILE) create-$(KAFFE_TGZ) create-$(KAFFE_OLD_TGZ)

maintclean:
	rm -f *.tar.gz *.patch

$(DIST_TAR_FILE): create-$(PATCH_FILE) create-$(OLD_PATCH_FILE)
	dir=`pwd` && cd /tmp && mkdir guarana-$$$$ && cd guarana-$$$$ && \
	$(CVS) -q export -r guarana_`echo $(GUARANA_VERSION) | sed 's/\./-/g'` -d $(GUARANA_DIR) guarana && \
	cp $$dir/$(PATCH_FILE) $(GUARANA_DIR) && \
	{ test -z "$(KAFFE_OLD_VERSION)" || \
	  cp $$dir/$(OLD_PATCH_FILE) $(GUARANA_DIR); } && \
	tar -cf - $(GUARANA_DIR) | gzip -9c > $$dir/$@ && \
	cd /tmp && rm -rf guarana-$$$$

.PHONY: create-$(PATCH_FILE) guarana-diff
create-$(PATCH_FILE) guarana-diff:
	test -f $(PATCH_FILE) || \
	$(CVS) -q rdiff -u -r kaffe_`echo $(KAFFE_VERSION) | sed 's/\./-/g'`$(KAFFE_SNAPSHOT_SUFFIX) -r guarana_`echo $(GUARANA_VERSION)$(GUARANA_KAFFE_BRANCH) | sed 's/\./-/g'` kaffe > $(PATCH_FILE)

.PHONY: create-$(KAFFE_TGZ) kaffe-dist
create-$(KAFFE_TGZ) kaffe-dist:
	if test ! -f $(KAFFE_TGZ); then \
	  dir=`pwd` && cd /tmp && mkdir guarana-$$$$ && cd guarana-$$$$ && \
	  { test -z "$(KAFFE_SNAPSHOT_SUFFIX)" || \
	    $(CVS) -q export -r kaffe_`echo $(KAFFE_VERSION) | sed 's/\./-/g'`$(KAFFE_SNAPSHOT_SUFFIX) -d $(KAFFE_DIR) kaffe && \
	      tar -cf - $(KAFFE_DIR) | gzip -9c > $$dir/$(KAFFE_TGZ); } && \
	  cd /tmp && rm -rf guarana-$$$$; \
	fi

.PHONY: create-$(OLD_PATCH_FILE) guarana-old-diff
create-$(OLD_PATCH_FILE) guarana-old-diff:
	@test -z "$(KAFFE_OLD_VERSION)" || \
	$(MAKE) $(FLAGS_FOR_OLD) guarana-diff

.PHONY: create-$(KAFFE_OLD_TGZ)
create-$(KAFFE_OLD_TGZ) kaffe-old-dist:
	@test -z "$(KAFFE_OLD_VERSION)" || \
	$(MAKE) $(FLAGS_FOR_OLD) kaffe-dist

.PHONY: old-flags
old-flags:
	@echo $(FLAGS_FOR_OLD)

# FORCE = -F

.PHONY: tag tag-current tag-old
tag: tag-current tag-old

tag-current:
	$(CVS) -q rtag $(FORCE) guarana_`echo $(GUARANA_VERSION) | sed 's/\./-/g'` guarana kaffe

tag-old:
	test -z "$(KAFFE_OLD_VERSION)" || $(CVS) -q rtag $(FORCE) -r kaffe_`echo $(KAFFE_OLD_VERSION) | sed 's/\./-/g'`_guarana_branch guarana_`echo $(GUARANA_VERSION) | sed 's/\./-/g'`$(GUARANA_KAFFE_OLD_BRANCH) kaffe
