#!/usr/bin/make -f
# The template for this file was originally written by Joey Hess and Craig Small.
# Developed for Gnash by Miriam Ruiz

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

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

MAIN_VERSION = $(shell head -n 1 debian/changelog | cut '-d ' -f 2 | sed 's/[()]//g')

CFLAGS = -Wall -g -Werror-implicit-function-declaration

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif

# shared library versions, option 1
#version=2.0.5
#major=2
# option 2, assuming the library is created as src/.libs/libfoo.so.2.0.5 or so
#version=`ls src/.libs/lib*.so.* | \
# awk '{if (match($$0,/[0-9]+\.[0-9]+\.[0-9]+$$/)) print substr($$0,RSTART)}'`
#major=`ls src/.libs/lib*.so.* | \
# awk '{if (match($$0,/\.so\.[0-9]+$$/)) print substr($$0,RSTART+4)}'`

# CONFIGURE

config.status: configure.ac
	dh_testdir

	@printf "\n == APPLYING PATCHES ============================================ \n\n"
	# we have no patches, we're the developers!
#	$(MAKE) -f /usr/share/quilt/quilt.make patch
	@printf "\n ----------------------------------- End of APPLYING PATCHES ---- \n\n"

ifneq "$(wildcard /usr/share/misc/config.sub)" ""
	cp -f /usr/share/misc/config.sub config.sub
endif
ifneq "$(wildcard /usr/share/misc/config.guess)" ""
	cp -f /usr/share/misc/config.guess config.guess
endif

	@printf "\n == AUTOGEN ===================================================== \n\n"
	./autogen.sh
	@printf "\n -------------------------------------------- End of AUTOGEN ---- \n\n"

	# for now, only build the kde4 support,as kde3 is being depreciated.

	@printf "\n == CONFIGURE =================================================== \n\n"
	./configure CFLAGS="$(CFLAGS)" CXXFLAGS="$(CXXFLAGS)" \
		--host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) \
		--prefix=/usr \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--disable-dependency-tracking \
		--disable-rpath \
		--enable-cygnal \
		--enable-avm2 \
		--enable-python \
		--enable-jemalloc \
		--enable-sdkinstall \
		--disable-testsuite \
		--enable-media=gst \
		--enable-gui=gtk,kde4 \
		--enable-renderer=agg \
		--with-plugins-install=system

	@printf "\n ------------------------------------------- End of CONFIGURE ---- \n\n"

	cp libamf/README README.amf

# BUILD

build: build-stamp
build-stamp:  config.status
	dh_testdir

	@printf "\n == MAKE ======================================================== \n\n"
	$(MAKE) 
	@printf "\n ----------------------------------------------- End of MAKE ---- \n\n"

#	@printf "\n == CHECK ======================================================= \n\n"
#	$(MAKE) check
#	@printf "\n ---------------------------------------------- End of CHECK ---- \n\n"

	touch build-stamp

# CLEAN

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp 

ifeq (Makefile,$(wildcard Makefile))
	-$(MAKE) maintainer-clean
	-$(MAKE) distclean
endif

ifneq ($(wildcard ${AUTO_JUNK}),)
	rm $(wildcard ${AUTO_JUNK})
endif

	rm -f config.{sub,guess,log,status}
	rm -f libtool
	rm -f `find . -name "Makefile"| grep -v "debian/"`
	rm -f `find . -name "*.o"`
	rm -rf `find . -name ".libs"`
	rm -rf `find . -name ".deps"`

	rm -f configure aclocal.m4 config.h.in
	#-find . -name Makefile.in -exec rm {} \;
	-rm `find . -name Makefile.in`

	-rm -rf libltdl/autom4te.cache
	-rm -f libltdl/* ltmain.sh

	#cd debian/h2m; $(MAKE) clean

	-rm -f README.amf

	@printf "\n == CLEANING PATCHES ============================================ \n\n"
	# we have no patches, we're the developers!
#	$(MAKE) -f /usr/share/quilt/quilt.make unpatch
	@printf "\n ----------------------------------- End of CLEANING PATCHES ---- \n\n"

	dh_clean 

# INSTALL

install: build
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs

	install -d $(CURDIR)/debian/tmp/usr/bin
	install -d $(CURDIR)/debian/tmp/usr/lib
	install -d $(CURDIR)/debian/tmp/usr/lib/gnash/
	install -d $(CURDIR)/debian/tmp/usr/lib/mozilla/plugins/
	install -d $(CURDIR)/debian/tmp/usr/lib/firefox/plugins/
	install -d $(CURDIR)/debian/tmp/usr/include/gnash

	@printf "\n == INSTALL ===================================================== \n\n"
	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp
	$(MAKE) install-plugins DESTDIR=$(CURDIR)/debian/tmp
	@printf "\n -------------------------------------------- End of INSTALL ---- \n\n"

	#cd debian/h2m; $(MAKE)

	install -d $(CURDIR)/debian/tmp/usr/include/gnash/libbase/
	cp libbase/*.h $(CURDIR)/debian/tmp/usr/include/gnash/libbase/
	install -d $(CURDIR)/debian/tmp/usr/include/gnash/libnet/
	cp libbase/*.h $(CURDIR)/debian/tmp/usr/include/gnash/libnet/
	install -d $(CURDIR)/debian/tmp/usr/include/gnash/libamf/
	cp libamf/*.h $(CURDIR)/debian/tmp/usr/include/gnash/libamf/
	install -d $(CURDIR)/debian/tmp/usr/include/gnash/libcore/
	cp libcore/*.h $(CURDIR)/debian/tmp/usr/include/gnash/libcore/
	install -d $(CURDIR)/debian/tmp/usr/include/gnash/libmedia/
	cp libmedia/*.h $(CURDIR)/debian/tmp/usr/include/gnash/libmedia/
	install -d $(CURDIR)/debian/tmp/usr/include/gnash/backend/
	cp backend/*.h $(CURDIR)/debian/tmp/usr/include/gnash/backend/
	install -d $(CURDIR)/debian/tmp/usr/include/gnash/gui/
	cp gui/*.h $(CURDIR)/debian/tmp/usr/include/gnash/gui/

	install -d $(CURDIR)/debian/tmp/usr/share/pixmaps/
	cp packaging/*.xpm $(CURDIR)/debian/tmp/usr/share/pixmaps/
	install -d $(CURDIR)/debian/tmp/usr/share/applications/
	cp debian/*.desktop $(CURDIR)/debian/tmp/usr/share/applications/

# CREATE PACKAGES

# 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 ChangeLog
	dh_installdocs
	dh_installexamples
	dh_install --sourcedir=$(CURDIR)/debian/tmp
#	dh_installmenu
#	dh_installmime
	dh_installman
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
#	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

VERSION_DATE = $(shell /bin/date --utc +%0Y%0m%0d.%0k%0M)
VERSION_ID = trunk_$(VERSION_DATE)
get-orig-source:
	echo Downloading gnash $(VERSION_ID) from CVS...
	#test -e gnashd && exit
	export CVS_RSH="ssh"; cvs -z3 -d:pserver:anonymous@cvs.sv.gnu.org:/sources/gnash co gnash
	#cd gnash; rm -rf `find . -name CVS`
	tar cvfz "gnash_$(VERSION_ID).orig.tar.gz" gnash
	mv gnash "gnash-$(VERSION_ID)"
	cp debian "gnash-$(VERSION_ID)" -a
	cd "gnash-$(VERSION_ID)"; dch -v "$(VERSION_ID)-1" "New Upstream Release. Downloaded from CVS."


binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install get-orig-source
