#!/usr/bin/ruby

require 'fileutils'

if ARGV.length != 2
  puts "usage: test-package [OPTIONS] PACKAGE OUTPUTDIR"
  exit 1
end

pkg = ARGV.shift
outdir = ARGV.shift

puts "Not really running anything .."
puts "This will fail, pass or tmpfail randomly. Passing is twice as probable as fail and tmpfail"

r = nil
case ENV['DEBCI_FAKE_RESULT']
when 'pass'
  r = 0
when 'fail'
  r = 2
when 'tmpfail'
  r = 3
else
  r = rand(4)
end

FileUtils.mkdir(outdir)
if ENV["DEBCI_FAKE_DEPS"]
  File.open(File.join(outdir, 'foo0t-mytest-packages'), 'w') do |f|
    ENV["DEBCI_FAKE_DEPS"].split('|').each do |line|
      f.puts line.gsub(" ", "\t")
    end
  end
end

case r
when 0..1
  log = "Passed :-)\n"
  rc = 0
when 2
  log = "Failed :-(\n"
  rc = 4
when 3
  log = "Some error ocurred\n"
  rc = 16
end

log = log + "adt-run [%s]: finished\n" % Time.now.strftime('%Y-%m-%d %H:%M:%S')

File.open(File.join(outdir, 'log'), 'w') do |f|
  f.puts log
end
File.open(File.join(outdir, 'exitcode'), 'w') do |f|
  f.puts rc
end
puts log
exit rc
