require 'fileutils'
require 'erb'

task :generate => [:generate_usermanual] do
  require 'rdiscount'
  require 'lib/release_notes'

  `rm -rf output`
  FileUtils.mkdir("output")
  `cat text/*.markdown > output/UserManual.markdown`
  `cp -r assets/* output`

  template = ERB.new(File.read("templates/site.html"))

  Dir.glob("text/*.markdown").each do |file|
    contents = File.read(file)
		@body = Markdown.new(ERB.new(contents).result(binding)).to_html
		@filename = File.basename(file).gsub(/markdown$/,"html")
		File.open("output/" + @filename, "w") do |f|
			f.puts template.result(binding)
		end
  end

	# Add the user manual
	FileUtils.mkdir("output/images/UserManual")
	`cp -r UserManual/output/images/* output/images/UserManual`
	@filename = 'user_manual.html'
	@body = File.read("UserManual/output/UserManual.html")
	@title = "User Manual"
	File.open("output/user_manual.html", "w") do |f|
		f.puts template.result(binding)
	end

  `open output/index.html`
end

task :generate_usermanual do
  require 'rdiscount'
  `rm -rf UserManual/output`
  FileUtils.mkdir("UserManual/output")
  `cat UserManual/text/*.markdown > output/UserManual.markdown`
  `cp -r UserManual/assets/images UserManual/output`
 
  source = Dir.glob("UserManual/text/**/*.markdown").map do |file|
    File.read(file)
  end.join("\n")
  body = Markdown.new(source).to_html
  File.open("UserManual/output/UserManual.html", "w") do |f|
    f.puts body
  end
end

task :generate_appcast do
    require 'lib/release_notes.rb'
    require 'rdiscount'
    FileUtils.mkdir_p("output/Downloads")
    template = ERB.new(File.read("templates/sparkle.xml"))

    filename = ENV["STABLE"] ? "appcast.xml" : "appcast_DEBUG.xml"
    File.open("output/Downloads/#{filename}", "w") do |f|
        f.puts template.result(binding)
    end
end

task :upload do
  `rsync -a output/ sydney:public_html/gitx/`
end