# Custom external facts loader, because you can't pass
# `--external-dir` to puppet.
#
# * Reads from $BOXEN_HOME/facts.d, instead of /etc/puppet/facts.d
# * Sets facts' weight to 1 higher than the default loader's weight

require 'facter/util/directory_loader'
require 'boxen/config'

class BoxenFactsDirectoryLoader < Facter::Util::DirectoryLoader
  EXTERNAL_FACT_WEIGHT = Facter::Util::DirectoryLoader::EXTERNAL_FACT_WEIGHT + 1
end

# Find where boxen is installed
config = Boxen::Config.load
boxen_home = config.repodir
facts_d = File.join(boxen_home, "facts.d")

# Load all "external facts" from $BOXEN_HOME/facts.d
loader = BoxenFactsDirectoryLoader.loader_for(facts_d)
loader.load(Facter.collection)

# Custom external facts loader, because you can't pass
# `--external-dir` to puppet.
#
# * Reads from $BOXEN_HOME/facts.d, instead of /etc/puppet/facts.d
# * Sets facts' weight to 1 higher than the default loader's weight

require 'facter/util/directory_loader'
require 'boxen/config'

class BoxenFactsDirectoryLoader < Facter::Util::DirectoryLoader
  EXTERNAL_FACT_WEIGHT = Facter::Util::DirectoryLoader::EXTERNAL_FACT_WEIGHT + 1

  def load(collection)
    entries.each do |file|
      parser = Facter::Util::Parser.parser_for(file)
      if parser == nil
        next
      end

      data = parser.results
      if data == false
        Facter.warn "Could not interpret fact file #{file}"
      elsif data == {} or data == nil
        Facter.warn "Fact file #{file} was parsed but returned an empty data set"
      else
        data.each { |p,v| collection.add(p, :value => v) { has_weight(EXTERNAL_FACT_WEIGHT) } }
      end
    end
  end
end

# Find where boxen is installed
config = Boxen::Config.load
boxen_home = config.repodir
facts_d = File.join(boxen_home, "facts.d")

# Load all "external facts" from $BOXEN_HOME/facts.d
loader = BoxenFactsDirectoryLoader.new(facts_d)
loader.load(Facter.collection)

Fix custom external fact loader's weights
Fix custom external fact loader's weights

Because facter doesn't expose the weight as a customizable value, we
need to override the constant. You can't do that in a subclass:

```ruby
class A
  FOO="A"
  def foo; FOO; end
end

class B < A
  FOO="B"
end

A.new.foo # => "A"
B.new.foo # => "A"

class A
  def foo; self.class::FOO; end
end

A.new.foo # => "A"
B.new.foo # => "B"
```

So, we copy the `#load` method :(
