#!/bin/bash

echo "packaging ruby protocol buffers"
pushd target/ruby > /dev/null

rm -rf jampb

#bundle gem jampb > /dev/null
echo "build gem jampb"
bundle gem jampb

# copy over built ruby code
cp src/*.rb jampb/lib/jampb

if [ -z $BUILD_NUMBER ]; then
        BUILD_NUMBER="1"
fi

VERSION="0.1.${BUILD_NUMBER}"

pushd jampb > /dev/null

cat > lib/jampb/version.rb << EOF
module Jampb
  VERSION = "$VERSION"
end
EOF


# define gemspec
cat >> jampb.gemspec << EOF
 
# -*- encoding: utf-8 -*-
require File.expand_path('../lib/jampb/version', __FILE__)

Gem::Specification.new do |gem|
  gem.authors       = ["Seth Call"]
  gem.email         = ["seth@jamkazam.com"]
  gem.description   = %q{protocol buffers for jamkazam}
  gem.summary       = %q{protocol buffers for jamkazam.}
  gem.homepage      = "http://www.jamkazam.com"

  gem.files         = ['Gemfile', 'Rakefile', 'README.md', 'jampb.gemspec', 'lib/jampb.rb', 'lib/jampb/client_container.pb.rb', 'lib/jampb/version.rb' ]
  gem.executables   = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
  gem.test_files    = gem.files.grep(%r{^(test|spec|features)/})
  gem.name          = "jampb"
  gem.require_paths = ["lib"]
  gem.version       = Jampb::VERSION
end
EOF

# make sure library manifest includes the generated ruby file
cat >> lib/jampb.rb << EOF
require "jampb/version"
require "jampb/client_container.pb"

module Jampb
end

EOF

echo "gem build jampb.gemspec"
gem build jampb.gemspec > /dev/null
#gem build jampb.gemspec > /dev/null

popd > /dev/null
popd > /dev/null

