<NME> Makefile
<BEF> # Requirements to build:
#   rsync
#   wget or curl
#
JRUBY_VERSION=1.7.11
JRUBY_VERSION=1.7.0
ELASTICSEARCH_VERSION=0.19.10
JODA_VERSION=2.1
VERSION=$(shell ruby -r./lib/logstash/version -e 'puts LOGSTASH_VERSION')

JRUBY_CMD=build/jruby/jruby-$(JRUBY_VERSION)/bin/jruby
WITH_JRUBY=bash $(JRUBY_CMD) --1.9 -S
JRUBY_URL=http://repository.codehaus.org/org/jruby/jruby-complete/$(JRUBY_VERSION)
JRUBY=vendor/jar/jruby-complete-$(JRUBY_VERSION).jar
JRUBYC=java -Djruby.compat.version=RUBY1_9 -jar $(PWD)/$(JRUBY) -S jrubyc
ELASTICSEARCH_URL=http://github.com/downloads/elasticsearch/elasticsearch
ELASTICSEARCH=vendor/jar/elasticsearch-$(ELASTICSEARCH_VERSION)
JODA=vendor/jar/joda-time-$(JODA_VERSION)/joda-time-$(JODA_VERSION).jar
QUIET=@
ifeq (@,$(QUIET))
	QUIET_OUTPUT=> /dev/null 2>&1
endif

WGET=$(shell which wget 2>/dev/null)
CURL=$(shell which curl 2>/dev/null)

# OS-specific options
TARCHECK=$(shell tar --help|grep wildcard|wc -l|tr -d ' ')
ifeq (0, $(TARCHECK))
TAR_OPTS=
else
TAR_OPTS=--wildcards
endif

TESTS=$(wildcard spec/*.rb spec/**/*.rb spec/**/**/*.rb)

#spec/outputs/graphite.rb spec/outputs/email.rb)
default:
	@echo "Make targets you might be interested in:"
	@echo "  tarball -- builds the tarball package"
	@echo "  tarball-test -- runs the test suite against the tarball package"

.VERSION.mk:
	echo "VERSION=${LOGSTASH_VERSION}" >> $@

-include .VERSION.mk

version:
	@echo "Version: $(VERSION)"

# Figure out if we're using wget or curl
.PHONY: wget-or-curl
wget-or-curl:
ifeq ($(CURL),)
ifeq ($(WGET),)
	@echo "wget or curl are required."
	exit 1
else
DOWNLOAD_COMMAND=wget -q --no-check-certificate -O
endif
else
DOWNLOAD_COMMAND=curl -s -L -k -o
endif

.PHONY: clean
clean:
	@echo "=> Cleaning up"
.PHONY: compile-runner
compile-runner: build/ruby/logstash/runner.class
build/ruby/logstash/runner.class: lib/logstash/runner.rb | build/ruby $(JRUBY)
	$(QUIET)(cd lib; JRUBY_OPTS=--1.9 $(JRUBYC) -t ../build/ruby logstash/runner.rb)

# TODO(sissel): Stop using cpio for this
.PHONY: copy-ruby-files
	-$(QUIET)rm -rf vendor/kibana vendor/geoip vendor/collectd
	-$(QUIET)rm -rf vendor/jar vendor/ua-parser

.PHONY: clean-vendor
clean-vendor:
	-$(QUIET)rm -rf vendor

.PHONY: copy-ruby-files
copy-ruby-files: | build/ruby
	@# Copy lib/ and test/ files to the root
	$(QUIET)rsync -a --include "*/" --include "*.rb" --include "*.yaml" --exclude "*" ./lib/ ./build/ruby
	$(QUIET)rsync -a ./spec ./build/ruby

build-jruby: $(JRUBY)

$(JRUBY): build/jruby/jruby-$(JRUBY_VERSION)/lib/jruby-complete.jar | vendor/jar
	$(QUIET)cp $< $@

build/jruby: build
	$(QUIET)mkdir -p $@

$(JRUBY_CMD): build/jruby/jruby-$(JRUBY_VERSION)/lib/jruby-complete.jar
build/jruby/jruby-$(JRUBY_VERSION)/lib/jruby-complete.jar: build/jruby/jruby-$(JRUBY_VERSION)
	# Build jruby from source targeted at 1.9 - patch that, yo.
	$(QUIET)sed -i -e 's/jruby.default.ruby.version=.*/jruby.default.ruby.version=1.9/' $</default.build.properties
	$(QUIET)(cd $<; ant jar-jruby-complete)

build/jruby/jruby-$(JRUBY_VERSION): build/jruby/jruby-src-$(JRUBY_VERSION).tar.gz
	$(QUIET)tar -C build/jruby/ $(TAR_OPTS) -zxf $<

build/jruby/jruby-src-$(JRUBY_VERSION).tar.gz: | wget-or-curl build/jruby
	@echo "=> Fetching jruby source"
	$(QUIET)$(DOWNLOAD_COMMAND) $@ http://jruby.org.s3.amazonaws.com/downloads/$(JRUBY_VERSION)/jruby-src-$(JRUBY_VERSION).tar.gz

vendor/jar/elasticsearch-$(ELASTICSEARCH_VERSION).tar.gz: | wget-or-curl vendor/jar
	@echo "=> Fetching elasticsearch"
vendor-jruby: $(JRUBY)

$(JRUBY): | vendor/jar
	$(QUIET)echo "=> Downloading jruby $(JRUBY_VERSION)"
	$(QUIET)$(DOWNLOAD_COMMAND) $@ $(JRUBY_URL)

vendor/jar/elasticsearch-$(ELASTICSEARCH_VERSION).tar.gz: | wget-or-curl vendor/jar
	@echo "=> Fetching elasticsearch"
	$(QUIET)$(DOWNLOAD_COMMAND) $@ $(ELASTICSEARCH_URL)/elasticsearch-$(ELASTICSEARCH_VERSION).tar.gz

build/tarball/logstash-$(LOGSTASH_VERSION).tar.gz: | wget-or-curl build build/tarball
	@echo "=> Fetching logstash $(LOGSTASH_VERSION)"
	$(QUIET)$(DOWNLOAD_COMMAND) $@ $(LOGSTASH_URL)/logstash-$(LOGSTASH_VERSION).tar.gz

.PHONY: vendor-elasticsearch
vendor-elasticsearch: $(ELASTICSEARCH)
$(ELASTICSEARCH): $(ELASTICSEARCH).tar.gz | vendor/jar
	@echo "=> Pulling the jars out of $<"
	$(QUIET)tar -C $(shell dirname $@) -xf $< $(TAR_OPTS) --exclude '*sigar*' \
		'elasticsearch-$(ELASTICSEARCH_VERSION)/lib/*.jar'

.PHONY: vendor-gems
vendor-gems: | vendor/bundle

.PHONY: vendor/bundle
vendor/bundle: | vendor $(JRUBY)
	@echo "=> Ensuring ruby gems dependencies are in $@..."
	$(QUIET)GEM_HOME=./vendor/bundle/jruby/1.9/ GEM_PATH= $(JRUBY_CMD) --1.9 ./gembag.rb logstash-kafka.gemspec
	@# Purge any junk that fattens our jar without need!
	@# The riak gem includes previous gems in the 'pkg' dir. :(
	-$(QUIET)rm -rf $@/jruby/1.9/gems/riak-client-1.0.3/pkg
.PHONY: vendor-gems
vendor-gems: | vendor/bundle

$(GEM_HOME)/bin/bundle: | $(JRUBY_CMD)
	@echo "=> Installing bundler ($@)"
	$(QUIET)GEM_HOME=$(GEM_HOME) $(WITH_JRUBY) gem install bundler

.PHONY: vendor/bundle
vendor/bundle: | $(GEM_HOME)/bin/bundle fix-bundler
	@echo "=> Installing gems to $@..."
	$(QUIET)GEM_HOME=$(GEM_HOME) bash $(JRUBY_CMD) --1.9 $(GEM_HOME)/bin/bundle install --deployment
	@# Purge any junk that fattens our jar without need!
	@# The riak gem includes previous gems in the 'pkg' dir. :(
	-rm -rf $@/jruby/1.9/gems/riak-client-1.0.3/pkg
$(LOGSTASH): $(LOGSTASH).tar.gz | build build/tarball
	@echo "=> Extracting $<"
		$(QUIET)tar -C $(shell dirname $@) -xf $< $(TAR_OPTS)
		$(QUIET)rm -rf $<

package: | prepare-tarball
	pkg/build.sh centos 6

build/tarball: | build
	-$(QUIET)mkdir $@

show:
	echo $(VERSION)

.PHONY: prepare-tarball
prepare-tarball tarball zip: WORKDIR=build/tarball/logstash-$(VERSION)
prepare-tarball: $(LOGSTASH) $(JRUBY) vendor-gems
prepare-tarball:
	@echo "=> Preparing tarball"
	$(QUIET)$(MAKE) $(WORKDIR)
	$(QUIET)rsync -a --relative lib spec vendor/bundle/jruby vendor/jar --exclude 'vendor/bundle/jruby/1.9/cache' --exclude 'vendor/bundle/jruby/1.9/gems/*/doc' --exclude 'vendor/jar/elasticsearch-$(ELASTICSEARCH_VERSION).tar.gz' $(WORKDIR)

.PHONY: tarball
tarball: | build/logstash-$(VERSION).tar.gz
build/logstash-$(VERSION).tar.gz: | prepare-tarball
	$(QUIET)tar -C $$(dirname $(WORKDIR)) -c $$(basename $(WORKDIR)) \
		| gzip -9c > $@
	@echo "=> tarball ready: $@"

.PHONY: zip
zip: | build/logstash-$(VERSION).zip
build/logstash-$(VERSION).zip: | prepare-tarball
	$(QUIET)(cd $$(dirname $(WORKDIR)); find $$(basename $(WORKDIR)) | zip $(PWD)/$@ -@ -9)$(QUIET_OUTPUT)
	@echo "=> zip ready: $@"

.PHONY: tarball-test
tarball-test: #build/logstash-$(VERSION).tar.gz
	$(QUIET)-rm -rf build/test-tarball/
	$(QUIET)mkdir -p build/test-tarball/
	tar -C build/test-tarball --strip-components 1 -xf build/logstash-$(VERSION).tar.gz
	(cd build/test-tarball; bin/logstash rspec spec/**/kafka*.rb)
	$(QUIET)jar uf build/logstash-$(VERSION)-monolithic.jar -C build/ruby .

.PHONY: test
test: | $(JRUBY_CMD) vendor-elasticsearch
	$(QUIET)bash $(JRUBY_CMD) bin/logstash test

.PHONY: docs
docs: docgen doccopy docindex

<MSG> - Now that JRuby (as of 1.7.0) defaults to 1.9 mode, there's no need
  to download the jruby source and compile it with that default. We
  can simply use the jruby-complete jar releases now. WOOT.

<DFF> @@ -6,13 +6,14 @@
 JRUBY_VERSION=1.7.0
 ELASTICSEARCH_VERSION=0.19.10
 JODA_VERSION=2.1
-VERSION=$(shell ruby -r./lib/logstash/version -e 'puts LOGSTASH_VERSION')
+#VERSION=$(shell ruby -r./lib/logstash/version -e 'puts LOGSTASH_VERSION')
+VERSION=$(shell awk -F\" '/LOGSTASH_VERSION/ {print $$2}' lib/logstash/version.rb )
 
-JRUBY_CMD=build/jruby/jruby-$(JRUBY_VERSION)/bin/jruby
-WITH_JRUBY=bash $(JRUBY_CMD) --1.9 -S
-JRUBY_URL=http://repository.codehaus.org/org/jruby/jruby-complete/$(JRUBY_VERSION)
+WITH_JRUBY=java -jar $(shell pwd)/$(JRUBY) -S
 JRUBY=vendor/jar/jruby-complete-$(JRUBY_VERSION).jar
-JRUBYC=java -Djruby.compat.version=RUBY1_9 -jar $(PWD)/$(JRUBY) -S jrubyc
+JRUBY_URL=http://repository.codehaus.org/org/jruby/jruby-complete/$(JRUBY_VERSION)
+JRUBY_CMD=java -jar $(JRUBY)
+JRUBYC=$(WITH_JRUBY) jrubyc
 ELASTICSEARCH_URL=http://github.com/downloads/elasticsearch/elasticsearch
 ELASTICSEARCH=vendor/jar/elasticsearch-$(ELASTICSEARCH_VERSION)
 JODA=vendor/jar/joda-time-$(JODA_VERSION)/joda-time-$(JODA_VERSION).jar
@@ -68,7 +69,7 @@ compile: compile-grammar compile-runner | build/ruby
 .PHONY: compile-runner
 compile-runner: build/ruby/logstash/runner.class
 build/ruby/logstash/runner.class: lib/logstash/runner.rb | build/ruby $(JRUBY)
-	$(QUIET)(cd lib; JRUBY_OPTS=--1.9 $(JRUBYC) -t ../build/ruby logstash/runner.rb)
+	$(QUIET)(cd lib; $(JRUBYC) -t ../build/ruby logstash/runner.rb)
 
 # TODO(sissel): Stop using cpio for this
 .PHONY: copy-ruby-files
@@ -87,24 +88,9 @@ vendor/jar: | vendor
 
 build-jruby: $(JRUBY)
 
-$(JRUBY): build/jruby/jruby-$(JRUBY_VERSION)/lib/jruby-complete.jar | vendor/jar
-	$(QUIET)cp $< $@
-
-build/jruby: build
-	$(QUIET)mkdir -p $@
-
-$(JRUBY_CMD): build/jruby/jruby-$(JRUBY_VERSION)/lib/jruby-complete.jar
-build/jruby/jruby-$(JRUBY_VERSION)/lib/jruby-complete.jar: build/jruby/jruby-$(JRUBY_VERSION)
-	# Build jruby from source targeted at 1.9 - patch that, yo.
-	$(QUIET)sed -i -e 's/jruby.default.ruby.version=.*/jruby.default.ruby.version=1.9/' $</default.build.properties
-	$(QUIET)(cd $<; ant jar-jruby-complete)
-
-build/jruby/jruby-$(JRUBY_VERSION): build/jruby/jruby-src-$(JRUBY_VERSION).tar.gz
-	$(QUIET)tar -C build/jruby/ $(TAR_OPTS) -zxf $<
-
-build/jruby/jruby-src-$(JRUBY_VERSION).tar.gz: | wget-or-curl build/jruby
-	@echo "=> Fetching jruby source"
-	$(QUIET)$(DOWNLOAD_COMMAND) $@ http://jruby.org.s3.amazonaws.com/downloads/$(JRUBY_VERSION)/jruby-src-$(JRUBY_VERSION).tar.gz
+$(JRUBY): | vendor/jar
+	$(QUIET)echo " ==> Downloading jruby $(JRUBY_VERSION)"
+	$(QUIET)$(DOWNLOAD_COMMAND) $@ http://repository.codehaus.org/org/jruby/jruby-complete/$(JRUBY_VERSION)/jruby-complete-$(JRUBY_VERSION).jar
 
 vendor/jar/elasticsearch-$(ELASTICSEARCH_VERSION).tar.gz: | wget-or-curl vendor/jar
 	@echo "=> Fetching elasticsearch"
@@ -142,14 +128,14 @@ fix-bundler:
 .PHONY: vendor-gems
 vendor-gems: | vendor/bundle
 
-$(GEM_HOME)/bin/bundle: | $(JRUBY_CMD)
+$(GEM_HOME)/bin/bundle: | $(JRUBY)
 	@echo "=> Installing bundler ($@)"
 	$(QUIET)GEM_HOME=$(GEM_HOME) $(WITH_JRUBY) gem install bundler
 
 .PHONY: vendor/bundle
 vendor/bundle: | $(GEM_HOME)/bin/bundle fix-bundler
 	@echo "=> Installing gems to $@..."
-	$(QUIET)GEM_HOME=$(GEM_HOME) bash $(JRUBY_CMD) --1.9 $(GEM_HOME)/bin/bundle install --deployment
+	$(QUIET)GEM_HOME=$(GEM_HOME) $(JRUBY_CMD) --1.9 $(GEM_HOME)/bin/bundle install --deployment
 	@# Purge any junk that fattens our jar without need!
 	@# The riak gem includes previous gems in the 'pkg' dir. :(
 	-rm -rf $@/jruby/1.9/gems/riak-client-1.0.3/pkg
@@ -217,8 +203,8 @@ update-jar: copy-ruby-files
 	$(QUIET)jar uf build/logstash-$(VERSION)-monolithic.jar -C build/ruby .
 
 .PHONY: test
-test: | $(JRUBY_CMD) vendor-elasticsearch
-	$(QUIET)bash $(JRUBY_CMD) bin/logstash test
+test: | $(JRUBY) vendor-elasticsearch
+	$(QUIET)$(JRUBY_CMD) bin/logstash test
 
 .PHONY: docs
 docs: docgen doccopy docindex

- Now that JRuby (as of 1.7.0) defaults to 1.9 mode, there's no need   to download the jruby source and compile it with that default. We   can simply use the jruby-complete jar releases now. WOOT.