#!/usr/bin/env ruby

def parse_changelog(changelog)
  log = File.read(changelog)
  match = /^#\s*\[?#{ENV["_VERSION"]}\]?\s+-\s+(?<title>.+?)\r?\n(?<body>.*?)\r?\n#/ms.match(log)
  match ? match["body"] : nil
end

file = ARGV[0]
dest = File.join(ENV["__BUILD_DIR"], ENV["_DESTINATION"] || file)
out = parse_changelog(file)
if out
  puts "Changelog:"
  puts out
  File.open(dest, "w") {|f| f.puts(out) }
else
  puts "Failed to find changelog data"
  exit 1
end
