#!/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

CFLAGS = -Wall -g

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

check:
	dh_testdir
ifeq (,$(filter nocheck, $(DEB_BUILD_OPTIONS)))
	relocate/bin/yorick -batch debian/check.i
endif

configure: configure-stamp
configure-stamp:
	dh_testdir
	make Y_HOME=relocate ysite
	chmod a+x debian/helpers/*.i
	touch configure-stamp


BUILDDIR=../../build/
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
	cp -a relocate build/share/yorick
	mv build/share/yorick/doc build
	cp debian/helpers/README build/doc
	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
	$(MAKE) -C doc/html BUILDDIR=$(BUILDDIR) manual refcard $(BUILDDIR)doc/README.html $(BUILDDIR)doc/images/triangle3.jpg $(BUILDDIR)doc/skull.css
	cp doc/html/packinfo.txt build/00yorick.packinfo
	cp doc/html/keywords.txt build/00yorick.keywords
	cp doc/html/aliases.txt build/00yorick.aliases
	touch build-stamp

clean: configure-stamp
	dh_testdir
	dh_testroot
	-rm -Rf build-stamp configure-stamp build relocate
	$(MAKE) distclean
	$(MAKE) Y_HOME=. ysite
	$(MAKE) -C doc/html clean
	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_installdebconf
	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

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

###### Getting the original source:
Y_VERS:=$(shell dpkg-parsechangelog -l$(PATH_TO_DEBIAN)/changelog | grep Version | sed 's/Version: //' | sed 's/+.*//')
CVSTAG:=y_$(shell dpkg-parsechangelog -l$(PATH_TO_DEBIAN)/changelog | grep Version | sed 's/Version: //' | sed 's/+.*//' | tr . _)
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 }')
CVSVERSDATE:=$(shell dpkg-parsechangelog -l$(PATH_TO_DEBIAN)/changelog | grep Version | sed 's/.*cvs//' | sed 's/-.*//')
CVSVERS:=$(Y_VERS)+dfsg+cvs$(CVSVERSDATE).orig
ORIG_URL:=http://dl.sourceforge.net/sourceforge/yorick/
ORIG_TGZ:=yorick-$(Y_VERS).tgz
# Non-DFSG-free files (see copyright):
PRUNEFILES:=play/hacks/hack103.c i/idlsave.i opengl

# Use this rule to get the original sources if the current version contains
# "+cvsyyyymmdd". Breaks when sourceforge change their CVS access.
#
# WARNING: Side effect: this will use and remove the
# directory "orig-sources" and overwrite yorick-(version).orig/
# and yorick_(version).orig.tar.gz.
get-cvs-date:
	-rm -Rf ../orig-sources
	mkdir ../orig-sources
	cvs -d$(CVSROOT) login
	cd ../orig-sources ; cvs -z3 -d$(CVSROOT) export -D $(CVSDATE) yorick
	-rm -Rf ../yorick-$(CVSVERS)
	mv ../orig-sources/yorick ../yorick-$(CVSVERS)
	rm -Rf ../orig-sources
	cd ../yorick-$(CVSVERS); rm -Rf $(PRUNEFILES)
	cd ..; tar cf yorick_$(CVSVERS).tar yorick-$(CVSVERS)
	gzip -9 ../yorick_$(CVSVERS).tar

# check copyright to see whether was from cvs or from tgz.
get-cvs-tag:
	-rm -Rf ../orig-sources
	mkdir ../orig-sources
	cvs -d$(CVSROOT) login
	cd ../orig-sources ; cvs -z3 -d$(CVSROOT) export -r $(CVSTAG) yorick
	-rm -Rf ../yorick-$(CVSVERS)
	mv ../orig-sources/yorick ../yorick-$(Y_VERS)+dfsg
	rm -Rf ../orig-sources
	cd ../yorick-$(Y_VERS)+dfsg; rm -Rf $(PRUNEFILES)
	cd ..; tar cf yorick_$(Y_VERS)+dfsg.orig.tar yorick-$(Y_VERS)+dfsg
	gzip -9 ../yorick_$(Y_VERS)+dfsg.orig.tar

get-tgz:
	cd ..; wget $(ORIG_URL)$(ORIG_TGZ)
	cd ..; tar xvzf $(ORIG_TGZ)
	mv ../yorick-2.1 ../yorick-$(CVSVERS)
	cd ../yorick-$(CVSVERS); rm -Rf $(PRUNEFILES)
	cd ..; tar cf yorick_$(CVSVERS).tar yorick-$(CVSVERS)
	gzip -9 ../yorick_$(CVSVERS).tar

get-orig-source get-orig-sources:
	echo "For a stable release try get-tgz or get-cvs-tag"
	echo "for a +cvs release try get-cvs-date"

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