#!/usr/bin/make -f

DEB_HOST_ARCH := $(shell dpkg-architecture -qDEB_HOST_ARCH)

export JAVA_HOME=/usr/lib/jvm/java-6-openjdk-$(DEB_HOST_ARCH)

#package names
pkg-platform			:= platform13
pkg-lib-platform		:= libnb-$(pkg-platform)
libnb-platform-java		:= $(pkg-lib-platform)-java
libnb-platform-java-doc 	:= $(pkg-lib-platform)-java-doc
libnb-platform-devel-java	:= libnb-platform-devel-java

# Included clusters
pkg-nb                         := netbeans
base-apisupport                := apisupport3
base-ide                       := ide14
base-java                      := java5
base-nb                        := nb
base-platform                  := platform13

pkg-apisupport                 := libnb-$(base-apisupport)-java
pkg-ide                        := libnb-$(base-ide)-java
pkg-java                       := libnb-$(base-java)-java


NBDIR          := /usr/share/netbeans
export JAVADIR := /usr/share/java

JH_JAR          := jhall.jar
JSEARCH_JAR     := jsearch.jar
NB_JH_JAR       := jhall-2.0_05.jar
NB_JSEARCH_JAR  := jsearch-2.0_05.jar

JAVADOC_WEBSITE		:= http://bits.netbeans.org/7.0/javadoc

NB_INSTALL_DIR := usr/share/netbeans

EXT_JAR_LINK_DIR	:= $(NB_INSTALL_DIR)/$(pkg-platform)/modules/ext/
LIB_DIR			:= $(NB_INSTALL_DIR)/$(pkg-platform)/lib/

INSTALL        := dh_install -p
LINK           := dh_link -p

NB_PLATFORM_BUILD_DIR := $(shell pwd)/nbbuild/netbeans/platform

DEB_UPSTREAM_VERSION = $(shell dpkg-parsechangelog | \
			       grep -e '^Version' | \
                               perl -pi -e 's/^Version:\s+(.*)\+dfsg\d+-\d+.*$$/$$1/')

# Defined function to be used to create symlinks
define symlink
	mkdir -p $(dir $(2))
	ln -sf $(1) $(2)
endef

%:
	dh $@ --with javahelper

#link:
#	$(shell awk 'NF==4 {printf("echo ln -sf %s %s/%s;\n",$$1,$$2,$$4)}' debian/linked-jars)
#	$(shell awk 'NF==4 {printf("echo ln -sf %s nbbuild/netbeans/%s/%s;\n",$$1,$$3,$$4)}' debian/linked-jars)

override_dh_auto_build:
	mkdir -p nbbuild/external/xhtml1-dtds
	ln -sf /usr/share/xml/xhtml/schema/dtd/1.0/xhtml* nbbuild/external/xhtml1-dtds
	ln -sf /usr/share/xml/entities/xhtml/xhtml* nbbuild/external/xhtml1-dtds
	# link the required jars
	$(shell awk 'NF==4 {printf("ln -sf %s %s/%s;\n",$$1,$$2,$$4)}' debian/linked-jars)
	######## Remove services and org/eclipse/* from equinox osgi.jar#####################
#	mkdir debian/jar_tmp;  cd debian/jar_tmp; \
#	jar -xf $(JAVADIR)/org.eclipse.osgi.jar; \
#	rm -rf META-INF/services org/eclipse; \
#	jar -cf ../../libs.osgi/external/osgi.core-4.2.jar ./*;
#	rm -rf debian/jar_tmp
#TODO	$(call symlink, /usr/share/ant/lib, o.apache.tools.ant.module/external/lib)
	################################################################
	# Build the Windows launcher
	cd o.n.bootstrap/launcher/windows && LDFLAGS="" make .build-impl nbexec.exe
	cd apisupport.harness/windows-launcher-src && LDFLAGS="" make
	################################################################
	# Build the ide. DISPLAY must be unset to avoid build failures.
	unset DISPLAY; ant \
	-Dpermit.jdk6.builds=true \
	-Dbuild.compiler.deprecation=false \
	-Dbuild.compiler.debug=false \
	-Dext.binaries.downloaded=true \
	-Dverify.checkout=false \
	-Dcluster.config=basic \
	-f nbbuild/build.xml build-nozip
	################################################################
	# Build javadoc package
	ant \
	-Dbuild.compiler.deprecation=false \
	-Dbuild.compiler.debug=false \
	-Dverify.checkout=false \
	-Dallmodules= \
	-Dcluster.config=platform \
	-Dconfig.javadoc.cluster=$(pkg-base) \
	-Djavadoc.docs.org-netbeans-api-java=$(JAVADOC_WEBSITE)/org-netbeans-api-java/ \
	-Djavadoc.docs.org-netbeans-modules-project-ant=$(JAVADOC_WEBSITE)/org-netbeans-modules-project-ant/ \
	-Djavadoc.docs.org-netbeans-modules-projectapi=$(JAVADOC_WEBSITE)/org-netbeans-modules-projectapi/ \
	-f nbbuild/build.xml build-javadoc
	# remove the zip files - we only want the extracted files
	rm -f nbbuild/build/javadoc/*.zip



override_dh_auto_install:
	# recreate links
	$(shell awk 'NF==4 {printf("ln -sf %s nbbuild/netbeans/%s/%s;\n",$$1,$$3,$$4)}' debian/linked-jars)
	# Install libraries
	mh_installjar -plibnb-org-openide-util-java -l debian/pom/openide-util.xml \
		nbbuild/netbeans/platform/lib/org-openide-util.jar
	mh_installpom -plibnb-org-openide-util-java debian/pom/openide-util.xml
	mh_installjar -plibnb-org-openide-util-lookup-java -l debian/pom/openide-util-lookup.xml \
		nbbuild/netbeans/platform/lib/org-openide-util-lookup.jar
	mh_installpom -plibnb-org-openide-util-lookup-java debian/pom/openide-util-lookup.xml
	mh_installjar -plibnb-org-openide-modules-java -l debian/pom/openide-modules.xml \
		nbbuild/netbeans/platform/lib/org-openide-modules.jar
	mh_installpom -plibnb-org-openide-modules-java debian/pom/openide-modules.xml
	mh_installjar -plibnb-absolutelayout-java -l debian/pom/AbsoluteLayout.xml \
		nbbuild/netbeans/java/modules/ext/AbsoluteLayout.jar
	mh_installpom -plibnb-absolutelayout-java debian/pom/AbsoluteLayout.xml
	#
	# Install Platform
	echo > nbbuild/netbeans/platform/.noautoupdate
	echo > nbbuild/netbeans/platform/.lastModified
	$(INSTALL)$(libnb-platform-java)   nbbuild/netbeans/platform/* $(NB_INSTALL_DIR)/$(pkg-platform)
	$(INSTALL)$(libnb-platform-java)   nbbuild/netbeans/platform/.noautoupdate $(NB_INSTALL_DIR)/$(pkg-platform)
	$(INSTALL)$(libnb-platform-java)   nbbuild/netbeans/platform/.lastModified $(NB_INSTALL_DIR)/$(pkg-platform)
	$(LINK)$(libnb-platform-java) $(JAVADIR)/org-openide-util.jar        $(LIB_DIR)/org-openide-util.jar
	$(LINK)$(libnb-platform-java) $(JAVADIR)/org-openide-util-lookup.jar $(LIB_DIR)/org-openide-util-lookup.jar
	$(LINK)$(libnb-platform-java) $(JAVADIR)/org-openide-modules.jar     $(LIB_DIR)/org-openide-modules.jar
	#
	# Install devel package
	rm -f nbbuild/netbeans/harness/antlib/$(NB_JSEARCH_JAR)
	echo > nbbuild/netbeans/harness/.noautoupdate
	echo > nbbuild/netbeans/harness/.lastModified
	$(INSTALL)$(libnb-platform-devel-java)   nbbuild/netbeans/harness/* $(NB_INSTALL_DIR)/harness
	$(INSTALL)$(libnb-platform-devel-java)   nbbuild/netbeans/harness/.noautoupdate $(NB_INSTALL_DIR)/harness
	$(INSTALL)$(libnb-platform-devel-java)   nbbuild/netbeans/harness/.lastModified $(NB_INSTALL_DIR)/harness
	#
	#Fixing permissions
	chmod 0755 debian/$(libnb-platform-devel-java)/$(NB_INSTALL_DIR)/harness/launchers/app.sh
	#
	# Install IDE package files
	echo > nbbuild/netbeans/ide/.noautoupdate
	echo > nbbuild/netbeans/ide/.lastModified
#	#all files are removed from lib because they are in dll formats
#	rm -rf nbbuild/netbeans/$(base-ide)/modules/lib
	$(INSTALL)$(pkg-ide) nbbuild/netbeans/ide/*               $(NB_INSTALL_DIR)/$(base-ide)
	$(INSTALL)$(pkg-ide) nbbuild/netbeans/ide/.noautoupdate   $(NB_INSTALL_DIR)/$(base-ide)
	$(INSTALL)$(pkg-ide) nbbuild/netbeans/ide/.lastModified   $(NB_INSTALL_DIR)/$(base-ide)

	# Install Java package files
	echo > nbbuild/netbeans/java/.noautoupdate
	echo > nbbuild/netbeans/java/.lastModified
#	#all doc files are removed because they are in zip and jar formats
#	rm -rf nbbuild/netbeans/java/docs
	$(INSTALL)$(pkg-java) nbbuild/netbeans/java/*             $(NB_INSTALL_DIR)/$(base-java)
	$(INSTALL)$(pkg-java) nbbuild/netbeans/java/.noautoupdate $(NB_INSTALL_DIR)/$(base-java)
	$(INSTALL)$(pkg-java) nbbuild/netbeans/java/.lastModified $(NB_INSTALL_DIR)/$(base-java)
	# Link in Ant libraries and binaries
	-rmdir debian/$(pkg-java)/$(NB_INSTALL_DIR)/$(base-java)/ant/lib
	$(LINK)$(pkg-java)    usr/share/ant/bin 	$(NB_INSTALL_DIR)/$(base-java)/ant/bin
	$(LINK)$(pkg-java)    usr/share/ant/etc 	$(NB_INSTALL_DIR)/$(base-java)/ant/etc
	$(LINK)$(pkg-java)    usr/share/ant/lib 	$(NB_INSTALL_DIR)/$(base-java)/ant/lib
	# Link in AbsoluteLayout jar
	-rm debian/$(pkg-java)/$(NB_INSTALL_DIR)/$(base-java)/modules/ext/AbsoluteLayout.jar
	$(LINK)$(pkg-java)    usr/share/java/AbsoluteLayout.jar  $(NB_INSTALL_DIR)/$(base-java)/modules/ext/AbsoluteLayout.jar
#
	# Install API support package files
	echo > nbbuild/netbeans/apisupport/.noautoupdate
	echo > nbbuild/netbeans/apisupport/.lastModified
	$(INSTALL)$(pkg-apisupport) nbbuild/netbeans/apisupport/*              $(NB_INSTALL_DIR)/$(base-apisupport)
	$(INSTALL)$(pkg-apisupport) nbbuild/netbeans/apisupport/.noautoupdate  $(NB_INSTALL_DIR)/$(base-apisupport)
	$(INSTALL)$(pkg-apisupport) nbbuild/netbeans/apisupport/.lastModified  $(NB_INSTALL_DIR)/$(base-apisupport)
#	# Install Netbeans package files
	echo > nbbuild/netbeans/nb/.noautoupdate
	$(INSTALL)$(pkg-nb) nbbuild/netbeans/nb/*               $(NB_INSTALL_DIR)/$(DEB_UPSTREAM_VERSION)/nb
	$(INSTALL)$(pkg-nb) nbbuild/netbeans/nb/.lastModified   $(NB_INSTALL_DIR)/$(DEB_UPSTREAM_VERSION)/nb
	$(INSTALL)$(pkg-nb) nbbuild/netbeans/nb/.noautoupdate   $(NB_INSTALL_DIR)/$(DEB_UPSTREAM_VERSION)/nb
	$(INSTALL)$(pkg-nb) nbbuild/netbeans/bin/*              $(NB_INSTALL_DIR)/$(DEB_UPSTREAM_VERSION)/bin
	$(INSTALL)$(pkg-nb) nbbuild/netbeans/CREDITS.html                 usr/share/$(pkg-nb)
	$(INSTALL)$(pkg-nb) nbbuild/netbeans/README.html                  usr/share/$(pkg-nb)
	$(INSTALL)$(pkg-nb) nbbuild/netbeans/netbeans.css                 usr/share/$(pkg-nb)
	$(LINK)$(pkg-nb)    $(NB_INSTALL_DIR)/$(base-platform) $(NB_INSTALL_DIR)/$(DEB_UPSTREAM_VERSION)/platform
	$(LINK)$(pkg-nb)    $(NB_INSTALL_DIR)/harness          $(NB_INSTALL_DIR)/$(DEB_UPSTREAM_VERSION)/harness
	$(LINK)$(pkg-nb)    $(NB_INSTALL_DIR)/$(base-apisupport) $(NB_INSTALL_DIR)/$(DEB_UPSTREAM_VERSION)/apisupport
	$(LINK)$(pkg-nb)    $(NB_INSTALL_DIR)/$(base-ide)        $(NB_INSTALL_DIR)/$(DEB_UPSTREAM_VERSION)/ide
	$(LINK)$(pkg-nb)    $(NB_INSTALL_DIR)/$(base-java)       $(NB_INSTALL_DIR)/$(DEB_UPSTREAM_VERSION)/java

	# Create file marker to avoid displaying license dialog at first IDE start
	echo > debian/license_accepted
	$(INSTALL)$(pkg-nb) debian/license_accepted $(NB_INSTALL_DIR)/$(DEB_UPSTREAM_VERSION)/nb/var
	# Install file productid used by autoupdate to report installation type
	$(INSTALL)$(pkg-nb) debian/productid 	  $(NB_INSTALL_DIR)/$(DEB_UPSTREAM_VERSION)/nb/config
#
	# Install netbeans.conf
	$(INSTALL)$(pkg-nb) nbbuild/netbeans/etc/netbeans.conf etc
	$(LINK)$(pkg-nb) etc/netbeans.conf 	$(NB_INSTALL_DIR)/$(DEB_UPSTREAM_VERSION)/etc/netbeans.conf
#
	# Install netbeans.clusters
	$(INSTALL)$(pkg-nb) nbbuild/netbeans/etc/netbeans.clusters etc
	$(LINK)$(pkg-nb) etc/netbeans.clusters 	$(NB_INSTALL_DIR)/$(DEB_UPSTREAM_VERSION)/etc/netbeans.clusters
#
#	# Save original version of netbeans.conf as postinst patches it
#	#cp nbbuild/netbeans/etc/netbeans.conf 	  nbbuild/netbeans/etc/netbeans.conf.orig
#	#$(INSTALL)$(pkg-nb) nbbuild/netbeans/etc/netbeans.conf.orig $(NB_INSTALL_DIR)/$(DEB_UPSTREAM_VERSION)/etc
#
	# Install launcher
	$(INSTALL)$(pkg-nb) nbbuild/netbeans/bin/netbeans usr/bin
	# Install desktop file
	$(INSTALL)$(pkg-nb) debian/netbeans.desktop usr/share/applications


override_dh_auto_clean:
	-rm -f *.cdbs-config_list
	-rm -f stamp-build-$(libnb-platform-java)
	-rm -f stamp-build-$(libnb-platform-devel-java)
	-rm -f stamp-build-$(libnb-platform-java-doc)
	$(call symlink, $(JAVADIR)/$(JH_JAR), javahelp/external/$(NB_JH_JAR))
	$(call symlink, $(JAVADIR)/$(JSEARCH_JAR), apisupport.harness/external/$(NB_JSEARCH_JAR))
	ant -Dpermit.jdk6.builds=true -f nbbuild/build.xml clean
	cd o.n.bootstrap/launcher/windows && make clean
	cd apisupport.harness/windows-launcher-src && make clean
	mh_clean
# remove all the symlinks to jar files we created
	find . -name "*.jar" -type l -execdir rm -f \{\} \;
#	-rm -f libs.osgi/external/osgi.core-4.2.jar
	-rm -rf nbbuild/external/xhtml1-dtds
	-rm -f  nbbuild/nbproject/private/scan-cache-standard.ser
#	-rm -f  nbbuild/nbantext.jar
#	-rm -rf debian/jar_tmp
	-rm -f debian/license_accepted
	# these files are not deleted by ant clean though it should so delete here
	-rm -f swingapp/src/org/netbeans/modules/swingapp/resources/CRUDShellApp.zip
	-rm -f swingapp/src/org/netbeans/modules/swingapp/resources/BasicShellApp.zip
# TODO - shouldn't have to do this
#	-rm -rf */build
#
#	-rm -f stamp-build-$(pkg-nb)
#	-rm -f nbbuild/netbeans/platform
#	-rm -f nbbuild/netbeans/harness
#	-rm -f o.apache.tools.ant.module/external/lib

override_jh_depends:
	# Skipping jh_depends run - since we use symlinks to /usr/share/java then 
	# jh_depends gets it wrong.

get-orig-source:
	uscan --download-version $(subst +dfsg\d*,,$(DEB_UPSTREAM_VERSION)) --force-download
