#!/usr/bin/env ruby

require 'librarian/puppet'

def manifests(lockfile)
  unless File.exist?(lockfile)
    raise ArgumentError, "Lockfile #{lockfile} doesn't exist"
  end

  librarian_environment = Librarian::Puppet::Environment.new(pwd: lockfile)
  librarian_environment.lock.manifests.sort_by { |manifest| manifest.name.split('-').last }
end

path = ARGV[0] || 'Puppetfile.lock'

begin
  manifests(path).each do |manifest|
    author, name = manifest.name.split('-')
    if ['theforeman', 'katello'].include?(author)
      branch = "#{manifest.version.to_s.split('.')[0..1].join('.')}-stable"
      puts "puppet-#{name} #{manifest.version} #{branch}"
    end
  end
rescue ArgumentError => e
  puts e
  exit 1
end
