#! /usr/bin/ruby
# Usage: rip-fetch-dependencies PACKAGE
# Fetch dependencies with a breadth-first traversal

$-e = true

require 'rip/script'
require 'set'

fetched = Set.new
queue   = Set.new

if ARGV.any?
  ARGV.each do |path|
    queue << path
  end
else
  $stdin.each_line do |path|
    queue << path.chomp
  end
end

while queue.any?
  rip_io(:package, "w+") do |f|
    queue.each do |path|
      if File.exist?(deps = "#{path}/deps.rip")
        debug "found #{deps}"
        f.puts deps
      end
    end
    queue.clear
    f.close_write

    f.each_line do |line|
      path = line.chomp
      if !fetched.include?(path)
        queue << path
        fetched << path
        puts path
      end
    end
    f.close_read
  end
end
