#!/bin/bash
set -e

NAME=eclipse-ptp
VERSION=8.0.0
DEB_VERSION=${VERSION}

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

rm -rf ${NAME}-${DEB_VERSION}

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

TARBALL=${REPO_NAME}-PTP_${VERSION_UNDERSCORE}.tar.bz2

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

mv ${REPO_NAME}-PTP_${VERSION_UNDERSCORE} ${NAME}-${DEB_VERSION}
cd ${NAME}-${DEB_VERSION}

find -type f -name .gitignore -delete
find -type f -name .cvsignore -delete

rm -rf pom.xml debug rms workspace

find releng -mindepth 1 -maxdepth 1 | \
	grep -v -e org.eclipse.ptp.core-feature \
		-e org.eclipse.ptp.services-feature \
		-e org.eclipse.ptp.remote-feature \
		-e org.eclipse.ptp.remote.remotetools-feature \
		-e org.eclipse.ptp.remote.rse-feature \
		-e org.eclipse.ptp.remotetools-feature \
		-e org.eclipse.ptp.rdt-feature \
		-e org.eclipse.ptp.rdt.editor-feature | xargs rm -rf

find core -mindepth 1 -maxdepth 1 | \
	grep -v -e org.eclipse.ptp.core$ \
		-e org.eclipse.ptp.remote.core \
		-e org.eclipse.ptp.remote.rse.core \
		-e org.eclipse.ptp.remote.rse.ui \
		-e org.eclipse.ptp.remote.remotetools.core \
		-e org.eclipse.ptp.remote.remotetools.ui \
		-e org.eclipse.ptp.remote.server.core \
		-e org.eclipse.ptp.remote.ui \
		-e org.eclipse.ptp.services.core \
		-e org.eclipse.ptp.services.ui \
		-e org.eclipse.ptp.ui | xargs rm -rf

find tools -mindepth 1 -maxdepth 1 |
	grep -v -e remotetools | xargs rm -rf

cd rdt
rm -rf org.eclipse.ptp.rdt.core.remotejars \
	org.eclipse.ptp.rdt.core.stubs \
	org.eclipse.ptp.rdt.core.tests \
	org.eclipse.ptp.rdt.managedbuilder.xlc.ui \
	org.eclipse.ptp.rdt.managedbuilder.xlupc.ui \
	org.eclipse.ptp.rdt.server.dstore \
	org.eclipse.ptp.rdt.sync.* \
	org.eclipse.ptp.rdt.ui.tests \
	org.eclipse.ptp.rdt.xlc
cd ..

# remove empty directories
find . -depth -type d -empty -delete

cd ..

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

rm -rf ${NAME}-${DEB_VERSION}
