#!/usr/bin/make -f
#Licensed to the Apache Software Foundation (ASF) under one
#or more contributor license agreements.  See the NOTICE file
#distributed with this work for additional information
#regarding copyright ownership.  The ASF licenses this file
#to you under the Apache License, Version 2.0 (the
#"License"); you may not use this file except in compliance
#with the License.  You may obtain a copy of the License at
#
#http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing,
#software distributed under the License is distributed on an
#"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
#KIND, either express or implied.  See the License for the
#specific language governing permissions and limitations
#under the License.

DH_VERBOSE	:=
SHELL		:= /bin/bash -O extglob

export DH_VERBOSE

JAVA_HOME	:= /usr/lib/jvm/java-6-sun
JAVAC		:= $(JAVA_HOME)/bin/javac
JAVA		:= $(JAVA_HOME)/bin/java

export JAVA_HOME
UPSTREAM_VERSION := $(shell dpkg-parsechangelog | awk 'BEGIN{FS="[ -]";}/^Version:/{print $$2}')

merge:
	# This does *not* support checkout paths with spaces in them
	# caveat empto
	# TODO: svn
	set -ex;set -- `(svk info . 2>/dev/null || true) | awk -F '[ ,]+' '/^(Depot Path|Mirrored From): /{print $$3}'`;\
	if [ "$$1" ]; then \
		if [ "$$2" ]; then svk sync "$$1"; fi; \
		svk up; \
		set -- `svk info . 2>/dev/null | awk -F '[ ,]+' '/^Mirrored From: /{print $$3, $$5}'`;\
		if [ "$$1" ] && [ "$$2" ] && [ "$(UPSTREAM_VERSION)" -ne "$$2" ]; then \
			dch -v "$$2-1" -m "New upstream version."; \
		fi \
	fi

make-orig:
	set -ex; \
	process_orig_dir() { \
		wait; \
		cp -a $$1/changelog changelog; \
		mv $$1/changelog "$$1/ofbiz-$$2/changelog"; \
		tar -C $$1 -c "ofbiz-$$2" | gzip -9 > "$$1/ofbiz_$$2.orig.tar.gz"; \
		mv "$$1/ofbiz_$$2.orig.tar.gz" ..; \
		rm -rf $$1; \
	}; \
	if [ -d .git ]; then \
		rm -rf debian/orig-work; mkdir -p debian/orig-work/ofbiz-$(UPSTREAM_VERSION); \
		treeish=`git svn find-rev r$(UPSTREAM_VERSION)`;\
		git svn log -v -r "$(UPSTREAM_VERSION):0" > debian/orig-work/changelog & \
		(cd debian/orig-work; git clone -n ../.. ofbiz-$(UPSTREAM_VERSION); cd ofbiz-$(UPSTREAM_VERSION); git checkout $$treeish; rm -rf .git); \
		process_orig_dir debian/orig-work "$(UPSTREAM_VERSION)"; \
		exit; \
	fi; \
	set -- `(svk info . 2>/dev/null || true) | awk -F '[ ,]+' '/^(Depot Path|Mirrored From): /{print $$3, $$5}'`;\
	if [ "$$1" ]; then \
		rm -rf ../orig; mkdir -p ../orig; \
		svk log -v > ../orig/changelog & \
		svk co --export "$$1" "../orig/ofbiz-$$3" & \
		process_orig_dir ../orig $$3; \
	fi \

clean-debian:
	dh_clean
	rm -f debian/EncryptPassword.class
	rm -rf runtime/catalina/work debian/stamps debian/copyright

clean: clean-debian
	$(SHELL) ant clean-all
	debconf-updatepo
	rm -rf debian/orig-work

build: debian/stamps/build-ofbiz debian/EncryptPassword.class
build-ofbiz: debian/stamps/build-ofbiz
binary-indep: debian/stamps/build-ofbiz
debian/stamps/build-ofbiz:
	$(SHELL) ant
	@mkdir -p $(@D) && touch $@

binary-indep: debian/EncryptPassword.class
debian/EncryptPassword.class: %.class: %.java
debian/EncryptPassword.class: debian/stamps/build-ofbiz
	$(JAVAC) -classpath framework/base/build/lib/ofbiz-base.jar:framework/common/build/lib/ofbiz-common.jar -d $(@D) $*.java

debian/copyright: debian/copyright.in LICENSE
	cat debian/copyright.in > $@.new
	sh debian/strip-common-licenses.sh >> $@.new
	mv $@.new $@

binary-indep: DH_OPTIONS=-i
binary-indep: debian/copyright
	dh_testdir
	dh_testroot
	dh_clean -k
	mkdir -p runtime/catalina/work
	perl debian/move-files.pl
	dh_installdirs -v
	cp -a debian/*.xslt debian/*.class debian/ofbiz-framework/usr/share/ofbiz/support
	dh_link
#	tar -c . --exclude debian --exclude src --exclude build.xml --exclude build/classes --exclude framework/example | tar -C debian/ofbiz-svn-$(UPSTREAM_VERSION)/var/lib/ofbiz/$(UPSTREAM_VERSION) -x
#	dh_strip
#	dh_shlibdeps
	dh_installinit -N ofbiz-framework
	dh_installinit -p ofbiz-framework --name ofbiz -r --no-start
	dh_installchangelogs
	dh_installlogrotate
	dh_installdebconf
	dh_installdocs
	dh_installdeb
	dh_fixperms
	dh_compress
	dh_md5sums
	dh_gencontrol
	dh_builddeb

binary-arch:

binary: binary-indep binary-arch

.EXPORT_ALL_VARIABLES:

