#!/usr/bin/env ruby

def run(name)
  `#{File.join(__dir__, name)}`
end

def stat(key, val = '')
  puts "#{key} #{val}"
end

# Run the fs changes hook, so that /etc/hosts is created on startup
# but don't print the results
run 'ctld-changed'

# Run the ctld-runtime hook and print the results
puts run 'ctld-runtime'

vendor = ''
dmi = `sudo dmidecode`.split("\n\n").each do |section|
  if section.include?("Processor Information")
    section.lines.each do |line|
      stat "hw.cpu", line.split(':')[1].strip if line.include?('Family:')
    end
  end

  if section.include?("System Information")
    section.lines.each do |line|
      if line.include?('Manufacturer:')
        vendor = line.split(':')[1].gsub(" Inc.", "").strip
        stat "hw.vendor", vendor
      end

      stat "hw.model", line.split(':')[1].gsub(vendor, '').strip if line.include?('Product Name:')
    end
  end

  if section.include?("Chassis Information")
    section.lines.each do |line|
      stat "hw.type", line.split(':')[1].strip if line.include?("Type:")
    end
  end
end
