# tests
# + each package has a pack*.so file for dyn.load()
# + pack*s packages have additionally a static library
#   libpack*.a
# + pack*d packages have additionally a dynamic library
#   (symlink) libpack*.so            
# + the pack*m example mixes dynamic and sstatic linking
#
# simple example (desired use):
# pack2d uses pack3d (dynamically)
# pack2m uses pack3s (statically), (the same way as pack2s 
# uses pack3s)
# 
#
# link graph: 
#
# +-pack2d-------+          +-pack3d-------+
# |              |          |              |
# | pack2d.so    |--\       | pack3d.so    |
# |              |   d      |              |
# | libpack2d.so |    \---> | libpack3d.so |
# |              |          |              |
# | func2()      |          | func3()      |
# +--------------+          +--------------+
#
# +-pack2m-------+          +-pack3s-------+
# |              |          |              |
# | pack2m.so    |--\       | pack3s.so    |
# |              |   s      |              |
# | libpack2m.so |    \---> | libpack3s.a  |
# |              |          |              |
# | func2        |          | func3        |
# +--------------+          +--------------+
#
#
# complex example:
# - pack1? reimplements function func3() from pack3 (duplicate symbol)
# - pack2s is installed twice (duplicate package)
# - this is an example how to misuse this approach! 
#   it generates different results on different systems
#
# pack1d uses pack2d (dynamically), pack2d uses pack3d (dynamically).
# pack1s uses pack2s (static), pack2s uses pack3s (static).
# pack1m uses pack2m (dynamically), pack2m uses pack3s (static).
# 
# 
# link graph: 
#
# +-pack1d-------+          +-pack2d-------+          +-pack3d-------+
# |              |          |              |          |              |
# | pack1d.so    |--\       | pack2d.so    |--\       | pack3d.so    |
# |              |   d      |              |   d      |              |
# |              |    \---> | libpack2d.so |    \---> | libpack3d.so |
# |              |          |              |          |              |
# | func1,func3  |          | func2        |          | func3        |
# +--------------+          +--------------+          +--------------+
#
#
# +-pack1m-------+          +-pack2m-------+          
# |              |          |              |         
# | pack1m.so    |--\       | pack2m.so    |--\      
# |              |   d      |              |   s     
# |              |    \---> | libpack2m.so |    \ 
# |              |          |              |     | 
# | func1,func3  |          | func2        |     |
# +--------------+          +--------------+     |
#                                                |
#                                                |
# +-pack1s-------+          +-pack2s-------+     |    +-pack3s-------+
# |              |          |              |     |    |              |
# | pack1s.so    |--\       | pack2s.so    |--\  |    | pack3s.so    |
# |              |   s      |              |   s |    |              |
# |              |    \---> | libpack2s.a  |    \---> | libpack3s.a  |
# |              |          |              |          |              |
# | func1,func3  |          | func2        |          | func3        |
# +--------------+          +--------------+          +--------------+
#

# mix some paths to check what happens with multiple installations:
R_LIB1=/tmp/R-Libs1
R_LIB2=/tmp/R-Libs2
R_LIBS=~/R-Libs:$(R_LIB1):$(R_LIB2)

PACKS=pack3d pack2d pack1d pack3s pack2s pack1s pack2m pack1m 
INSTPACKS=inst-pack3d inst-pack2d inst-pack1d inst-pack3s inst-pack2s inst-pack1s inst-pack2m inst-pack1m 

all: dir cleanall cleanman man prep edit packs test

dir:
	mkdir -p $(R_LIB1)
	mkdir -p $(R_LIB2)
packs: $(INSTPACKS)

inst-pack1d: inst-pack2d
	R_LIBS=$(R_LIBS) R INSTALL -l $(R_LIB1) pack1d

inst-pack2d: inst-pack3d
	R_LIBS=$(R_LIBS) R INSTALL -l $(R_LIB1) --create-lib-so pack2d
	R_LIBS=$(R_LIBS) R INSTALL -l $(R_LIB2) pack2d

inst-pack3d: 
	R_LIBS=$(R_LIBS) R INSTALL -l $(R_LIB2) --create-lib-so pack3d

inst-pack1s: inst-pack2s
	R_LIBS=$(R_LIBS) R INSTALL -l $(R_LIB1) pack1s

inst-pack2s: inst-pack3s
	R_LIBS=$(R_LIBS) R INSTALL -l $(R_LIB1) --create-lib-a pack2s
	R_LIBS=$(R_LIBS) R INSTALL -l $(R_LIB2) pack2s

inst-pack3s: 
	R_LIBS=$(R_LIBS) R INSTALL -l $(R_LIB2) --create-lib-a pack3s

inst-pack1m: inst-pack2m
	R_LIBS=$(R_LIBS) R INSTALL -l $(R_LIB1) pack1m

inst-pack2m: inst-pack3s
	R_LIBS=$(R_LIBS) R INSTALL -l $(R_LIB1) --create-lib-so pack2m

test: test1s.out test2s.out test3s.out test1d.out test3d.out test1m.out \
test1m.out test2m.out
	cat test??.out >test.out-`uname -m`-`uname -s`-`uname -r`

test1s.out:
	R_LIBS=$(R_LIBS) R --vanilla 2>&1 >test1s.out <test1s.R || echo failed

test2s.out:
	R_LIBS=$(R_LIBS) R --vanilla 2>&1 >test2s.out <test2s.R || echo failed
	
test3s.out:
	R_LIBS=$(R_LIBS) R --vanilla 2>&1 >test3s.out <test3s.R || echo failed
	
test1d.out:
	R_LIBS=$(R_LIBS) R --vanilla 2>&1 >test1d.out <test1d.R || echo failed

test2d.out:
	R_LIBS=$(R_LIBS) R --vanilla 2>&1 >test2d.out <test2d.R || echo failed
	
test3d.out:
	R_LIBS=$(R_LIBS) R --vanilla 2>&1 >test3d.out <test3d.R || echo failed
	
test1m.out:
	R_LIBS=$(R_LIBS) R --vanilla 2>&1 >test1m.out <test1m.R || echo failed

test2m.out:
	R_LIBS=$(R_LIBS) R --vanilla 2>&1 >test2m.out <test2m.R || echo failed

prep:
	R CMD build --makevars --force pack1d
	R CMD build --makevars --force pack2d
	R CMD build --force pack3d
	R CMD build --makevars --force pack1s
	R CMD build --makevars --force pack2s
	R CMD build --force pack3s
	R CMD build --makevars --force pack1m
	R CMD build --makevars --force pack2m
	echo 
	echo edit now configure.in in pack2? and pack3?
	echo or call make edit

edit: 
	cd pack1d; mv configure.in configure.in.bak; \
cat configure.in.bak | \
sed -e 's/#R_FIND_LIB/R_FIND_LIB/' | \
sed -e 's/somepack/pack2d/' > configure.in; \
autoconf 
	cd pack2d; mv configure.in configure.in.bak; \
cat configure.in.bak | \
sed -e 's/#R_FIND_LIB/R_FIND_LIB/' | \
sed -e 's/somepack/pack3d/' > configure.in; \
autoconf 
	cd pack1s; mv configure.in configure.in.bak; \
cat configure.in.bak | \
sed -e 's/#R_FIND_LIB/R_FIND_LIB/' | \
sed -e 's/somepack/pack2s/' > configure.in; \
autoconf 
	cd pack2s; mv configure.in configure.in.bak; \
cat configure.in.bak | \
sed -e 's/#R_FIND_LIB/R_FIND_LIB/' | \
sed -e 's/somepack/pack3s/' > configure.in; \
autoconf 
	cd pack1m; mv configure.in configure.in.bak; \
cat configure.in.bak | \
sed -e 's/#R_FIND_LIB/R_FIND_LIB/' | \
sed -e 's/somepack/pack2m/' > configure.in; \
autoconf 
	cd pack2m; mv configure.in configure.in.bak; \
cat configure.in.bak | \
sed -e 's/#R_FIND_LIB/R_FIND_LIB/' | \
sed -e 's/somepack/pack3s/' > configure.in; \
autoconf 

clean:
	rm -f *~ pack*/src/*.o pack*/*.bak pack*/src/*.bak *.tar.gz \
pack*/config.status pack*/config.cache pack*/config.log pack*/confdef.h \
*.out pack*/src/so_locations

cleanall: clean
	rm -f pack*/conf* pack*/ac* pack*/src/Make* pack*/install-sh pack*/src/*.so

man:
	for i in $(PACKS); do \
cd $$i/man; for j in ../R/func*.R; do \
f=`echo $$j | sed -e 's/\.\.\/R\///' | sed -e 's/\.R//'`; \
echo "source(\"$$j\"); prompt($$f);" | \
R --vanilla >/dev/null; \
done; \
cd ../..; \
done

cleanman:
	rm -f pack*/man/*.Rd

PHONY: pack1d pack2d pack3d pack1s pack2s pack3s pack1m pack2m dir \
prep test1 test2 test3 edit
