#!/usr/bin/env sh

JRUBY_VERSION=$(cat ../.ruby-version | sed 's/jruby-//')
SHOES_VERSION=$(cat ../VERSION)

echo "Building for JRuby $JRUBY_VERSION and Shoes $SHOES_VERSION"

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

# Make a place to work in...
mkdir -p $DIR/tmp

BUILD_DIR=$DIR/tmp/Shoes4.app
rm -rf $BUILD_DIR

cp -r $DIR/mac-template $BUILD_DIR
pushd $BUILD_DIR

# Put the Shoes app in there
mkdir -p ./Contents/PlugIns/Shoes
cp ../../*.rb ./Contents/Plugins/Shoes
cp ../../*.png ./Contents/Plugins/Shoes

# Get our app script templated out right
sed "s/@@JRUBY_VERSION@@/jruby-$JRUBY_VERSION/g" ./Contents/MacOS/app.template > ./Contents/MacOS/app
chmod +x ./Contents/MacOS/app

# Get me a JRuby
curl -s https://repo1.maven.org/maven2/org/jruby/jruby-dist/$JRUBY_VERSION/jruby-dist-$JRUBY_VERSION-bin.tar.gz > ./Contents/PlugIns/jruby.tar.gz
tar -C ./Contents/PlugIns -xvf ./Contents/PlugIns/jruby.tar.gz

# Get me some Shoes!
./Contents/PlugIns/jruby-$JRUBY_VERSION/bin/jruby -S gem install shoes -v $SHOES_VERSION

# Clean up after ourselves
rm ./Contents/PlugIns/jruby.tar.gz
rm ./Contents/MacOS/*.template

cd ..
tar -czf Shoes4-mac.tar.gz Shoes4.app

popd
