require 'rubygems'
require 'rake/clean'
require 'fileutils'

task :default => [:update_docs, :doxygen, :javadoc, :build, :clean]

CLEAN.include("**/.DS_Store")

desc "Build the website from source"
task :build do
  puts "Building website from static source"
  result = system("middleman build --clean")
  if result
    puts "Successfully generated the site, please commit your changes"
  else
    puts "An error was encountered when generating the site"
  end
end

desc "Update the latest docs from the Apache Mesos codebase"
task :update_docs do
  puts "Updating latest documentation from the Apache Mesos codebase"

  docs_dir = File.join(File.dirname(__FILE__), "source/documentation")

  puts "Updating docs to the latest version"
  FileUtils.mkdir_p("source/documentation/latest/")
  FileUtils.rm_rf(Dir.glob("source/documentation/latest/*"))
  FileUtils.cp_r(FileList['../docs/**'].exclude("../docs/images"), File.expand_path("source/documentation/latest/"))

  # Due to a limitation in middleman, it doesn't support .md files
  # named (e.g.  state.json.md). Middlman would generate an *html* file
  # called state.json from this instead of generating the standard
  # state.json/index.html like it does for other .md files.
  puts "Migrating markdown files with paths matching **/*.json.md to **/*.json/index.md"
  Dir.chdir("#{docs_dir}/latest/") {
    Dir.glob('**/*.json.md').each { |doc|
      docdir = doc.chomp(".md")
      FileUtils.mkdir(docdir)
      FileUtils.mv(doc, docdir + "/index.md")
    }
  }

  puts "Parse documentation files to removing md extension in any links"
  Dir.chdir("#{docs_dir}/latest/") {
    Dir.glob('**/*.md').each { |doc|
      puts "working on: #{doc}"

      # TODO(klueska): we really need to get rid of this complicated
      # regular expression and add in some logic that is more easily
      # understood.
      docdir = File.dirname(doc)
      IO.write(doc, File.open(doc) { |f|
        f.read
          .gsub(/\(([^(]+?)(?:\/index)?\.md(\#.+?)?\)/, "(/documentation/latest/#{docdir}/" + '\1/\2)')
          .gsub(/\(images\/(.*)\)/, '(/assets/img/documentation/\1)')
      })
    }
  }

  puts "Moving documentation index to its own 'latest' directory"
  FileUtils.mv("source/documentation/latest/home.md", "source/documentation/latest.html.md")

  puts "Documentation updated"

  # Copy the images from the docs
  FileUtils.mkdir_p("source/assets/img/documentation/")
  FileUtils.rm_f(Dir.glob("source/assets/img/documentation/*"))
  FileUtils.cp_r(Dir.glob("../docs/images/*"), File.expand_path("source/assets/img/documentation/"))
end

desc "Generate javadoc from the Java source files in the codebase"
task :javadoc do
  proto_java_folder = "../build/src/java/generated"
  if not File.directory?(proto_java_folder)
    raise "Please make sure Java proto files are generated in #{proto_java_folder} folder."
  end
  # TODO: add version to the path. Uses 'current' for now.
  system("javadoc -d source/api/latest/java -sourcepath ../src/java/src:#{proto_java_folder} org.apache.mesos")
end

desc "Generate doxygen from the C++ source files in the codebase"
task :doxygen do
  FileUtils.rm_rf(Dir.glob("source/api/latest/c++"))
  FileUtils.mkdir_p("source/api/latest")
  system("doxygen ../Doxyfile && mv doxygen/html source/api/latest/c++ && rm -rf doxygen")
end

desc "Clean up generated documents"
task :clean_docs do
  FileUtils.rm_rf(Dir.glob("source/api"))
  FileUtils.rm_rf(Dir.glob("source/documentation"))
  FileUtils.rm_rf(Dir.glob("source/assets/img/documentation"))
end

desc "Run the site in development mode. Preview available at http://localhost:4567/"
task :dev do
  system("middleman server")
end
