#!/usr/bin/make -f
# -*- makefile -*-

# SITE_CFLAGS=-fPIC -ffast-math -funsigned-char -mpowerpc -mpowerpc-gpopt -mpowerpc-gfxopt -maltivec -mabi=altivec -mtune=G4 -mcpu=G4 -pipe
# SITE_CFLAGS=-march=pentium4

VERSION=0.80
FARCH=$(shell sh debian/architecture.sh)

configure: configure-stamp

configure-stamp:
	dh_testdir
	touch configure-stamp

build: build-stamp

build-stamp: configure-stamp
	dh_testdir
	# Build the bootstrap image
	$(MAKE) linux-$(FARCH)
	mv f runtime
	echo 'USE: image "$(FARCH)" make-image' | ./runtime /usr/lib/factor/basic.image # || wget http://factorcode.org/boot.image.$(FARCH)
	mv boot.image.$(FARCH) boot.image
	# Build the factor image
	./runtime boot.image -libraries:sdl:name=libSDL.so -libraries:freetype:name=libfreetype.so
	mv factor.image basic.image
	# Build the extended image
	sqlite3 contrib/sqlite/test.db < contrib/sqlite/test.txt
	echo 'USING: image kernel ; "contrib/load.factor" run-file compile-all "extended.image" save-image 0 exit' | ./runtime basic.image +X8
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp
	-$(MAKE) clean
	-rm -f f runtime factor.image boot.image boot.image.$(FARCH) basic.image extended.image
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs
	# Install binary files
	install -m 755 -o root -g root runtime $(CURDIR)/debian/factor/usr/lib/factor
	install -m 644 -o root -g root boot.image basic.image extended.image $(CURDIR)/debian/factor/usr/lib/factor
	# Install source files
	cp -r contrib examples fonts library version.factor $(CURDIR)/debian/factor/usr/share/factor
	find $(CURDIR)/debian/factor/usr/share/factor -type d -exec chmod 755 {} \;
	find $(CURDIR)/debian/factor/usr/share/factor -type f -exec chmod 644 {} \;
	chown -R root.root $(CURDIR)/debian/factor/usr/share/factor
	# Install wrapper scripts
	install -m 755 -o root -g root debian/scripts/factor-* $(CURDIR)/debian/factor/usr/bin

binary-indep: build install

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs CHANGES.html
	dh_installdocs
	dh_installexamples
	dh_installman
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install configure
