#!/bin/sh
#
# Ideally, axis2 would be packaged for Ubuntu, and a build dependency of the
# eucalyptus package.
#
# However, axis2 is not feasible to package in Ubuntu (ask Thierry Carrez).
#
# This script is intended to be run by the Ubuntu eucalyptus package maintainer.
#
# It will download the source/binary tarball for axis2, and run WSDL2C.sh, which
# regenerates a bunch of C-stubs need for Eucalyptus.  This only needs to be
# executed if any of the wsdl files change:
#  * wsdl/eucalyptus_cc.wsdl
#  * wsdl/eucalyptus_gl.wsdl
#  * wsdl/eucalyptus_nc.wsdl
#
# -- Dustin Kirkland <kirkland@canonical.com> 2010-01-04 17:02:16

set -e
set -x

ORIG_DIR="$PWD"
DEST="$ORIG_DIR/debian/patches/01-wsdl-stubs.patch"
if [ ! -f "$DEST" ]; then
	echo
	echo "Run '$0' from the root of the eucalyptus branch"
	echo
	exit 1
fi

# Create a sandbox
DIR=$(mktemp -d)
cd "$DIR"

# Get the nasty axis2 tool
if [ -z "$AXIS2_HOME" ] || [ ! -d "$AXIS2_HOME" ]; then
	wget -O eucalyptus-1.6.1-src-deps.tar.gz http://open.eucalyptus.com/downloads/127
	tar zxvf eucalyptus-1.6.1-src-deps.tar.gz
	cd eucalyptus-src-deps
	tar zxvf axis2-1.4.tgz
	export AXIS2_HOME="$DIR"/eucalyptus-src-deps/axis2-1.4
	cd ..
fi

# Grab the Ubuntu eucalyptus branch
bzr branch "$ORIG_DIR" ubuntu
cd "$DIR"/ubuntu

# Reset the generated environment
rm -rf cluster/generated node/generated gatherlog/generated
mkdir -p cluster/generated node/generated gatherlog/generated

# Generate the stubs
sh /usr/lib/axis2/bin/tools/wsdl2c/WSDL2C.sh -uri wsdl/eucalyptus_gl.wsdl -g -d adb -u -uw -f -o gatherlog/generated | grep -v 'the classpath'
sh /usr/lib/axis2/bin/tools/wsdl2c/WSDL2C.sh -uri wsdl/eucalyptus_gl.wsdl -sd -ss -d adb -u -uw -f -o gatherlog/generated | grep -v 'the classpath'
sh /usr/lib/axis2/bin/tools/wsdl2c/WSDL2C.sh -uri wsdl/eucalyptus_nc.wsdl -g -d adb -u -uw -f -o node/generated | grep -v 'the classpath'
sh /usr/lib/axis2/bin/tools/wsdl2c/WSDL2C.sh -uri wsdl/eucalyptus_nc.wsdl -sd -ss -d adb -u -uw -f -o node/generated | grep -v 'the classpath'
sh /usr/lib/axis2/bin/tools/wsdl2c/WSDL2C.sh -uri wsdl/eucalyptus_cc.wsdl -g -d adb -u -uw -f -o cluster/generated | grep -v 'the classpath'
sh /usr/lib/axis2/bin/tools/wsdl2c/WSDL2C.sh -uri wsdl/eucalyptus_nc.wsdl -g -d adb -u -uw -f -o cluster/generated | grep -v 'the classpath'
sh /usr/lib/axis2/bin/tools/wsdl2c/WSDL2C.sh -uri wsdl/eucalyptus_cc.wsdl -sd -ss -d adb -u -uw -f -o cluster/generated | grep -v 'the classpath'

# Doctor the generated output like Eucalyptus does
./tools/add_marshalling.pl */generated/axis2_skel_*.c
patch -d cluster/generated -p1 < cluster/axis2_svc_generated.patch
patch -d node/generated -p1 < node/axis2_svc_generated.patch
find . | grep "axis2_svc_skel_.*.orig" | xargs -i rm -f {}

# Generate the diff, and update the md5sums
bzr add cluster/generated node/generated gatherlog/generated
bzr diff -p1 | tee "$DEST"
cd "$ORIG_DIR"
md5sum wsdl/*.wsdl | tee debian/wsdl.md5sums

# Clean up
rm -rf "$DIR"

# Check work and commit
bzr diff | diffstat
echo
echo "SUCCESS: $DEST updated"
echo
echo "INFO: Update changelog, and debcommit!"
echo
echo "  * debian/patches/01-wsdl-stubs.patch, debian/wsdl.md5sums: wsdl stubs updated"
echo
