#! /usr/bin/env ruby

exclude = %w[README header _config.yml CODE_OF_CONDUCT CONTRIBUTING data-structure actions filters shortcodes changelog frequently-asked-questions]
docs_url = "https://wp-document-revisions.github.io/wp-document-revisions/"
output = File.open("./docs/header.md").read

# Remove title, badge, and description from README
readme = File.open("./docs/README.md").read
readme = "\n#" + readme.split("#")[2..-1].join("#")
output << readme

docs = Dir["./docs/*"]
features = docs.delete("./docs/features.md")
docs.unshift(features)

docs.each do |file|
  next if exclude.include?(File.basename(file,".md"))
  output << "\n\n"
  output << File.open(file).read
end

# Add truncated changelog (only recent versions)
changelog_content = File.open("./docs/changelog.md").read
changelog_lines = changelog_content.split("\n")
output << "\n\n"

# Find first version line and include only first 5 versions
version_count = 0
in_changelog = false
changelog_lines.each do |line|
  if line.match(/^## Changelog/)
    in_changelog = true
  end
  
  if in_changelog
    output << line + "\n"
    if line.match(/^### \d+\.\d+/)
      version_count += 1
      if version_count >= 3
        output << "\nFor complete changelog, see [GitHub](https://wp-document-revisions.github.io/wp-document-revisions/changelog/)\n"
        break
      end
    end
  end
end

output.gsub! /^### ?(.*?)$/im, "= \\1 ="
output.gsub! /^## ?(.*?)$/im, "== \\1 =="
output.gsub! /^# ?(.*?)$/im, "=== \\1 ==="
output.gsub! /\]\(\.?\/?(.*?)\.md/, "](#{docs_url}\\1/"

File.open("readme.txt", "wb") do |file|
  file << output
end
