#
# Copyright (c) 2000-2015, Toshimi Sawada. All rights reserved.
# Copyright (c) 2014-2015, Norbert Preining. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
#   * Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#
#   * Redistributions in binary form must reproduce the above
#     copyright notice, this list of conditions and the following
#     disclaimer in the documentation and/or other materials
#     provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

# stuff that is determined by configure:
# automatically
srcdir = @srcdir@
# --prefix
prefix = @prefix@
# --with-lisp
lisp = @lisp@
# determined first choice
firstchoice = @firstchoice@
# --enable-rebuild-doc
enable_rebuild_doc = @enable_rebuild_doc@
# --enable-traditional-layout
enable_traditional_layout = @enable_traditional_layout@
# --enable-windows
enable_windows = @enable_windows@
# --enable-distribution
enable_distribution = @enable_distribution@

# general stuff
PACKAGE = @PACKAGE_TARNAME@
VERSION = @PACKAGE_VERSION@
VMINOR  = @VMINOR@
PATCHLEVEL = @PATCHLEVEL@

INSTALL = @INSTALL@

# Engine definitions 
# interpreter cmd names can be configured, so are coming from
# configure(.ac)
ACL_DUMP          = dumps/acl/@acl_dump@
ACL_INTERPRETER   = @acl_interp@
ACL_BUILDOPTS     =
ACL_DUMPFILE      = make-cafeobj.lisp

ACL_STANDALONE_DUMP          = dumps/acl-standalone/@acl_standalone_dump@
ACL_STANDALONE_INTERPRETER   = @acl_interp@
ACL_STANDALONE_BUILDOPTS     =
ACL_STANDALONE_DUMPFILE      = build-acl-dist.cl

SBCL_DUMP         = dumps/sbcl/@sbcl_dump@
SBCL_INTERPRETER  = @sbcl_interp@
SBCL_BUILDOPTS    = --dynamic-space-size 2048
SBCL_DUMPFILE      = make-cafeobj.lisp

CMU_DUMP          = dumps/cmu/@cmu_dump@
CMU_INTERPRETER   = @cmu_interp@
CMU_BUILDOPTION   =
CMU_DUMPFILE      = make-cafeobj.lisp

CCL32_DUMP          = dumps/ccl/@ccl32_dump@
CCL32_INTERPRETER   = @ccl32_interp@
CCL32_BUILDOPTS     =
CCL32_DUMPFILE      = make-cafeobj.lisp

CCL64_DUMP        = dumps/ccl64/@ccl64_dump@
CCL64_INTERPRETER = @ccl64_interp@
CCL64_BUILDOPTS   =
CCL64_DUMPFILE    = make-cafeobj.lisp

CLISP_DUMP        = dumps/clisp/@clisp_dump@
CLISP_INTERPRETER = @clisp_interp@
CLISP_BUILDOPTS   =
CLISP_DUMPFILE    = make-cafeobj.lisp

GCL_DUMP          = dumps/gcl/@gcl_dump@
GCL_INTERPRETER   = @gcl_interp@
GCL_BUILDOPTS     =
GCL_DUMPFILE      = make-cafeobj.lisp
# End of engine definitions

#
# specifying the files that go into the distribution is a pain,
# because we cannot simply include * as this would include the
# to be build dist dir, too.
# for now, let us simply not suport make dist ;-)
# EXTRA_DIST = *

#
# traditional layout means
# /usr/local/bin/cafeobj
# /usr/local/cafeobj-N.M/bin/DUMP
# /usr/local/cafeobj-N.M/{lib,prelude}
ifeq ($(enable_traditional_layout),yes)
  # the first three variable will be prefixed with $DESTDIR/$prefix
  cafeobj_share = $(PACKAGE)-$(VERSION)
  cafeobj_lib   = $(PACKAGE)-$(VERSION)
  cafeobj_bin   = bin
  cafeobjdumpdir = $(cafeobj_lib)/bin
  cafeobjlibdir  = $(cafeobj_share)/lib
  cafeobjpreludedir = $(cafeobj_share)/prelude
  cafeobjdocdir = $(cafeobj_share)/doc
  cafeobjmandir = $(cafeobj_share)/man
else
  # the first three variable will be prefixed with $DESTDIR/$prefix
  cafeobj_share = share/$(PACKAGE)-$(VERSION)
  cafeobj_lib   = lib/$(PACKAGE)-$(VERSION)
  cafeobj_bin   = bin
  cafeobjdumpdir = $(cafeobj_lib)/
  cafeobjlibdir  = $(cafeobj_share)/lib
  cafeobjpreludedir = $(cafeobj_share)/prelude
  cafeobjdocdir = share/doc/$(PACKAGE)
  cafeobjmandir = share/man/man1
endif

ifeq ($(enable_windows),yes)
  prefix = $(srcdir)
  # we only support building 
  lisp = sbcl
  SBCL_INTERPRETER  = wine sbcl
  # it seems that windows sbcl does not like this cmd line args
  SBCL_BUILDOPTS    = 
  # the first three variable will be prefixed with $DESTDIR/$prefix
  cafeobj_share = $(PACKAGE)-$(VERSION)-sbcl
  cafeobj_lib   = $(PACKAGE)-$(VERSION)-sbcl
  cafeobj_bin   = $(PACKAGE)-$(VERSION)-sbcl
  cafeobjdumpdir = $(cafeobj_lib)/
  cafeobjlibdir  = $(cafeobj_share)/lib
  cafeobjpreludedir = $(cafeobj_share)/prelude
  cafeobjdocdir = $(cafeobj_share)/doc
  cafeobjmandir = $(cafeobj_share)/doc
endif

ifeq ($(enable_distribution),yes)
  prefix = $(srcdir)/dist
  # the first three variable will be prefixed with $DESTDIR/$prefix
  cafeobj_share = share/$(PACKAGE)-$(VERSION)
  cafeobj_lib   = lib/$(PACKAGE)-$(VERSION)
  cafeobj_bin   = bin
  cafeobjdumpdir = $(cafeobj_lib)/
  cafeobjlibdir  = $(cafeobj_share)/lib
  cafeobjpreludedir = $(cafeobj_share)/prelude
  cafeobjdocdir = share/doc/$(PACKAGE)
  cafeobjmandir = share/man/man1
endif


engine_dumps = $(patsubst acl,$(ACL_DUMP),\
               $(patsubst acl-standalone,$(ACL_STANDALONE_DUMP),\
               $(patsubst sbcl,$(SBCL_DUMP),\
	       $(patsubst cmu,$(CMU_DUMP),\
	       $(patsubst ccl,$(CCL32_DUMP),\
	       $(patsubst ccl64,$(CCL64_DUMP),\
	       $(patsubst clisp,$(CLISP_DUMP),\
	       $(patsubst gcl,$(GCL_DUMP),$(lisp)))))))))

# documentation rebuilding
doc_targets =
ifeq ($(enable_rebuild_doc),yes)
	doc_targets += \
		doc/refman/reference-manual.pdf		\
		doc/manual/manual.pdf			\
		doc/RefCard/interp.pdf			\
		doc/RefCard/syntax.pdf			\
		doc/PigNose/pnguide.pdf			\
		doc/citp-manual/citp.pdf
endif

#
# for dumping the image the lisp interpreter needs to know
# where the input files are during building
# used only in creating make-cafeobj.lisp script
chaos_root = $(srcdir)

# first target when building windows distribution
ifeq ($(enable_windows),yes)
make-dist: install-stamp
	zip -r cafeobj-$(VERSION)$(VMINOR)-sbcl-win32.zip $(cafeobj_bin)
endif

# first target when building binary distributions
ifeq ($(enable_distribution),yes)
make-dist: install-stamp
	bash make-release-tarballs $(VERSION) $(VMINOR) $(lisp)
endif

# first target on Unix!
build: build-stamp

build-stamp: version.lisp make-cafeobj.lisp $(engine_dumps) xbin/cafeobj $(doc_targets)
	touch build-stamp

#
# cafeobj <- here - cafeobj.in  <- cafeobj.in.in via configure
xbin/cafeobj: xbin/cafeobj.in
	cat $(srcdir)/xbin/cafeobj.in | \
	sed -e 's:@FIRSTCHOICE@:$(firstchoice):g' \
	-e 's:@LIBPATH@:$(cafeobj_lib):g' \
	-e 's:@SHAREPATH@:$(cafeobj_share):g' >$(srcdir)/xbin/cafeobj 

$(ACL_DUMP) : interp = $(ACL_INTERPRETER) 
$(ACL_DUMP) : buildopts = $(ACL_BUILDOPTS)
$(ACL_DUMP) : dumpfile = $(ACL_DUMPFILE)
$(ACL_STANDALONE_DUMP) : interp = $(ACL_STANDALONE_INTERPRETER) 
$(ACL_STANDALONE_DUMP) : buildopts = $(ACL_STANDALONE_BUILDOPTS)
$(ACL_STANDALONE_DUMP) : dumpfile = $(ACL_STANDALONE_DUMPFILE)
$(SBCL_DUMP) : interp = $(SBCL_INTERPRETER)
$(SBCL_DUMP) : buildopts = $(SBCL_BUILDOPTS)
$(SBCL_DUMP) : dumpfile = $(SBCL_DUMPFILE)
$(CMU_DUMP) : interp = $(CMU_INTERPRETER)
$(CMU_DUMP) : buildopts = $(CMU_BUILDOPTS)
$(CMU_DUMP) : dumpfile = $(CMU_DUMPFILE)
$(CLISP_DUMP) : interp = $(CLISP_INTERPRETER)
$(CLISP_DUMP) : buildopts = $(CLISP_BUILDOPTS)
$(CLISP_DUMP) : dumpfile = $(CLISP_DUMPFILE)
$(CCL32_DUMP) : interp = $(CCL32_INTERPRETER)
$(CCL32_DUMP) : buildopts = $(CCL32_BUILDOPTS)
$(CCL32_DUMP) : dumpfile = $(CCL32_DUMPFILE)
$(CCL64_DUMP) : interp = $(CCL64_INTERPRETER)
$(CCL64_DUMP) : buildopts = $(CCL64_BUILDOPTS)
$(CCL64_DUMP) : dumpfile = $(CCL64_DUMPFILE)
$(GCL_DUMP) : interp = $(GCL_INTERPRETER)
$(GCL_DUMP) : buildopts = $(GCL_BUILDOPTS)
$(GCL_DUMP) : dumpfile = $(GCL_DUMPFILE)

$(engine_dumps): %: make-cafeobj.lisp version.lisp
	mkdir -p $(dir $@)
	$(interp) $(buildopts) < $(dumpfile)
	chmod +x $@


install: install-stamp

install-stamp: build-stamp
	$(INSTALL) -d $(DESTDIR)$(prefix)/$(cafeobjlibdir)
	$(INSTALL) -m 0644 		\
			$(srcdir)/lib/lib/*.cafe	\
			$(DESTDIR)$(prefix)/$(cafeobjlibdir)
	$(INSTALL) -d $(DESTDIR)$(prefix)/$(cafeobjpreludedir)
	$(INSTALL) -m 0644 $(srcdir)/lib/prelude/*.bin 	\
			$(DESTDIR)$(prefix)/$(cafeobjpreludedir)
	$(INSTALL) -d $(DESTDIR)$(prefix)/$(cafeobjdumpdir)
ifeq ($(enable_windows),yes)
	cp -a $(SBCL_DUMP) $(DESTDIR)$(prefix)/$(cafeobjdumpdir)/CafeOBJ.exe
else
	cp -a dumps/* $(DESTDIR)$(prefix)/$(cafeobjdumpdir)
	chmod 0755 $(DESTDIR)$(prefix)/$(cafeobjdumpdir)/*/*
	$(INSTALL) -d $(DESTDIR)$(prefix)/$(cafeobj_bin)
	$(INSTALL) -m 0755 xbin/cafeobj $(DESTDIR)$(prefix)/$(cafeobj_bin)
endif
	# man page
	$(INSTALL) -d $(DESTDIR)$(prefix)/$(cafeobjmandir)
	$(INSTALL) -m 0644 doc/etc/cafeobj.1 $(DESTDIR)$(prefix)/$(cafeobjmandir)
	# doc files
	$(INSTALL) -d $(DESTDIR)$(prefix)/$(cafeobjdocdir)
	$(INSTALL) -m 0644 				\
		doc/manual/manual.pdf			\
		doc/PigNose/pnguide.pdf			\
		doc/RefCard/interp.pdf			\
		doc/RefCard/syntax.pdf			\
		doc/refman/reference-manual.pdf		\
		doc/citp-manual/citp.pdf		\
		doc/etc/cafe-citp.txt			\
		elisp/cafeobj-mode.el			\
		lib/icons/cafeobj-logo.png		\
		lib/icons/cafeobj-logo-small.png		\
		$(DESTDIR)$(prefix)/$(cafeobjdocdir)
	$(INSTALL) -d $(DESTDIR)$(prefix)/$(cafeobjdocdir)/examples
	$(INSTALL) -m 0644 doc/examples/*.mod 		\
		$(DESTDIR)$(prefix)/$(cafeobjdocdir)/examples
	touch install-stamp

doc/refman/reference-manual.pdf:
	$(MAKE) -C doc/refman reference-manual.pdf
doc/manual/manual.pdf:
	$(MAKE) -C doc/manual manual.pdf
doc/PigNose/pnguide.pdf:
	$(MAKE) -C doc/PigNose pnguide.pdf
doc/RefCard/interp.pdf:
	$(MAKE) -C doc/RefCard interp.pdf
doc/RefCard/syntax.pdf:
	$(MAKE) -C doc/RefCard syntax.pdf
doc/citp-manual/citp.pdf:
	$(MAKE) -C doc/citp-manual citp.pdf



clean:
	-test -r doc/refman/Makefile && $(MAKE) -C doc/refman clean
	-test -f doc/manual/Makefile && $(MAKE) -C doc/manual clean
	-test -f doc/PigNose/Makefile && $(MAKE) -C doc/PigNose clean
	-test -f doc/RefCard/Makefile && $(MAKE) -C doc/RefCard clean
	-test -f doc/citp-manual/Makefile && $(MAKE) -C doc/citp-manual clean
	rm -rf dumps
	rm -f stamp-windows-prepare stamp-windows-build
	rm -f build-stamp
	rm -f install-stamp
	rm -f xbin/cafeobj

distclean: clean
	rm -f Makefile config.log config.status
	rm -f xbin/cafeobj.in
	rm -f make-cafeobj.lisp
	rm -f version.lisp
	rm -rf autom4te.cache
	rm -f doc/Makefile doc/refman/Makefile doc/manual/Makefile
	rm -f doc/PigNose/Makefile doc/RefCard/Makefile
	rm -rf WindowsDist
	rm -rf dist
	rm -f autoloads.out
	find . -name \*.fasl -exec rm '{}' \;

#
# declare the clean targets as well as the scripts that are
# generated as phony to make sure they are rebuilt at any time.
.PHONY: clean distclean $(doc_targets)
