#!/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.
#
# Modified to make a template file for a multi-binary package with separated
# build-arch and build-indep targets  by Bill Allombert 2001

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

# start by saving the path to debian/rules (useful in get-orig-source)
PATH_TO_DEBIAN := $(shell dirname $(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST)))

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

include /usr/share/dpatch/dpatch.make

CFLAGS = -Wall -g

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

configure: configure-stamp
configure-stamp: patch-stamp
	dh_testdir
	make Y_HOME=relocate ysite
	touch configure-stamp


build: build-stamp
build-stamp: configure-stamp 
	dh_testdir
	$(MAKE) install
	-rm relocate/LICENSE
	$(MAKE) -C doc yorick.info
	mkdir -p build/share
	mkdir -p build/lib/yorick
	mv relocate/doc build/doc
	mv relocate build/share/yorick
	mkdir -p build/share/yorick/packages/installed
	mv build/share/yorick/bin build/lib/yorick/
	mv build/share/yorick/lib build/lib/yorick/
	mkdir -p build/yorick-dev/share/yorick/
	mkdir -p build/yorick-dev/lib/yorick/lib/
	mv build/share/yorick/Make* build/yorick-dev/share/yorick/
	mv build/share/yorick/include build/yorick-dev/share/yorick/
	cd build/lib/yorick/lib; \
	   mv codger install.grp install.sh libdep.sh libyor.a main.o \
              $(CURDIR)/build/yorick-dev/lib/yorick/lib
	cp emacs/yorick-auto.el build/50yorick-auto.el
	convert icons/yicon32.png build/yorick32x32.xpm
	convert icons/yicon16.png build/yorick16x16.xpm
	touch build-stamp

clean: configure-stamp clean-patched unpatch
clean-patched:
	dh_testdir
	dh_testroot
	-rm -Rf build-stamp configure-stamp build relocate
	$(MAKE) distclean
	$(MAKE) Y_HOME=. ysite
	dh_clean

# Must not depend on anything. This is to be called by
# binary-arch/binary-indep
# in another 'make' thread.
binary-common:
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs
	dh_install
	dh_installchangelogs 
	dh_installdocs
	dh_installexamples
	dh_installmenu
	dh_installinfo
	dh_installman
	dh_link
	dh_strip
	dh_usrlocal
	dh_compress 
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture independant packages using the common target.
#binary-indep: build-indep install-indep
binary-indep: build-stamp
	$(MAKE) -f debian/rules DH_OPTIONS=-i binary-common

#binary-indep:

# Build architecture dependant packages using the common target.
binary-arch: build-stamp
	$(MAKE) -f debian/rules DH_OPTIONS=-a binary-common

CVSVERS:=$(shell dpkg-parsechangelog -l$(PATH_TO_DEBIAN)/changelog | grep Version | sed 's/Version: //' | sed 's/-.*//').orig
CVSROOT:=:pserver:anonymous:@yorick.cvs.sourceforge.net:/cvsroot/yorick
CVSDATE:=$(shell dpkg-parsechangelog -l$(PATH_TO_DEBIAN)/changelog | grep Version | sed 's/.*cvs//' | sed 's/-.*//' | awk '{ printf "%08.0f", $$1+1 }')

# This rule is to be called from anywhere, preferentially _not_ from within
# the source tree.  It will download the source tree from the CVS
# corresponding to the last version in the changelog, then prune
# non-DFSG-compatible files (see copyright), and pack all this as the relevant
# .orig.tar.gz file (in the current directory).
#
# WARNING: Side effect: this will use and remove the
# directory "orig-sources".
get-orig-source:
	-rm -Rf orig-sources
	mkdir orig-sources
	cvs -d$(CVSROOT) login
	cd orig-sources ; cvs -z3 -d$(CVSROOT) export -D $(CVSDATE) yorick
	rm orig-sources/yorick/play/hacks/hack103.c
	rm orig-sources/yorick/i/idlsave.i
	rm -Rf orig-sources/opengl
	-rm -Rf yorick-$(CVSVERS)
	mv orig-sources/yorick yorick-$(CVSVERS)
	tar cvzf yorick_$(CVSVERS).tar.gz yorick-$(CVSVERS)
	rm -Rf yorick-$(CVSVERS)
	rm -Rf orig-sources


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