#!/usr/bin/make -f
# -*- mode: makefile; coding: utf-8 -*-
# Copyright © 2011, 2012, 2014-2016 Jonas Smedegaard <dr@jones.dk>
# Description: Main Debian packaging script for Compass
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

include /usr/share/cdbs/1/rules/utils.mk
include /usr/share/cdbs/1/rules/debhelper.mk

pkg = $(DEB_SOURCE_PACKAGE)

# Build-depend unversioned on debhelper
#  TODO: Drop when adopted in cdbs
CDBS_BUILD_DEPENDS_rules_debhelper_v9 = debhelper

# needed for our build
bdeps = gem2deb (>= 0.4.1~)

# needed during build and at runtime
deps = ruby-sass (>= 3.3.6), ruby-sass (<< 3.5)
deps +=, ruby-chunky-png (<< 2.0)
deps-core = ruby-multi-json (<< 2.0)

# needed (always/often) at runtime
depends-all = ruby | ruby-interpreter
depends +=, ruby-rb-inotify (>= 0.9)
depends +=, ruby-compass-core (= $(DEB_VERSION))
depends +=, ruby-compass-import-once (= $(DEB_VERSION))
depends +=, ruby-sass (>= 3.3.13), ruby-sass (<< 3.5)
depends-core = ruby-sass (>= 3.3.0), ruby-sass (<< 3.5)
depends-import-once = ruby-sass (>= 3.2), ruby-sass (<< 3.5)
recommends = ruby-oily-png

CDBS_BUILD_DEPENDS +=, $(bdeps), $(deps), $(deps-core)
CDBS_DEPENDS_$(pkg) = $(depends-all), $(deps), $(depends)
CDBS_DEPENDS_ruby-compass-core = $(deps-core), $(depends-all), $(depends-core)
CDBS_DEPENDS_ruby-compass-import-once = $(depends-all), $(depends-import-once)
CDBS_RECOMMENDS_$(pkg) = $(recommends)

DEB_INSTALL_CHANGELOGS_ALL = compass-style.org/content/CHANGELOG.markdown
DEB_INSTALL_DOCS_ALL += TODO.md
DEB_INSTALL_DOCS_$(pkg) = README.markdown
DEB_INSTALL_DOCS_ruby-compass-core = core/README.md
DEB_INSTALL_DOCS_ruby-compass-import-once = import-once/README.md

# extract metadata from images and fonts before copyright check
CDBS_BUILD_DEPENDS +=, libimage-exiftool-perl, libregexp-assemble-perl
CDBS_BUILD_DEPENDS +=, libipc-system-simple-perl, lcdf-typetools
local_inspection_regex = png|jpg|jpeg|gif|ttf|otf
DEB_COPYRIGHT_CHECK_IGNORE_REGEX = ^((.*/)?[^/]+\.($(local_inspection_regex))|debian/(changelog|copyright(|_hints|_newhints)))$$
debian/stamp-copyright-check: debian/stamp-extract-copyright
debian/stamp-extract-copyright:
	find * -type f -regextype posix-extended \
		-regex '.*\.($(local_inspection_regex))' \
		-print0 | perl -0 debian/print-metadata.pl
	touch $@
pre-build:: clean-extracted-copyright-during-build
clean-extracted-copyright-during-build: debian/stamp-copyright-check
	find -type f -name '*.metadata_dump' -delete
clean::
	find -type f -name '*.metadata_dump' -delete
	rm -f debian/stamp-extract-copyright

common-configure-arch common-configure-indep:: debian/stamp-ruby-gem-configure
debian/stamp-ruby-gem-configure::
	dh_ruby --configure
	touch $@

common-build-arch common-build-indep:: debian/stamp-ruby-gem-build
debian/stamp-ruby-gem-build::
	dh_ruby --build
	touch $@

common-install-arch common-install-indep:: debian/stamp-ruby-gem-install
debian/stamp-ruby-gem-install::
	dh_ruby --install
	touch $@

clean::
	cp -f import-once/VERSION import-once/lib/compass/import-once/VERSION
	dh_ruby --clean
	rm -f import-once/lib/compass/import-once/VERSION
	rm -f $(patsubst %,debian/stamp-ruby-gem-%,configure build install)

# embed or install VERSION and VERSION_NAME files
CDBS_BUILD_DEPENDS +=, libfile-slurp-perl
binary-post-install/$(pkg)::
	perl -mFile::Slurp=:all \
		-e '($$ver) = read_file ( "cli/VERSION", chomp => 1 );' \
		-e '($$nick) = read_file ( "cli/VERSION_NAME", chomp => 1);' \
		-e 'edit_file {' \
			-e 's/(\h+)#.*(\n\1#.*)*/$${1}VERSION = "$$ver"\n$${1}VERSION_NAME = "$$nick"/' \
		-e '} "debian/$(cdbs_curpkg)/usr/lib/ruby/vendor_ruby/compass/generated_version.rb";'
binary-post-install/ruby-compass-core::
	perl -mFile::Slurp=:all \
		-e '($$ver) = read_file ( "core/VERSION", chomp => 1 );' \
		-e 'edit_file {' \
			-e 's/(\h+)#.*(\n\1#.*)*/$${1}VERSION = "$$ver"/' \
		-e '} "debian/$(cdbs_curpkg)/usr/lib/ruby/vendor_ruby/compass/core/generated_version.rb";'
binary-post-install/ruby-compass-import-once::
	install -m a=r,u+w \
		-t debian/$(cdbs_curpkg)/usr/lib/ruby/vendor_ruby/compass/import-once \
		import-once/VERSION

binary-post-install/$(pkg)::
	find debian/$(cdbs_curpkg) -type f \( -name LICENSE -or -name .gitignore \) -delete

binary-fixup/ruby-compass-core::
	chmod -R a-x,a+X debian/$(cdbs_curpkg)/usr/share/compass/

# copy VERSION file to patched location during build
pre-build:: debian/stamp-install-VERSION
debian/stamp-install-VERSION:
	cp -f import-once/VERSION import-once/lib/compass/import-once
	touch $@
clean::
	rm -f import-once/lib/compass/import-once/VERSION
	rm -f debian/stamp-install-VERSION

# generate manpage based on --help of script itself
manpages = debian/compass.1
CDBS_BUILD_DEPENDS +=, help2man
DEB_INSTALL_MANPAGES_ruby-compass = $(manpages)
build/$(pkg):: $(manpages)
$(manpages): debian/%.1 : cli/bin/%
	chmod +x $<
	RUBYLIB=import-once/lib:core/lib:cli/lib help2man \
		--name "Stylesheet Framework streamlining creation and maintenance of CSS" \
		--no-info --output=$@ $< \
		|| RUBYLIB=import-once/lib:core/lib:cli/lib $< --help
clean::
	rm -f $(manpages)
