#
# Copyright (c) 2000-2018, Toshimi Sawada. All rights reserved.
# Copyright (c) 2014-2018, 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@
exec_prefix = @exec_prefix@
# needed for automake compatibility
datarootdir = @datarootdir@
datadir = @datadir@
mandir = @mandir@
docdir = @docdir@
libdir = @libdir@
bindir = @bindir@
# --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@
# lispdir of emacs
lispdir = @lispdir@

OSBIT = @OSBIT@

# 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@
ifdef NO_SBCL_BUILDOPTS
SBCL_BUILDOPTS    =
else
ifeq ($(OSBIT),64)
SBCL_BUILDOPTS    = --dynamic-space-size 4096
else
SBCL_BUILDOPTS    = --dynamic-space-size 2048
endif
endif
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)
  cafeobj_share = $(prefix)/$(PACKAGE)-$(VERSION)
  cafeobj_lib   = $(prefix)/$(PACKAGE)-$(VERSION)
  cafeobj_bin   = $(prefix)/bin
  lib_relative  = .
  share_relative  = .
  cafeobjdumpdir = $(cafeobj_lib)/bin
  cafeobjlibdir  = $(cafeobj_share)/lib
  cafeobjpreludedir = $(cafeobj_share)/prelude
  cafeobjdocdir = $(cafeobj_share)/doc
  cafeobjmandir = $(cafeobj_share)/man
  cafeobjelisp =  $(cafeobj_share)/elisp
else
  cafeobj_share = $(datarootdir)/$(PACKAGE)-$(VERSION)
  cafeobj_lib   = $(libdir)/$(PACKAGE)-$(VERSION)
  cafeobj_bin   = $(bindir)
  lib_relative  = lib/$(PACKAGE)-$(VERSION)
  share_relative  = share/$(PACKAGE)-$(VERSION)
  cafeobjdumpdir = $(cafeobj_lib)
  cafeobjlibdir  = $(cafeobj_share)/lib
  cafeobjpreludedir = $(cafeobj_share)/prelude
  cafeobjdocdir = $(docdir)/cafeobj
  cafeobjmandir = $(mandir)/man1
  cafeobjelisp =  $(lispdir)
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    = 
  cafeobj_share = $(prefix)/$(PACKAGE)-$(VERSION)-sbcl
  cafeobj_lib   = $(prefix)/$(PACKAGE)-$(VERSION)-sbcl
  cafeobj_bin   = $(prefix)/$(PACKAGE)-$(VERSION)-sbcl
  # irrelevant, since we do not install the shell script
  lib_relative  = .
  share_relative  = .
  cafeobjdumpdir = $(cafeobj_lib)
  cafeobjlibdir  = $(cafeobj_share)/lib
  cafeobjpreludedir = $(cafeobj_share)/prelude
  cafeobjdocdir = $(cafeobj_share)/doc
  cafeobjmandir = $(cafeobj_share)/doc
  cafeobjelisp =  $(cafeobj_share)/elisp
endif

ifeq ($(enable_distribution),yes)
  prefix = $(srcdir)/dist
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)))))))))

doc_files = \
		doc/binspect/binspect.pdf		\
		doc/citp-manual/citp.pdf		\
		doc/manual/manual.pdf			\
		doc/namespace/namespace.pdf		\
		doc/PigNose/pnguide.pdf			\
		doc/RefCard/interp.pdf			\
		doc/RefCard/syntax.pdf			\
		doc/refman/reference-manual.pdf		\
		doc/search/search-predicate.pdf

# documentation rebuilding
doc_targets =
ifeq ($(enable_rebuild_doc),yes)
	doc_targets += $(doc_files)
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@:$(lib_relative):g' \
	-e 's:@SHAREPATH@:$(share_relative):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)$(cafeobjlibdir)
	$(INSTALL) -m 0644 		\
			$(srcdir)/lib/lib/*.cafe	\
			$(DESTDIR)$(cafeobjlibdir)
	$(INSTALL) -d $(DESTDIR)$(cafeobjpreludedir)
	$(INSTALL) -m 0644 $(srcdir)/lib/prelude/*.bin 	\
			$(DESTDIR)$(cafeobjpreludedir)
	$(INSTALL) -d $(DESTDIR)$(cafeobjdumpdir)
ifeq ($(enable_windows),yes)
	cp -a $(SBCL_DUMP) $(DESTDIR)$(cafeobjdumpdir)/CafeOBJ.exe
else
	cp -a dumps/* $(DESTDIR)$(cafeobjdumpdir)
	chmod 0755 $(DESTDIR)$(cafeobjdumpdir)/*/*
	$(INSTALL) -d $(DESTDIR)$(cafeobj_bin)
	$(INSTALL) -m 0755 xbin/cafeobj $(DESTDIR)$(cafeobj_bin)
endif
	# man page
	$(INSTALL) -d $(DESTDIR)$(cafeobjmandir)
	$(INSTALL) -m 0644 doc/etc/cafeobj.1 $(DESTDIR)$(cafeobjmandir)
	# doc files
	$(INSTALL) -d $(DESTDIR)$(cafeobjdocdir)
	$(INSTALL) -m 0644 				\
		$(doc_files)				\
		doc/etc/cafe-citp.txt			\
		elisp/cafeobj-mode.el			\
		lib/icons/cafeobj-logo.png		\
		lib/icons/cafeobj-logo-small.png		\
		$(DESTDIR)$(cafeobjdocdir)
	$(INSTALL) -d $(DESTDIR)$(cafeobjdocdir)/examples
	$(INSTALL) -m 0644 doc/examples/*.mod 		\
		$(DESTDIR)$(cafeobjdocdir)/examples
	# emacs lisp file installation
	$(INSTALL) -d $(DESTDIR)$(cafeobjelisp)
	$(INSTALL) -m 0644 elisp/cafeobj-mode.el $(DESTDIR)$(cafeobjelisp)
	# end of installation routines

	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/namespace/namespace.pdf:
	$(MAKE) -C doc/namespace namespace.pdf
doc/citp-manual/citp.pdf: doc/etc/kosakusha-logo-gray.png
	$(MAKE) -C doc/citp-manual citp.pdf
doc/binspect/binspect.pdf: doc/etc/kosakusha-logo-gray.png
	$(MAKE) -C doc/binspect binspect.pdf
doc/etc/kosakusha-logo-gray.png:
	$(MAKE) -C doc/etc kosakusha-logo-gray.png

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/namespace/Makefile && $(MAKE) -C doc/namespace clean
	-test -f doc/binspect/Makefile && $(MAKE) -C doc/binspect clean
	-test -f doc/citp-manual/Makefile && $(MAKE) -C doc/citp-manual clean
	-test -f doc/etc/Makefile && $(MAKE) -C doc/etc 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/etc/Makefile doc/namespace/Makefile doc/search/Makefile
	rm -f doc/PigNose/Makefile doc/RefCard/Makefile
	rm -f doc/citp-manual/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)
