#! /usr/bin/ruby
# Usage: rip-package URL

require 'rip/script'

if !$stdin.tty? && ARGV.empty?
  IO.popen("xargs -P#{Rip.processes} -I {} rip-package {}", "w") do |f|
    $stdin.each_line { |line| f.puts(line) }
  end
  exit $?.exitstatus
end

# flatten quoted args
args = (ARGV * ' ').split(' ')

debug "packaging #{args * ' '}"

commands.grep(/^package-/).each do |handler|
  out = rip(handler.chomp, args)

  if $?.exitstatus == 3
    next
  elsif $?.success?
    out.split("\n").each do |package_path|
      puts rip(:build, package_path)
    end

    exit 0
  else
    exit $?.exitstatus
  end
end

exit 1
