#!/usr/bin/make -f

# This has to be exported to make some magic below work.
export DH_OPTIONS

# To fetch upstream CVS snapshot -- relative path to it
# from the root of packaging project
CVS_ROOT_PATH=lush-cvs
CVS_TIMESTAMP=$(shell date +"20%y%m%d")

DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_GNU_CPU ?= $(shell dpkg-architecture -qDEB_HOST_GNU_CPU)
ifeq ($(DEB_HOST_ARCH),i386)
  CONFIG_ARGS += --with-cpu=$(DEB_HOST_GNU_CPU)
endif

# Mega rule
%:
	dh $@ --with autotools_dev

get-cvs-snapshot:
	#cd ../$(CVS_ROOT_PATH) && cvs update
	cd .. && \
	 diff -x config.sub -x config.guess -x .svn -x CVS -x .git -x debian -Naur lush $(CVS_ROOT_PATH) | \
	 dpatch patch-template -p "cvs$(CVS_TIMESTAMP)" "Dated CVS snapshot" >| lush/debian/patches/cvs$(CVS_TIMESTAMP) && \
	 { echo "cvs$(CVS_TIMESTAMP)"; grep -v '^cvs[0-9]*' lush/debian/patches/series; } >| patches.new && \
	 mv patches.new lush/debian/patches/series

override_dh_auto_configure:
	dh_auto_configure -- $(CONFIG_ARGS)

override_dh_install:
	dh_install

	# Additional doc file
	install -D -m 0644 doc/README debian/lush/usr/share/doc/lush/README.doc

	# For binary-arch only there would be no lush-library
	# Moving emacs script to proper location
	[ -d debian/lush-library ] && \
		mv debian/lush-library/usr/share/lush/etc/lush.el \
		   debian/lush-library/usr/share/emacs/site-lisp/lush/ || :

	# Removing double files or ones installed via other means
	rm -f debian/lush-library/usr/share/lush/src/lush \
		  debian/lush-library/usr/share/lush/etc/lush.1 \
		  debian/lush-library/usr/share/lush/etc/README*; \

	rm -rf debian/lush-library/usr/share/man

	# Cleaning up
	-find debian/lush*/usr -name "*.o" -delete

dh_fixperms_indep:
	# Fixing permissions
	find debian/lush-library/usr/share -type f -exec chmod 0644 {} \;
	grep -l '^#!' debian/lush-library/usr/share/lush/etc/* | xargs chmod a+x
	find debian/lush-library/usr/share/lush -name "*.py" -exec chmod 0755 {} \;
	find debian/lush-library/usr/share/lush -name "demo*" -exec chmod 0755 {} \;
	chmod 0755 debian/lush-library/usr/share/lush/packages/sn28/examples/tsp/tsp
	chmod 0755 debian/lush-library/usr/share/lush/demos/*
	chmod 0644 debian/lush-library/usr/share/lush/demos/README
	chmod 0644 debian/lush-library/usr/share/lush/demos/*.lsh

override_dh_fixperms:
	[ -d debian/lush-library ] && $(MAKE) -f debian/rules dh_fixperms_indep || :
	dh_fixperms
