#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

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

include /usr/share/dpatch/dpatch.make

#specify the changeable names here...

LIBECASOUND=libecasound7
LIBECASOUNDDEV=libecasound2.2-dev
LIBKVUTILS=libkvutils2
LIBKVUTILSDEV=libkvutils2.2-dev
LIBECASOUNDC=libecasoundc0
LIBECASOUNDCDEV=libecasoundc2.2-dev
PYTHONECASOUND=python-ecasound2.2
RUBYECASOUND=libecasound-ruby1.8
ECASOUND=ecasound

CONFIGURE_OPTIONS=--prefix=/usr --mandir=/usr/share/man --enable-pyecasound=python --enable-python-force-site-packages --enable-sys-readline --with-largefile --datadir=/usr/share/ecasound2.2 --disable-shared 

ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
# debug is found... debug version
CONFIGURE_OPTIONS+= --enable-debug=yes
else
# debug is not found, do not need to do anything really.
#CONFIGURE_OPTIONS+=--enable-debug=no # there was a bug in configure.in
endif


COMPILER_FLAGS=CXX=g++ CC=gcc

COMPILER_FLAGS+= CFLAGS="-g -O2 -D_REENTRANT -DNDEBUG -ffast-math -fstrict-aliasing -funroll-loops" CXXFLAGS="-g -O2 -D_REENTRANT -DNDEBUG -ffast-math -fstrict-aliasing -funroll-loops"

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
  COMPILER_FLAGS+= CFLAGS="-O0 -D_REENTRANT" CXXFLAGS="-O0 -D_REENTRANT"
endif

#  RUBY related configs.
RUBYVER=1.8
RUBY_FLAGS= ECA_S_RUBY_SITEDIR="/usr/lib/ruby/${RUBYVER}/"

configure: configure-stamp
configure-stamp: patch-stamp
	dh_testdir
	libtoolize --force
	aclocal-1.7
	autoheader
	autoconf
	automake-1.7 --add-missing
	( ${COMPILER_FLAGS} ./configure ${CONFIGURE_OPTIONS} )
	touch configure-stamp

build: configure-stamp build-stamp
build-stamp:
	dh_testdir
	$(MAKE)
	$(MAKE) -C Documentation docs 
	touch build-stamp

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

	# Add here commands to clean up after the build process.
	-$(MAKE) clean
	-$(MAKE) -C Documentation clean
	-$(MAKE) -C Documentation clean-docs
	-$(MAKE) -C Documentation/programmers_guide clean-docs
	-$(MAKE) -C Documentation/users_guide clean-docs
	-$(MAKE) -C Documentation distclean
	-$(MAKE) distclean
	-rm depcomp
	-rm -f aclocal.m4 configure stamp-h.in config.guess config.sub ltmain.sh readline-4.0/config.log readline-4.0/config.status Makefile
	-for a in . Documentation/users_guide Documentation/programmers_guide Documentation contrib ecasound ecatools examples kvutils libecasound libecasound/plugins libecasoundc pyecasound ; do rm -f $$a/Makefile.in; done 
	-rm -f Documentation/users_guide/users_guide.dvi Documentation/users_guide/ecasound_users_guide.pdf Documentation/programmers_guide/programmers_guide.dvi Documentation/programmers_guide/ecasound_programmers_guide.pdf Documentation/programmers_guide/eci_doc.dvi Documentation/programmers_guide/ecasound_eci_doc.pdf

	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs 

	# Add here commands to install the package into debian/tmp.
	# $(MAKE) install prefix=`pwd`/debian/tmp/usr 
	$(MAKE) install DESTDIR=`pwd`/debian/${ECASOUND} ${RUBY_FLAGS}
	for A in a la ; do \
	rm -f `pwd`/debian/${ECASOUND}/usr/lib/ecasound-plugins/*.$$A \
	 `pwd`/debian/${ECASOUND}/usr/lib/python*/site-packages/*.$$A; done

	#dh_movefiles is removed. Do the mving myself.
	# libecasound-dev
	for FILES in usr/bin/libecasound-config \
		usr/lib/libecasound.a usr/lib/libecasound.la usr/include/libecasound; do \
	 	mv $(CURDIR)/debian/${ECASOUND}/$$FILES $(CURDIR)/debian/$(LIBECASOUNDDEV)/$$FILES; \
	done
	# libkvutils-dev
	for FILES in usr/lib/libkvutils.a usr/lib/libkvutils.la usr/include/kvutils ; do \
		mv $(CURDIR)/debian/${ECASOUND}/$$FILES $(CURDIR)/debian/$(LIBKVUTILSDEV)/$$FILES; \
	done
	# python
	mv $(CURDIR)/debian/${ECASOUND}/usr/lib/python* $(CURDIR)/debian/$(PYTHONECASOUND)/usr/lib/
	chmod 644 $(CURDIR)/debian/$(PYTHONECASOUND)/usr/lib/python2.4/site-packages/pyeca.py
	# ruby
	mv $(CURDIR)/debian/${ECASOUND}/usr/lib/ruby/1.8/ecasound.rb $(CURDIR)/debian/$(RUBYECASOUND)/usr/lib/ruby/1.8/
	chmod 644 $(CURDIR)/debian/$(RUBYECASOUND)/usr/lib/ruby/1.8/ecasound.rb
	# libecasoundc-dev
	mv $(CURDIR)/debian/${ECASOUND}/usr/lib/libecasoundc.a $(CURDIR)/debian/$(LIBECASOUNDCDEV)/usr/lib
	mv $(CURDIR)/debian/${ECASOUND}/usr/lib/libecasoundc.la $(CURDIR)/debian/$(LIBECASOUNDCDEV)/usr/lib
	mv $(CURDIR)/debian/${ECASOUND}/usr/bin/libecasoundc-config $(CURDIR)/debian/$(LIBECASOUNDCDEV)/usr/bin
	mv $(CURDIR)/debian/${ECASOUND}/usr/include/libecasoundc/ $(CURDIR)/debian/$(LIBECASOUNDCDEV)/usr/include/

# Build architecture-independent files here.
binary-indep: build install
	dh_testdir -i
	dh_testroot -i
	dh_installdocs -i
	dh_installdirs -i
	install -m644 -o root -g root ecatools/ecasound.el debian/ecasound-el/usr/share/emacs/site-lisp/ecasound-el/ecasound.el
	dh_installemacsen -p ecasound-el --priority=60 
	dh_installchangelogs  -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir -a
	dh_testroot -a
	dh_installdocs -a
	sed 's/\.pdf\"/\.pdf.gz\"/' < Documentation/index.html \
	  > debian/${ECASOUND}/usr/share/doc/${ECASOUND}/index.html
	cp -a Documentation/users_guide debian/${ECASOUND}/usr/share/doc/${ECASOUND}/
	cp -a Documentation/programmers_guide debian/${ECASOUND}/usr/share/doc/${ECASOUND}/
	dh_installexamples -a
	dh_installmenu -a
	dh_installcron -a
	dh_installmanpages -p${ECASOUND} libecasound-config.1 libecasoundc-config.1
	dh_installman -p$(LIBECASOUNDDEV) debian/libecasound-config.1
	dh_installman -p$(LIBECASOUNDCDEV) debian/libecasoundc-config.1
	dh_installinfo -a
	dh_installchangelogs  -a
	dh_link -a
	dh_python -a 
	dh_strip -a
	dh_shlibdeps -a
	dh_compress -a
	dh_fixperms -a
	dh_gencontrol -a
	dh_installdeb -a
	dh_md5sums -a
	dh_builddeb -a

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