#!/usr/bin/ruby

PROBABILITY = 0.2

# this script will will randomly bump dependencies version numbers with the
# above probability.
#
# For packages without an existing list of dependencies, it will just create a
# bogus one.
#
# The caller can also use the environment variable DEBCI_FAKE_DEPS to
# manutacture the desired dependency list (useful for testing). The expected
# format it "pkg1 v2|pkg2 v2|...|pkgN vN"
#
#

if ENV["DEBCI_FAKE_DEPS"]
  ENV["DEBCI_FAKE_DEPS"].split('|').each do |line|
    puts line
  end
  exit
end

bin = File.dirname(__FILE__);

pkg = ARGV.first
unless pkg
  puts "usage: list-packages PACKAGE"
  exit 1
end

pkgdir = pkg.gsub(/((lib)?.).*/, '\1/\&')

suite = ENV['debci_suite'] || 'unstable'
arch = ENV['debci_arch'] || 'amd64';
dependencies_file = "#{bin}/../../data/packages/#{suite}/#{arch}/#{pkgdir}/dependencies.txt";

if (File.exists? dependencies_file)
  File.readlines(dependencies_file).each do |line|
    line.strip!
    if (rand() <= PROBABILITY)
      line += '+1';
    end
    puts line
  end
else
  %w/foo bar baz qux/.each do |fake_pkg|
    printf("%s %d.%d-%d\n", fake_pkg, rand(4), rand(4), rand(4))
  end
end
