#!/bin/bash
set -e

NAME=eclipse-wtp
VERSION=3.6.3
DEB_VERSION=${VERSION}

WTP_MODULES="webtools-common/webtools.common
webtools-common/webtools.common.fproj
webtools-common/webtools.common.snippets
jeetools/webtools.javaee
jsdt/webtools.jsdt
servertools/webtools.servertools
servertools/webtools.servertools.docs
sourceediting/webtools.sourceediting
sourceediting/webtools.sourceediting.xpath
sourceediting/webtools.sourceediting.xsl
webservices/webtools.webservices
cbi/org.eclipse.license:license-1.0.1.v20140414-1359:org.eclipse.license
"

ECLIPSE_GIT_WEB=http://git.eclipse.org/c

TMP_DIR=`mktemp -d`
WORK_DIR=$PWD

VERSION_UNDERSCORE=$(echo $VERSION | sed "s/\./_/g")

moveBundlesFiltered() {
	BUNDLE_DIR=$1
	BUNDLE_FILTER=$2

	BUNDLES=$(find $BUNDLE_DIR -maxdepth 1 -mindepth 1 -type d -name "org.eclipse.*" \
		! -name features ! -name plugins ! -name bundles ! -name docs)

	for bundle in $BUNDLES; do
		if [ "$BUNDLE_FILTER" != "" ] && [ $(basename "$bundle") != "$BUNDLE_FILTER" ]; then
			continue;
		fi
		mv $bundle .
	done
}

downloadSnapshot() {
	read MODULE_PATH MODULE_TAG MODULE_FILTER \
		<<<$(echo $1 | cut -d: -f1,2,3 --output-delimiter=" ")

	if [ "$MODULE_TAG" == "" ]; then 
		MODULE_TAG=R$VERSION_UNDERSCORE
	fi

	MODULE_NAME=$(echo $MODULE_PATH | sed "s/[^\/]\+\/\(.*\)/\1/g")
	MODULE_DIR=$MODULE_NAME-$MODULE_TAG
	TARBALL=$MODULE_DIR.tar.bz2

	wget "$ECLIPSE_GIT_WEB/$MODULE_PATH.git/snapshot/$TARBALL"
	tar xf $TARBALL
	rm -f $TARBALL

	for subdir in . features plugins bundles docs; do
		if [ -d $MODULE_DIR/$subdir ]; then
			moveBundlesFiltered "$MODULE_DIR/$subdir" "$MODULE_FILTER"
		fi
	done

	rm -r $MODULE_DIR
}

cd "$TMP_DIR"

mkdir modules
cd modules

for MODULE in $WTP_MODULES; do
	downloadSnapshot $MODULE
done

cd ..

mkdir ${NAME}-${VERSION}

while read LINE ; do
	echo "Picking $LINE"
	mv modules/$LINE ${NAME}-${VERSION}
done < "$WORK_DIR/debian/wtpbundles"

rm -rf modules

cd ${NAME}-${VERSION}

find -type f -name .cvsignore -delete
find -type f -name .gitignore -delete
find -type f -name Thumbs.db -delete
rm -f org.eclipse.jem.util/org.eclipse.perfmsr.core.stub/perfmsr.jar

rm -rf org.eclipse.wst.command.env.doc.user/index \
	org.eclipse.wst.command.env.doc.user/topics/*.html \
	org.eclipse.wst.command.env.doc.user/plugin.xml \
	org.eclipse.wst.command.env.doc.user/wsaxisant_toc.xml

rm -rf org.eclipse.wst.dtdeditor.doc.user/index \
	org.eclipse.wst.dtdeditor.doc.user/topics/*.html \
	org.eclipse.wst.dtdeditor.doc.user/DTDreftable.xml \
	org.eclipse.wst.dtdeditor.doc.user/dita.list \
	org.eclipse.wst.dtdeditor.doc.user/plugin.xml

rm -rf org.eclipse.wst.server.ui.doc.user/index \
	org.eclipse.wst.server.ui.doc.user/topics/*.html \
	org.eclipse.wst.server.ui.doc.user/topics/tdeactivateactivate.dita \
	org.eclipse.wst.server.ui.doc.user/plugin.xml \
	org.eclipse.wst.server.ui.doc.user/wtp_reltable.xml

rm -rf org.eclipse.wst.sse.doc.user/index \
	org.eclipse.wst.sse.doc.user/topics/*.html \
	org.eclipse.wst.sse.doc.user/plugin.xml \
	org.eclipse.wst.sse.doc.user/sse_toc.xml

rm -rf org.eclipse.wst.webtools.doc.user/index \
	org.eclipse.wst.webtools.doc.user/topics/*.html \
	org.eclipse.wst.webtools.doc.user/plugin.xml

rm -rf org.eclipse.wst.wsdl.ui.doc.user/concepts/cwsdled.html \
	org.eclipse.wst.wsdl.ui.doc.user/index \
	org.eclipse.wst.wsdl.ui.doc.user/tasks/*.html \
	org.eclipse.wst.wsdl.ui.doc.user/wsdl_build_toc.xml \
	org.eclipse.wst.wsdl.ui.doc.user/plugin.xml

rm -rf org.eclipse.wst.wsi.ui.doc.user/index \
	org.eclipse.wst.wsi.ui.doc.user/tasks/*.html \
	org.eclipse.wst.wsi.ui.doc.user/wswsitest_toc.xml \
	org.eclipse.wst.wsi.ui.doc.user/plugin.xml

rm -rf org.eclipse.wst.xmleditor.doc.user/index \
	org.eclipse.wst.xmleditor.doc.user/topics/*.html \
	org.eclipse.wst.xmleditor.doc.user/XMLBuilderrel.xml \
	org.eclipse.wst.xmleditor.doc.user/plugin.xml

rm -rf org.eclipse.wst.xsdeditor.doc.user/index \
	org.eclipse.wst.xsdeditor.doc.user/topics/*.html \
	org.eclipse.wst.xsdeditor.doc.user/XSDLeditorrel.xml \
	org.eclipse.wst.xsdeditor.doc.user/plugin.xml

rm -f org.eclipse.wst.jsdt.core/src/org/eclipse/wst/jsdt/internal/compiler/parser/parser*.rsc \
	org.eclipse.wst.jsdt.core/src/org/eclipse/wst/jsdt/internal/compiler/parser/readableNames.properties

rm -rf org.eclipse.wst.ws.explorer/servletapi \
	org.eclipse.wst.ws.explorer/wsexplorer/WEB-INF

rm -f org.eclipse.wst.standard.schemas/dtds/HTMLlat1.ent \
	org.eclipse.wst.standard.schemas/dtds/HTMLspecial.ent \
	org.eclipse.wst.standard.schemas/dtds/HTMLsymbol.ent \
	org.eclipse.wst.standard.schemas/dtds/wml11.dtd \
	org.eclipse.wst.standard.schemas/dtds/wml13.dtd \
	org.eclipse.wst.standard.schemas/dtds/xhtml-lat1.ent \
	org.eclipse.wst.standard.schemas/dtds/xhtml-mobile10-flat.dtd \
	org.eclipse.wst.standard.schemas/dtds/xhtml-special.ent \
	org.eclipse.wst.standard.schemas/dtds/xhtml-symbol.ent \
	org.eclipse.wst.standard.schemas/dtds/xhtml1-frameset.dtd \
	org.eclipse.wst.standard.schemas/dtds/xhtml1-strict.dtd \
	org.eclipse.wst.standard.schemas/xsd/http.xsd \
	org.eclipse.wst.standard.schemas/xsd/soap.xsd \
	org.eclipse.wst.standard.schemas/xsd/wsdl-mime.xsd \
	org.eclipse.wst.standard.schemas/xsd/wsdl.xsd \
	org.eclipse.wst.standard.schemas/xsd/xmlcatalog11.xsd

rm -f org.eclipse.wst.xml.core/src/org/eclipse/wst/xml/core/internal/contenttype/XMLHeadTokenizer.java \
	org.eclipse.wst.xml.core/src/org/eclipse/wst/xml/core/internal/parser/XML10Names.java

rm -rf org.eclipse.wst.xml.xpath2.processor/grammars/generated \
	org.eclipse.wst.xml.xpath2.processor/lib \
	org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/XPathCup.java \
	org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/XPathFlex.java \
	org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/XpathSym.java

rm -f org.eclipse.wst.xsl.core/lib/xinclude.jar

rm -rf org.eclipse.wst.xsl.core/lib

cd ..

echo "Creating tarball '${NAME}_${DEB_VERSION}.orig.tar.xz'..."
tar -cJf "$WORK_DIR/../${NAME}_${DEB_VERSION}.orig.tar.xz" $NAME-$VERSION

rm -rf "$TMP_DIR"
