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

# 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)
DEB_HOST_ARCH_OS    ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)

CFLAGS=$(shell dpkg-buildflags --get CFLAGS)
CXXFLAGS=$(shell dpkg-buildflags --get CXXFLAGS)
LDFLAGS=$(shell dpkg-buildflags --get LDFLAGS)

config.status: configure
	dh_testdir
	CFLAGS="$(CFLAGS)" CXXFLAGS="$(CXXFLAGS)" LDFLAGS="$(LDFLAGS)" ./configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --with-tcl=/usr/lib/tcl8.5 --with-tk=/usr/lib/tk8.5 --prefix=/usr

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

	$(MAKE) 

	touch build-stamp

build-arch: build
build-indep: build

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp 

	 [ ! -f Makefile ] || $(MAKE) distclean

	# Clean some libs that are not clean by makefile
	rm -f ./utils/TkCximage/TkCximage.so
	rm -f ./utils/webcamsn/webcamsn.so
	rm -f ./utils/linux/capture/config.h
	rm -f ./utils/tcl_siren/tcl_siren.so
	rm -f ./utils/tclISF/tclISF.so ./utils/asyncresolver/libasyncresolver.so
	rm -f ./config.log

	dh_clean

#amsn-remote should be in amsn, not amsn-data
#GNUGPL and INSTALL are not needed
#MusicWin are visual studio project files
#amsnplus is broken
#Thumbs.db is for Windows only
#growl is for Max only
#convert.tcl, test.tcl and missing.py are for developer use only
#license files are already elsewhere
SKIPFILES=amsn-remote \
	GNUGPL \
	INSTALL \
	MusicWin.ds \
	amsnplus \
	Thumbs.db \
	growl \
	convert.tcl \
	test.tcl \
	missing.py \
	license \
	LICENSE.txt 
SKIPFILES_EXPANDED := $(foreach f,$(SKIPFILES),-X$(f))

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp
	#Some permissions are wrong upstream and dh_fixperms doesn't handle these file types
	find $(CURDIR)/debian/tmp/usr/share/amsn -name \*.png -exec chmod a-x {} \;
	find $(CURDIR)/debian/tmp/usr/share/amsn -name \*.tcl -exec chmod a-x {} \;
	find $(CURDIR)/debian/tmp/usr/share/amsn -name \*.xml -exec chmod a-x {} \;
	chmod a-x $(CURDIR)/debian/tmp/usr/share/amsn/plugins/SpellCheck/lang/langlt
	dh_install -p amsn
	dh_install -p amsn-data $(SKIPFILES_EXPANDED)

binary-indep: build install
# We have nothing to do by default.

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs ChangeLog 
	dh_installdocs
	dh_installmenu
	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 
