task :default => 'man/chake.1'

file 'man/chake.1' => ['man/chake.adoc'] do
  command = 'asciidoctor --backend manpage --out-file man/chake.1 man/chake.adoc'
  if ENV['SOURCE_DATE_EPOCH']
    date = Time.at(ENV['SOURCE_DATE_EPOCH'].to_i)
    command << " --attribute docdate=#{date.strftime('%Y-%m-%d')}"
  end
  sh command
end

file 'man/chake.adoc' => ['README.md', 'man/readme2man.sed'] do |t|
  sh "sed -f man/readme2man.sed README.md > #{t.name} || (rm -f #{t.name}; false)"
end

task :install => 'man/chake.1' do
  prefix = ENV['PREFIX'] || File.exists?('debian/rules') && '/usr' || '/usr/local'
  target = [ENV["DESTDIR"], prefix , 'share/man/man1'].compact
  man = File.join(*target)
  sh 'install', '-d', '-m', '0755', man
  sh 'install', '-m', '0644', 'man/chake.1', man
end

task :clean do
  rm_f 'man/chake.1'
  rm_f 'man/chake.adoc'
end
