#!/usr/bin/make -f
# -*- make -*-
# debian/rules file for jed-extra
# GNU copyright 2003, 2005 by Rafael Laboissiere <rafael@debian.org>
# $Id: rules 1198 2009-10-05 18:25:59Z milde-guest $

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

#include /usr/share/dpatch/dpatch.make

# Variables
# =========

# File Sorting
# ------------

CONTENTS_FILE = debian/contents.txt

ADDITION = $(shell awk '/^A/ { print $$2"/*"; }' $(CONTENTS_FILE))
COLOR = $(shell awk '/^C/ { print $$2"/*"; }' $(CONTENTS_FILE))
DROP_IN = $(shell awk '/^D/ { print $$2"/*"; }' $(CONTENTS_FILE))
ENHANCEMENT = $(shell awk '/^E/ { print $$2"/*"; }' $(CONTENTS_FILE))
EXPERIMENTAL = $(shell awk '/^X/ { print $$2"/*"; }' $(CONTENTS_FILE))
TESTS = $(shell awk '/^T/ { print $$2"/*"; }' $(CONTENTS_FILE))
UTILS = $(shell awk '/^U/ { print $$2"/*"; }' $(CONTENTS_FILE))

debroot = $(CURDIR)/debian/jed-extra
libdir = /usr/share/jed/jed-extra

dh_inst_excl = -X.html -X.otl -X.example -X.template -XREADME


# Source download
# ---------------

WGET = wget -N

# URL of the upstream source package
UPSTREAM_VERSION ?= $(shell perl -ne '/([\d.]+[pre]*\d*)-?/; print $$1; exit' < debian/changelog)
UPSTREAM_PACKAGE =          jedmodes-$(UPSTREAM_VERSION).tgz
UPSTREAM_PACKAGE_CHECKSUM = jedmodes-$(UPSTREAM_VERSION).md5sum
# officially released CVS tarball from the SF FRS
UPSTREAM_URL = http://downloads.sourceforge.net/project/jedmodes/
MIRROR = ?use_mirror=surfnet

# build from local copy
# Use curl, as this also understands a file:// URL
# * write output to file named like the remote file
# * attempt  to  figure  out  the timestamp  of the remote file,
#   and if that is available make the local file get that same timestamp.
#UPSTREAM_URL = file:/home/milde/.jed/jedmodes/src/
#MIRROR = ''
#WGET = curl --remote-name --remote-time

# Rules
# =====

get-orig-source:
	# get and check the jedmodes tarball
	$(WGET) $(UPSTREAM_URL)$(UPSTREAM_PACKAGE)$(MIRROR)
	# Check the md5sum of the upstream tarball
	$(WGET) $(UPSTREAM_URL)$(UPSTREAM_PACKAGE_CHECKSUM)$(MIRROR)
	cat $(UPSTREAM_PACKAGE_CHECKSUM) | md5sum -c || exit 1

	mv $(UPSTREAM_PACKAGE) jed-extra_$(UPSTREAM_VERSION).orig.tar.gz

	# additional modes from other sources
	# currently none, code kept for reference
	# mkdir --parents jedmodes-$(UPSTREAM_VERSION)/boxquote
	# cd jedmodes-$(UPSTREAM_VERSION)/boxquote; \
	#     $(WGET) http://www.cis.fu-berlin.de/~phgrau/slang/boxquote.sl
	# 
	# gunzip jed-extra_$(UPSTREAM_VERSION).orig.tar.gz
	# tar --append --file jed-extra_$(UPSTREAM_VERSION).orig.tar \
	#     jedmodes-$(UPSTREAM_VERSION)/boxquote/*
	# gzip -9 jed-extra_$(UPSTREAM_VERSION).orig.tar

clean:
	dh_testdir
	# debconf-updatepo
	rm -rf *.tgz *.md5sum *.tar.gz jedmodes-*
	dh_clean

binary binary-indep:
	dh_testdir
	dh_testroot
	dh_clean

	# install modes
ifneq ($(ADDITION)$(ENHANCEMENT), )
	dh_install $(dh_inst_excl) $(ADDITION) $(ENHANCEMENT) $(libdir)
endif
ifneq ($(COLOR), )
	dh_install $(dh_inst_excl) $(COLOR) $(libdir)/colors
endif
ifneq ($(DROP_IN), )
	dh_install $(dh_inst_excl) $(DROP_IN) $(libdir)/drop-in
endif
ifneq ($(TESTS), )
	dh_install $(dh_inst_excl) $(TESTS) $(libdir)/tests
endif
ifneq ($(UTILS), )
	dh_install $(dh_inst_excl) $(UTILS) $(libdir)/utils
endif
ifneq ($(EXPERIMENTAL), )
	dh_install $(dh_inst_excl) $(EXPERIMENTAL) $(libdir)/extra
endif

        # install package documentation
	dh_installdocs debian/contents.txt

	# install upstream documentation
	dh_install $$(find . -wholename ./debian -prune \
	    -o \( -name README \) -print) usr/share/doc/jed-extra/

	dh_install $$(find . -wholename ./debian -prune \
	    -o \(   -name \*.html \
	         -o -name \*.otl \
	         -o -name \*.example \
		 -o -name \*.hlp \
		 -o -name \*.template \
	       \) -print) \
	    usr/share/doc/jed-extra/mode-doc/
	# TODO: *.hlp files are installed in mode-doc and jed-library-path
	#       symlink them instead

	# Install examples
	dh_installexamples debian/examples/*

	# fix some names (as dh_install* cannot change filenames)
	mv $(debroot)/usr/share/jed/compile/compile \
	   $(debroot)/usr/share/jed/compile/jed-extra

	chmod +x $(debroot)/usr/share/jed/compile/jed-extra	\
		$(debroot)/usr/share/jed/jed-extra/tm.sed

	dh_installdebconf

	dh_installchangelogs
	dh_compress --exclude=.sl
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

build build-arch build-indep:
	# nothing to do, but required by debian policy

binary-arch:
	# nothing to do, but we need it to be policy conform

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