#!/bin/sh

# JON version without the -SNAPSHOT suffix.
JON_VERSION="2.4.0"
# RHQ version without the -SNAPSHOT suffix.
RHQ_VERSION="3.0.0"

cd ~/Applications
if [ -d "jon-server-${JON_VERSION}-SNAPSHOT" ]; then
  echo "Removing previous JON ${JON_VERSION} installation..."
  rm -rf jon-server-${JON_VERSION}-SNAPSHOT
fi
if [ -d "jon-plugin-pack-soa-${JON_VERSION}-SNAPSHOT" ]; then
  echo "Removing previous JON ${JON_VERSION} SOA plugin pack installation..."
  rm -rf jon-plugin-pack-soa-${JON_VERSION}-SNAPSHOT
fi

cd ~/Downloads
echo "Downloading latest JON ${JON_VERSION} build from Hudson..."
rm -f jon-server-${JON_VERSION}-SNAPSHOT.zip
wget --no-verbose http://hudson-qe.rhq.rdu.redhat.com:8080/view/DEV/job/jon/lastSuccessfulBuild/artifact/jon/dist/server/target/jon-server-${JON_VERSION}-SNAPSHOT.zip
echo "Downloading latest JON ${JON_VERSION} SOA plugin pack build from Hudson..."
rm -f jon-plugin-pack-soa-${JON_VERSION}-SNAPSHOT.zip
wget --no-verbose http://hudson-qe.rhq.rdu.redhat.com:8080/view/DEV/job/jon/lastSuccessfulBuild/artifact/jon/dist/plugin-packs/soa/target/jon-plugin-pack-soa-${JON_VERSION}-SNAPSHOT.zip

cd ~/Applications
echo "Unzipping JON ${JON_VERSION} distribution..."
unzip -q ~/Downloads/jon-server-${JON_VERSION}-SNAPSHOT.zip
chmod 755 jon-server-${JON_VERSION}-SNAPSHOT
echo "Unzipping JON ${JON_VERSION} SOA plugin pack distribution..."
unzip -q ~/Downloads/jon-plugin-pack-soa-${JON_VERSION}-SNAPSHOT.zip
chmod 755 jon-plugin-pack-soa-${JON_VERSION}-SNAPSHOT
echo "Installing plugins from SOA plugin pack into JON Server's rhq-plugins dir..."
cp jon-plugin-pack-soa-${JON_VERSION}-SNAPSHOT/*.jar jon-server-${JON_VERSION}-SNAPSHOT/jbossas/server/default/deploy/rhq.ear.rej/rhq-downloads/rhq-plugins
echo "Removing all Windows batch files from JON ${JON_VERSION} installation..."
find jon-server-${JON_VERSION}-SNAPSHOT -name "*.bat" | xargs rm -f

# TODO: Enable JPDA in JON Server's run.conf.

# TODO: Install Agent?
#cp jon-server-${JON_VERSION}-SNAPSHOT/jbossas/server/default/deploy/rhq.ear/rhq-downloads/rhq-agent/rhq-enterprise-agent-${RHQ_VERSION}-SNAPSHOT.jar ~/Applications

