#!/usr/bin/make -f
# -*- mode: makefile; coding: utf-8 -*-
# Copyright 2011-2012, 2014-2017 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/debhelper.mk

pkg = $(DEB_SOURCE_PACKAGE)

# Resolve custom version for ruby-compass-import-once binary package
_common-part = $(firstword $(subst ~,$(space),$(DEB_UPSTREAM_VERSION)))
_custom-part = $(strip $(shell cat import-once/VERSION))
VERSION-custom = $(subst $(_common-part),$(_custom-part),$(DEB_VERSION))

# needed for our build
bdeps = gem2deb

# needed during build and (always) at runtime
deps-all = 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
depends +=, ruby-compass-core (= $(DEB_VERSION))
depends +=, ruby-compass-import-once (= $(VERSION-custom))
recommends = ruby-oily-png

CDBS_BUILD_DEPENDS +=, $(bdeps), $(deps), $(deps-core), $(deps-all)
CDBS_DEPENDS_$(pkg) = $(depends-all), $(deps), $(deps-all), $(depends)
CDBS_DEPENDS_ruby-compass-core = $(deps-core), $(depends-all), $(deps-all)
CDBS_DEPENDS_ruby-compass-import-once = $(depends-all), $(deps-all)
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

DEB_DH_GENCONTROL_ARGS = $(strip \
 $(if $(filter ruby-compass-import-once,$(cdbs_curpkg)),\
  -- -v$(VERSION-custom)))

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)
