
FORTRAN = gfortran
SRC = mtx09.pas add.pas opr.pas private.pas
INC = add.inc sub.inc mul.inc dvd.inc opr.inc private.inc mtx09.inc
FPCOPT = -gl -dMATH

.PHONY: all
all: mtx09.o mtx09.ppu unitpath

# kludge: cmd to retrieve the current system unit path
# fpc -h | head -n1 | cut -d \  -f 5,8
# may also be a way but the target is not consistent with the
# actual path: i386 instead of linux-i386 and so on...
unitpath: unitpath.pas
	fpc unitpath

spline.o: spline.f
	$(FORTRAN) -c spline.f

# macro code factorisation
$(INC): $(SRC)
	./inc

mtx09.o mtx09.ppu: $(INC) spline.o
	fpc $(FPCOPT) mtx09.pas

.PHONY: clean
clean:
	rm -f *~
	rm -f $(INC)

# NOTE unitpath binary is not removed because we need it to install
# so it is removed in archiv target ... a bit messy.
.PHONY: distclean
distclean: clean
	rm -f *.o *.ppu *.html lsp
	rm -rf ./test

# It is necessary to make into the install dir with proper suid
# hence the strange distclean and build after in situ...
# The installation dir is the standard freepascal unit directory
# returned by unitpath, so that no fiddling with fpc.cfg is needed.
# I consider that unitpath is a general utility and therefore it is
# copied in /usr/local/bin
.PHONY: install
install: distclean
	cp -f ./unitpath /usr/local/bin
	cp -R $$PWD `./unitpath`
	cd `./unitpath`/`basename $$PWD`;\
	make;\
	make clean;\
	cd -

# TEST INSTALLATION of unit mtx09 from an empty directory
# the code should compile and execute properly
.PHONY: test
test: p0.pas mtx09.o mtx09.ppu
	mkdir test;\
	cp p0.pas test/;\
	cd test;\
	fpc p0;\
	./p0;\
	cd -

# there is a dependency on some perl modules
# not easy to get on ubuntu karmic ? may be use another syntax highlighting
# engine with better LTS
.PHONY: doc
doc: add.inc sub.inc mul.inc dvd.inc mtx09.inc
	cd pasdoc/ex; make; cd ..;\
	./bin/pasdoc mtx09.pas;\
	cd -

.PHONY: archiv
archiv: distclean
	rm unitpath
	cd .. ; tar --exclude ".svn*"  -zcvf mtx09.tgz mtx09 ; cd -
