<commit_before>require 'formula'

class Elasticsearch < Formula
  homepage 'http://www.elasticsearch.org'
  url 'https://github.com/downloads/elasticsearch/elasticsearch/elasticsearch-0.20.1.tar.gz'
  sha1 'd1c468a589060dd43532c1afabec9eee10095429'

  def cluster_name
    "elasticsearch_#{ENV['USER']}"
  end

  def install
    # Remove Windows files
    rm_f Dir["bin/*.bat"]
    # Move JARs from lib to libexec according to homebrew conventions
    libexec.install Dir['lib/*.jar']
    (libexec+'sigar').install Dir['lib/sigar/*.jar']

    # Install everything directly into folder
    prefix.install Dir['*']

    # Set up ElasticSearch for local development:
    inreplace "#{prefix}/config/elasticsearch.yml" do |s|

      # 1. Give the cluster a unique name
      s.gsub! /#\s*cluster\.name\: elasticsearch/, "cluster.name: #{cluster_name}"

      # 2. Configure paths
      s.gsub! /#\s*path\.data\: [^\n]+/, "path.data: #{var}/elasticsearch/"
      s.gsub! /#\s*path\.logs\: [^\n]+/, "path.logs: #{var}/log/elasticsearch/"

      # 3. Bind to loopback IP for laptops roaming different networks
      s.gsub! /#\s*network\.host\: [^\n]+/, "network.host: 127.0.0.1"
    end

    inreplace "#{bin}/elasticsearch.in.sh" do |s|
      # Replace CLASSPATH paths to use libexec instead of lib
      s.gsub! /ES_HOME\/lib\//, "ES_HOME/libexec/"
    end

    inreplace "#{bin}/elasticsearch" do |s|
      # Set ES_HOME to prefix value
      s.gsub! /^ES_HOME=.*$/, "ES_HOME=#{prefix}"
    end

    inreplace "#{bin}/plugin" do |s|
      # Set ES_HOME to prefix value
      s.gsub! /^ES_HOME=.*$/, "ES_HOME=#{prefix}"
      # Replace CLASSPATH paths to use libexec instead of lib
      s.gsub! /-cp \".*\"/, '-cp "$ES_HOME/libexec/*"'
    end
  end

  def caveats; <<-EOS.undent
    If upgrading from 0.18 ElasticSearch requires flushing before shutting
    down the cluster with no indexing operations happening after flush:
        curl host:9200/_flush

    See the 'elasticsearch.yml' file for configuration options.

    You'll find the ElasticSearch log here:
        open #{var}/log/elasticsearch/#{cluster_name}.log

    The folder with cluster data is here:
        open #{var}/elasticsearch/#{cluster_name}/<commit_msg>elasticsearch: persist plugins on upgrade.<commit_after>require 'formula'

class Elasticsearch < Formula
  homepage 'http://www.elasticsearch.org'
  url 'https://github.com/downloads/elasticsearch/elasticsearch/elasticsearch-0.20.1.tar.gz'
  sha1 'd1c468a589060dd43532c1afabec9eee10095429'

  def cluster_name
    "elasticsearch_#{ENV['USER']}"
  end

  def install
    # Remove Windows files
    rm_f Dir["bin/*.bat"]
    # Move JARs from lib to libexec according to homebrew conventions
    libexec.install Dir['lib/*.jar']
    (libexec+'sigar').install Dir['lib/sigar/*.jar']

    # Install everything directly into folder
    prefix.install Dir['*']

    # Set up ElasticSearch for local development:
    inreplace "#{prefix}/config/elasticsearch.yml" do |s|

      # 1. Give the cluster a unique name
      s.gsub! /#\s*cluster\.name\: elasticsearch/, "cluster.name: #{cluster_name}"

      # 2. Configure paths
      s.gsub! /#\s*path\.data\: [^\n]+/, "path.data: #{var}/elasticsearch/"
      s.gsub! /#\s*path\.logs\: [^\n]+/, "path.logs: #{var}/log/elasticsearch/"

      # 3. Bind to loopback IP for laptops roaming different networks
      s.gsub! /#\s*network\.host\: [^\n]+/, "network.host: 127.0.0.1"
    end

    inreplace "#{bin}/elasticsearch.in.sh" do |s|
      # Replace CLASSPATH paths to use libexec instead of lib
      s.gsub! /ES_HOME\/lib\//, "ES_HOME/libexec/"
    end

    inreplace "#{bin}/elasticsearch" do |s|
      # Set ES_HOME to prefix value
      s.gsub! /^ES_HOME=.*$/, "ES_HOME=#{prefix}"
    end

    inreplace "#{bin}/plugin" do |s|
      # Set ES_HOME to prefix value
      s.gsub! /^ES_HOME=.*$/, "ES_HOME=#{prefix}"
      # Replace CLASSPATH paths to use libexec instead of lib
      s.gsub! /-cp \".*\"/, '-cp "$ES_HOME/libexec/*"'
    end

    # Persist plugins on upgrade
    plugins = "#{HOMEBREW_PREFIX}/var/lib/elasticsearch/plugins"
    mkdir_p plugins
    ln_sf plugins, "#{prefix}/plugins"
  end

  def caveats; <<-EOS.undent
    If upgrading from 0.18 ElasticSearch requires flushing before shutting
    down the cluster with no indexing operations happening after flush:
        curl host:9200/_flush

    See the 'elasticsearch.yml' file for configuration options.

    You'll find the ElasticSearch log here:
        open #{var}/log/elasticsearch/#{cluster_name}.log

    The folder with cluster data is here:
        open #{var}/elasticsearch/#{cluster_name}/
{'fraction_non_alphanumeric': 0.14307261366084895, 'fraction_numerical': 0.019823313940961, 'mean_word_length': 2.9709153122326777, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4405426', 'n_tokens_mistral': 1608, 'n_tokens_neox': 1530, 'n_words': 412}