#!/usr/bin/make -f

MAJOR_VERSION := 3.1
VERSION := `head -1 debian/changelog | sed 's|.\+(\([0-9\.]\+\)[-~].\+).\+|\1|'`

PEAR_LIB_DIR := $(shell pear config-get php_dir)

DESTDIR := debian/tmp$(PEAR_LIB_DIR)/kohana$(MAJOR_VERSION)
INSTDIR := $(PEAR_LIB_DIR)/kohana$(MAJOR_VERSION)

configure:

build-arch: build
build-indep: build

build:

clean:
	dh_testdir
	dh_testroot
	dh_clean

install:
	dh_testdir
	dh_testroot
	dh_prep
	mkdir -p $(DESTDIR)/
	cp -Rdaux index.php application system modules $(DESTDIR)/
	echo $(INSTDIR)
	sed -i "/'APPPATH'/ s|realpath([^)]*)|'$(INSTDIR)/application'|" $(DESTDIR)/index.php
	sed -i "/'MODPATH'/ s|realpath([^)]*)|'$(INSTDIR)/modules'|" $(DESTDIR)/index.php
	sed -i "/'SYSPATH'/ s|realpath([^)]*)|'$(INSTDIR)/system'|" $(DESTDIR)/index.php
	rm -f $(DESTDIR)/modules/image/media/guide/image/Thumbs.db
	rm -f $(DESTDIR)/modules/userguide/media/guide/js/jquery.cookie.js
	rm -f $(DESTDIR)/modules/userguide/media/guide/js/jquery.min.js
	rm -f $(DESTDIR)/modules/userguide/media/guide/js/shCore.js
	rm -f $(DESTDIR)/modules/userguide/vendor/markdown/License.text
	dh_link -plibkohana$(MAJOR_VERSION)-mod-userguide-php \
		/usr/share/javascript/jquery/jquery.min.js \
		/usr/share/php/kohana$(MAJOR_VERSION)/modules/userguide/media/guide/js/jquery.min.js
	dh_link -plibkohana$(MAJOR_VERSION)-mod-userguide-php \
		/usr/share/javascript/jquery-cookie/jquery.cookie.js \
		/usr/share/php/kohana$(MAJOR_VERSION)/modules/userguide/media/guide/js/jquery.cookie.js
	dh_link -plibkohana$(MAJOR_VERSION)-mod-userguide-php \
		/usr/share/php/kohana$(MAJOR_VERSION)/modules/userguide/media/guide/js/shBrushPhp.js \
		/usr/share/php/kohana$(MAJOR_VERSION)/modules/userguide/media/guide/js/shCore.js
	dh_install
	dh_installexamples
	dh_lintian

binary-arch: build install

binary-indep: build install
	dh_testdir
	dh_testroot
	dh_installdocs -i
	dh_installchangelogs -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

binary: binary-indep binary-arch

get-orig-source:
	uscan --force --verbose --repack --rename

clean-source:
	# Remove compressed JavaScript libraries
	rm -f modules/userguide/media/guide/js/jquery.min.js
	rm -f modules/userguide/media/guide/js/shCore.js
	# Recreate upstream tarball
	tar zcvf ../libkohana$(MAJOR_VERSION)-php_$(VERSION).orig.tar.gz \
		../libkohana$(MAJOR_VERSION)-php-$(VERSION)/ \
		--exclude='debian/*' --exclude='debian'

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