#!/usr/bin/ruby

$LOAD_PATH.unshift(File.expand_path(File.join(File.dirname(__FILE__), '../lib')))
require 'refrepo'

# This script is used to script manual changes over nodes yaml files

require 'optparse'

options = {
  :site => nil,
  :clusters => nil
}

OptionParser.new do |opts|
  opts.banner = "Usage: [options]"

  opts.separator ""

  opts.separator "Filters:"

  opts.on('-s', '--site a', String, 'Select site') do |s|
    options[:site] = s
  end

  opts.on('-c', '--cluster c', String, 'Select cluster') do |s|
    options[:cluster] = s
  end

end.parse!

Dir["input/grid5000/sites/*/clusters/*/nodes/*.yaml"].each do |f|
  m = f.match(/input\/grid5000\/sites\/(?<site>[^\/]*)\/clusters\/(?<cluster>[^\/]*)\/nodes\//)
  next if options[:site] and m[:site] != options[:site]
  next if options[:cluster] and m[:cluster] != options[:cluster]
  node = YAML::load_file(f)
  puts f

  ### TODO your changes go here
  ccn = %w{ gros grcinq uvb econome parapluie paranoia gemini hercule}.include?(m[:cluster]) ? 'contiguous' : 'round-robin'
  node.values.first['architecture']['cpu_core_numbering'] = ccn
  node.values.first['architecture'] = node.values.first['architecture'].sort.to_h

  File::open(f, 'w') do |fd|
    fd.puts "# Generated by g5k-checks (g5k-checks -m api)"
    fd.print YAML::dump(node)
  end
end