#!/bin/bash

GEM_SERVER=http://localhost:9000/gems

echo "starting build..."
./build

if [ "$?" = "0" ]; then
    echo "build succeeded"

    # generate gem version based on jenkins build number
    if [ -z $BUILD_NUMBER ]; then
        BUILD_NUMBER="1"
    fi
    VERSION="0.1.${BUILD_NUMBER}"
    echo "packaging gem jam_ruby-$VERSION"
    cat > lib/jam_ruby/version.rb << EOF
module JamRuby
  VERSION = "$VERSION"
end
EOF
    
    gem build jam_ruby.gemspec

    GEMNAME="jam_ruby-${VERSION}.gem"

    echo "publishing gem"
    curl -f -T $GEMNAME $GEM_SERVER/$GEMNAME

    if [ "$?" != "0" ]; then
        echo "publish failed"
        exit 1
    fi
    echo "done publishing gems"
else
    echo "build failed"
    exit 1
fi


