#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# Detect name of new compiler, take care that debian uses amd64 instead of x86_64
CPU_SOURCE := $(subst amd64,x86_64,$(shell dpkg-architecture -qDEB_BUILD_ARCH))
CPU_TARGET := $(subst amd64,x86_64,$(shell dpkg-architecture -qDEB_HOST_ARCH))
# Fixup armel, too
CPU_SOURCE := $(subst armel,arm,$(CPU_SOURCE))
CPU_TARGET := $(subst armel,arm,$(CPU_TARGET))

ifeq ($(CPU_TARGET),arm)
CFLAGS= -O3 -mcpu=cortex-a8 -mtune=cortex-a8 -fomit-frame-pointer -fsigned-char -mfpu=neon -mfloat-abi=softfp -DALIGN_INTS -DALIGN_SHORTS -DLSB_FIRST
endif
ifeq ($(CPU_TARGET),i386)
CFLAGS= -O2 -g -Wall
endif
#CFLAGS = -Wall -g -O3 -mcpu=arm1136j-s -mtune=arm1136j-s -fomit-frame-pointer -fsigned-char -mfpu=vfp -mfloat-abi=softfp -DALIGN_INTS -DALIGN_SHORTS -DLSB_FIRST

configure: configure-stamp
configure-stamp:
	dh_testdir
	# Add here commands to configure the package.
	CFLAGS="$(CFLAGS)" ./configure --prefix=/opt/x48/usr --disable-shm
	touch configure-stamp

build: build-stamp

build-stamp: configure-stamp 
	dh_testdir
	$(MAKE)
	#docbook-to-man debian/x48.sgml > x48.1

	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp

	# Add here commands to clean up after the build process.
	-$(MAKE) clean

	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs

	cd src
	# Add here commands to install the package into debian/x48.
	$(MAKE) install DESTDIR=$(CURDIR)/debian/x48
	
	# Portrait mode binary
	#cp $(CURDIR)/x48-portrait $(CURDIR)/debian/x48/opt/x48/usr/bin/
	
	# maemo stuff
	mkdir -p $(CURDIR)/debian/x48/opt/x48/usr/share/applications/hildon
	cp $(CURDIR)/x48.desktop $(CURDIR)/debian/x48/opt/x48/usr/share/applications/hildon
	#cp $(CURDIR)/x48v.desktop $(CURDIR)/debian/x48/opt/x48/usr/share/applications/hildon

	#Icones
	mkdir -p $(CURDIR)/debian/x48/opt/x48/usr/share/pixmaps
	cp $(CURDIR)/x48.png $(CURDIR)/debian/x48/opt/x48/usr/share/pixmaps/

	#Links simbolicos
	#checkrom  dump2rom  mkcard  x48  x48-portrait
	mkdir -p $(CURDIR)/debian/x48/usr/bin
	ln -sf /opt/x48/usr/bin/x48 $(CURDIR)/debian/x48/usr/bin/
	#ln -sf /opt/x48/usr/bin/x48-portrait $(CURDIR)/debian/x48/usr/bin/
	ln -sf /opt/x48/usr/bin/mkcard $(CURDIR)/debian/x48/usr/bin/
	ln -sf /opt/x48/usr/bin/checkrom $(CURDIR)/debian/x48/usr/bin/
	ln -sf /opt/x48/usr/bin/dump2rom $(CURDIR)/debian/x48/usr/bin/
	
	#x48.desktop   x48v.desktop
	mkdir -p $(CURDIR)/debian/x48/usr/share/applications/hildon
	mv $(CURDIR)/debian/x48/opt/x48/usr/share/applications/hildon/x48.desktop $(CURDIR)/debian/x48/usr/share/applications/hildon/
	#ln -sf /opt/x48/usr/share/applications/hildon/x48v.desktop $(CURDIR)/debian/x48/usr/share/applications/hildon/
	rm -rf $(CURDIR)/debian/x48/opt/x48/usr/share/applications
	
	mkdir -p $(CURDIR)/debian/x48/usr/share/pixmaps
	ln -sf /opt/x48/usr/share/pixmaps/x48.png $(CURDIR)/debian/x48/usr/share/pixmaps/

	#HP48 ROM/RAM
	mkdir -p $(CURDIR)/debian/x48/home/user/.hp48
	cp -r $(CURDIR)/ROM/rom.dump $(CURDIR)/debian/x48/home/user/.hp48/	

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs 
#	dh_installdocs
#	dh_installexamples
#	dh_install
#	dh_installmenu
#	dh_installdebconf	
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_installinit
#	dh_installcron
#	dh_installinfo
	dh_installman
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
#	dh_perl
#	dh_python
#	dh_makeshlibs
	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
