#! /bin/bash

# (C) Copyright IBM Corp. 2002
# $Id: unpackPlugin,v 1.2 2002/10/29 21:48:33 dolby-oss Exp $
# @author David Hovemeyer

# Given the name of a plugin (e.g., org.eclipse.ui_2.0.0),
# this script will attempt to unpack its source, create a symlink
# to the plugin's source directory, and generate shell scripts
# which compile and install the plugin.  It should be run
# from the root "eclipse" directory, i.e., the one containing the
# "plugins" directory.

plugin_name=$1
jarfile=$2
if [ -z "$plugin_name" ]; then
    echo "Usage: unpackPlugin <pluginName> [<jar filename>]"
    echo "Specify the name of the plugin jar file if there are multiple"
    echo "jar files in the plugin directory."
    exit 1
fi

if [ ! -d plugins ]; then
    echo "Are you in the eclipse directory?"
    exit 1
fi

eclipse_root=`pwd`

count() {
    list=$1
    result=0
    for elt in $list; do
	result=`expr $result + 1`
    done
    echo $result
}

# Find the plugin's jar file.
plugin_dir=plugins/$plugin_name
if [ ! -z "$jarfile" ]; then
    jarfile="$plugin_dir/$jarfile"
    if [ ! -f $jarfile ]; then
	echo "Can't find jar file $jarfile"
	exit 1
    fi
else
    jarfile_list=`ls $plugin_dir/*.jar`
    num_jarfiles=`count "$jarfile_list"`
    if [ $num_jarfiles -ne 1 ]; then
	echo "Wrong number of jar files ($num_jarfiles) detected in $plugin_dir"
	exit 1
    fi
    jarfile=$jarfile_list
fi
echo "Found jar file: $jarfile"

# Extract the short name of the jarfile, which should match the
# name of the corresponding source zip file.
shortname=`basename $jarfile .jar`

# Find the plugin's source file.
possible_src_dirs=`find plugins \( -name src -a -type d \) -print`
src_file=''
for d in $possible_src_dirs; do
    possible_src_file=$d/$plugin_name/"$shortname"src.zip
    if [ -r $possible_src_file ]; then
	src_file=$possible_src_file
	break
    fi
done
if [ -z "$src_file" ]; then
    echo "Could not find matching source file for plugin"
    exit 1
fi
echo "Found source file: $src_file"

# Create symlink to plugin's source directory
source_dir=`dirname $src_file`
symlink_name=$shortname""Src
echo "Creating symlink $symlink_name -> $source_dir"
ln -s $source_dir $symlink_name

if [ $? -ne 0 ]; then
    echo "Symlink creation failed"
    exit 1
fi

# Unpack source
echo "Unpacking source files..."
(cd $source_dir && unzip -q -o `basename $src_file`)
if [ $? -ne 0 ]; then
    echo "Couldn't unzip source file"
    exit 1
fi

# Back up the plugin jar file
if [ ! -f "$jarfile.orig" ]; then
    echo "Making backup of jar file"
    cp $jarfile $jarfile.orig
fi

# Create "obj" directory for output of compile script
mkdir -p $source_dir/obj

# Unpack jar file (but then remove class files)
echo "Unpacking jar file..."
(cd $source_dir/obj && jar xf $eclipse_root/$jarfile.orig && rm `find org -name '*.class' -print`)
if [ $? -ne 0 ]; then
    echo "Couldn't unpack jar for (for non-classfile contents)"
    exit 1
fi

# Generate "compile" script
cat > $source_dir/compile <<EOF1
#! /bin/bash

# Script to compile plugin $plugin_name from source

javac -d obj -classpath \`cd $eclipse_root; find . -name '*.jar' | printcp $eclipse_root\`:\$CLASSPATH \`find org -name '*.java' -print\`
EOF1
chmod a+x $source_dir/compile

# Generate "install" script
cat > $source_dir/install <<EOF2
#! /bin/bash

# Script to install a recompiled version of plugin $plugin_name

jarfile=$eclipse_root/$jarfile
if [ ! -f \$jarfile.orig ]; then
    echo 'The original plugin jar file is not backed up!'
    echo "Failed"
    exit 1
fi

cd obj && jar cf \$jarfile org
if [ \$? -ne 0 ]; then
    echo "Failed"
else
    echo "Success"
fi
EOF2
chmod a+x $source_dir/install

echo 'Plugin is ready to compile!'
