#!/usr/bin/env ruby

require_relative '../lib/const'
require_relative '../lib/color'
require_relative '../lib/package'
require_relative '../lib/package_utils'

# Add >LOCAL< lib to LOAD_PATH so that packages can be loaded
$LOAD_PATH.unshift File.join(CREW_LIB_PATH, 'lib')

def check_package(pkg, verbose: false)
  puts "Checking #{pkg.name} package ...".yellow if verbose
  unless PackageUtils.installed?(pkg.name)
    puts "Package #{pkg.name} is not installed.".lightred
    puts "Package tests for #{pkg.name} failed.".lightred
    return 1
  end
  if File.exist? "#{CREW_LIB_PATH}/tests/package/#{pkg.name[0]}/#{pkg.name}"
    stdout = `#{CREW_LIB_PATH}/tests/package/#{pkg.name[0]}/#{pkg.name} 2> /tmp/errout`
    errout = `cat /tmp/errout`.chomp
  else
    stdout = ''
    errout = "Package tests for #{pkg.name} do not exist. :(".lightred
  end
  print stdout
  # Check if the tests passed.
  if errout.empty?
    puts "Package tests for #{pkg.name} passed.".lightgreen if verbose
    return 0
  else
    puts errout
    puts "Package tests for #{pkg.name} failed.".lightred
    return 1
  end
end

if ARGV[0]
  ARGV.each do |name|
    if File.file?(File.join(CREW_PACKAGES_PATH, "#{name}.rb"))
      test_result = check_package(Package.load_package(File.join(CREW_PACKAGES_PATH, "#{name}.rb")), verbose: true)
      exit(1) if test_result.positive?
    else
      puts "Package #{name} not found.".lightred
    end
  end
else
  failed_packages = 0
  Dir["#{CREW_PACKAGES_PATH}/*.rb"].each do |filename|
    failed_packages += check_package(Package.load_package(filename))
  end

  if failed_packages.positive?
    puts "\n#{failed_packages} packages failed package tests.".lightred
    exit(1)
  else
    puts "\nAll packages passed package tests.".lightgreen
  end
end
