# Gforth integration Makefile

# Copyright (C) 2015   Bernd Paysan

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 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 Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

package = $(DESTDIR)
VPATH = @srcdir@:.
prefix = @prefix@
exec_prefix = @exec_prefix@
datarootdir = @datarootdir@
datadir   = $(package)@datadir@
srcdir    = @srcdir@
bindir    = $(package)@bindir@
libdir = $(package)@libdir@
LIBCCDEST = $(libdir)/gforth/$(GFVER)/$(machine)/libcc-named
libccdir = $(subst $(package),,$(LIBCCDEST))
LIBS 	 = keccak threefish ed25519-donna bdelta
MKDIR	 = mkdir
RMDIR	 = rm -rf
GCC	 = gcc
LIBTOOL	 = @GNU_LIBTOOL@
CFLAGS	 = -O3 -fomit-frame-pointer
HOST     = 
FAST     = @FAST@
FORTHLIB = ed25519-donna.fs keccak.fs threefish.fs bdelta.fs

SOURCES = 64bit.fs base64.fs base85.fs crypto-api.fs debugging.fs	     \
	  hash-table.fs keccak-small.fs kregion.fs n2o.fs addr.fs classes.fs \
	  cmd.fs connected.fs connect.fs crypt.fs dht.fs err.fs file.fs	     \
	  gui.fs net2o.fs ip.fs helper.fs keys.fs log.fs msg.fs notify.fs    \
	  qr.fs socks.fs squid.fs tools.fs vault.fs rng.fs xtype.fs $(TESTS) \
	  $(FORTHLIB) ed25519-donnalib.fs keccaklib.fs threefishlib.fs	     \
	  startn2o.fs doc/net2o-logo.png version.fs.in android/net.fs	     \
	  android/notify.fs linux/net.fs linux/notify.fs dhtroot.fs dvcs.fs  \
	  qrscan.fs android/qrscan-android.fs linux/qrscan-linux.fs	     \
	  doc/net2o-200.png doc/net2o.png json/parser.fs json/g+-schema.fs   \
	  json/g+-import.fs json/fb-schema.fs json/twitter-schema.fs	     \
	  json/test.fs json/test.json html/parser.fs xml/parser.fs	     \
	  xml/blogger-atom.fs

LANGS =	  lang/en lang/de lang/zh

ifeq "$(FAST)" "yes"
FORTHLIB += ed25519-donnafast.fs keccakfast.fs threefishfast.fs
else
SOURCES += ed25519-donnafast.fs keccakfast.fs threefishfast.fs
endif
SRC      = .
ENGINE   = @GFORTH@
GFORTH   = $(basename $(ENGINE))
GFVER  = @GFVER@
machine  = @GFARCH@
LIBCCNAMED = lib/$(notdir $(GFORTH))/$(GFVER)/$(machine)/libcc-named
PREFIX    = $(package)@prefix@
INCDIR    = $(PREFIX)/include/$(GFVER)
INSTALL	= @INSTALL@
INSTALL_DATA = @INSTALL_DATA@
INSTALL_SCRIPT = @INSTALL_SCRIPT@
ARCH	  =
DEBARCH   =
GFORTH_FAST = `which gforth-fast`-`gforth-fast --version 2>&1| cut -f2- -d' ' | tr ' ' '-'`

INS = Makefile n2o qrscan version.fs debian/changelog debian/control

TESTS = tests/alice2-msg.fs tests/alice-msg.fs tests/bob-msg.fs		\
	  tests/copy.fs tests/cryptspeed.fs tests/dht.fs		\
	  tests/dht-pop.fs tests/ed25519.fs tests/insdeltest.fs		\
	  tests/keccak.fs tests/keys.fs tests/msg.fs			\
	  tests/teststat.fs tests/threefish.fs tests/vault.fs		\
	  tests/ed25519-table.fs tests/alice-test.fs			\
	  tests/bob-test.fs tests/eve-test.fs tests/client-test.fs	\
	  tests/client-tests.fs tests/server-test.fs			\
	  tests/terminal-test.fs tests/test-keys.fs tests/dvcs-test	\
	  tests/dvcs-test.s2o tests/dvcs-test2.s2o			\
	  tests/termclient.fs tests/msg-test

@VERDIR@SRCDIRS = net2o-@PACKAGE_VERSION@/tests net2o-@PACKAGE_VERSION@/doc net2o-@PACKAGE_VERSION@/android net2o-@PACKAGE_VERSION@/linux net2o-@PACKAGE_VERSION@/lang net2o-@PACKAGE_VERSION@/json net2o-@PACKAGE_VERSION@/html
@NOVERDIR@SRCDIRS = net2o/tests net2o/doc net2o/android net2o/linux net2o/lang net2o/json net2o/html

SCRIPTS = n2o

GEN = version.fs

DOC = wiki/commands.md

all: no-config TAGS doc

distfiles:
	@echo $(SOURCES) $(LANGS) $(GEN) $(SCRIPTS) $(INS) $(INS:%=%.in)

no-config: libs $(GEN)

clean:  $(LIBS:%=%.clean)

%.clean:
	cd $(@:%.clean=%); $(MAKE) clean

configs $(CONFIGS:%.in=%):  $(LIBS:%=%.configs) $(CONFIGS)
	./configure --host=$(HOST) --prefix=$(PREFIX) 

%.configs:
	cd $(@:%.configs=%); ./autogen.sh CFLAGS="$(subst O2,O3,$(CFLAGS))" --host=$(HOST) --prefix=$(PREFIX) && $(MAKE) clean

libs:	$(LIBS:%=%.libs)

%.libs:
	cd $(@:%.libs=%); $(MAKE) all

install-libs: $(LIBS:%=%.instlibs)

%.instlibs:
	cd $(@:%.instlibs=%); $(MAKE) package=$(package) install

extra-install:	install-libs install

install:	libcc-install
	for i in $(SRCDIRS); do \
		$(MKDIR) -p $(datadir)/gforth/$(GFVER)/$$i; \
	done
	-@VERDIR@rm $(datadir)/gforth/$(GFVER)/net2o
	@VERDIR@ln -fs net2o-@PACKAGE_VERSION@ $(datadir)/gforth/$(GFVER)/net2o
	for i in $(SOURCES) $(LANGS) $(GEN); do \
		$(INSTALL_DATA) ./$$i $(datadir)/gforth/$(GFVER)/net2o/$$i; \
	done
	$(MKDIR) -p $(bindir)
	@VERDIR@for i in $(SCRIPTS); do \
	@VERDIR@	$(INSTALL_SCRIPT) ./$$i $(bindir)/$$i-@PACKAGE_VERSION@; \
	@VERDIR@	ln -fs $$i-@PACKAGE_VERSION@ $(bindir)/$$i; \
	@VERDIR@done
	@NOVERDIR@for i in $(SCRIPTS); do \
	@NOVERDIR@	$(INSTALL_SCRIPT) ./$$i $(bindir)/$$i; \
	@NOVERDIR@done

%.dirui:
	$(RMDIR) $(datadir)/gforth/$(GFVER)/$(@:%.dirui=%); \

%.scriptui:
	-@VERDIR@$(RM) $(bindir)/$(@:%.scriptui=%)-@PACKAGE_VERSION@; \
	$(RM) $(bindir)/$(@:%.scriptui=%); \

uninstall:	libcc-uninstall $(SRCDIRS:%=%.dirui) $(SCRIPTS:%=%.scriptui)
	-@VERDIR@rm $(datadir)/gforth/$(GFVER)/net2o

libcc:	$(FORTHLIB:%=%.libcc)

%.libcc:
	-echo "generating library $(@:%.libcc=%)"
	-$(ENGINE) unix/cpu.fs -e "also c-lib s\" $(LIBCCNAMED)/\" >libcc-named-dir libcc-path clear-path libcc-named-dir libcc-path also-path :noname 2drop s\" $(libccdir)/\" ; is replace-rpath previous cpuflags off" $(@:%.libcc=%) -e bye

net2o.fi:	$(SOURCES) $(FORTHLIB)
	GFORTH=$(GFORTH_FAST) gforthmi net2o.fi n2o.fs -e 'locs[] $$free wheres $$free'

libccdest:
	$(MKDIR) -p $(LIBCCDEST)

libcc-install:	libcc libccdest
	if test -n "$(LIBTOOL)"; then for i in $(subst -,_,$(FORTHLIB)); do \
		   $(LIBTOOL) --silent --mode=install $(INSTALL) $(LIBCCNAMED)/libgf`basename $$i .fs`.la $(LIBCCDEST)/libgf`basename $$i .fs`.la; \
		done; fi

libcc-uninstall:
	if test -n "$(LIBTOOL)"; then for i in $(subst -,_,$(FORTHLIB)); do \
		   $(LIBTOOL) --silent --mode=uninstall $(RM) $(LIBCCDEST)/libgf`basename $$i .fs`.*; \
		done; fi

doc:	$(DOC)

wiki/commands.md:	$(SOURCES)
	gforth -e ': docgen ;' n2o.fs -e bye | sed -e 's/@VERSION@/@PACKAGE_VERSION@/g' >wiki/commands.md

TAGS:	$(SOURCES) $(GEN)
	gforth etags.fs n2o.fs -e "bye" || >TAGS
	mv TAGS net2o.TAGS
	cat `gforth -e '"TAGS" open-fpath-file throw type bye'` net2o.TAGS >TAGS

configure: configure.ac
	cd $(srcdir) && autoreconf --force --verbose $(srcdir)

$(INS):	config.status $(INS:%=%.in)
	./config.status

config.status: configure
	./config.status --recheck

debdist:
	PATH=/usr/bin:/bin dpkg-buildpackage -us -uc -d -b $(DEBARCH)

debdist32:
	PATH=/usr/bin:/bin GFORTH=gforth-386 CC="gcc -m32" dpkg-buildpackage -us -uc -d -b -ai386
