#!/bin/bash
#
# Helper script for fetching and constructing a delta pack SDK file
# from p2 repository for Eclipse 4.5 Mars release and later
#

if [ "$#" -ne 1 ]
then
    echo "Usage: $0 p2-repository-url"
fi

url="$1"
[[ $(basename $url) =~ ^.+-(.+)-.+ ]] && release=${BASH_REMATCH[1]}

echo "Fetching release $release from $url"

dir="delta-pack-$release"
mkdir $dir

echo "Downloading files to $dir"

cd $dir

mkdir -p eclipse/features
mkdir -p eclipse/plugins

cd eclipse/features
wget -c -r -l 1 -nd -np -nv -A "org.eclipse.equinox.executable*.jar" "$url/features/?d"
cd ../..

cd eclipse/plugins
wget -c -r -l 1 -nd -np -nv --accept-regex ".*(cocoa|win32|gtk|linux|macosx).*.jar$" "$url/plugins/?d" -R "org.eclipse.test.performance*"
cd ../..

for f in eclipse/features/org.eclipse.equinox.executable_*.jar eclipse/plugins/org.eclipse.equinox.launcher.*.jar
do
	echo "Unpacking $f"
	unpack_dir="${f/.jar}"
	rm -rf "$unpack_dir"
	unzip -d "$unpack_dir" "$f"
	rm "$f"
done

zip=eclipse-$release-delta-pack.zip

rm $zip
zip -r $zip eclipse

cd ..

#rm -rf $dir
