require 'webrick'
require 'asciidoctor'
require 'asciidoctor-pdf'
require 'asciidoctor-epub3'

desc 'Run doc webserver'
task :serve do
  root = File.expand_path './_build/'
  `cp images ./_build/images -rv`
  server = WEBrick::HTTPServer.new Port: 3000, DocumentRoot: root
  trap 'INT' do server.shutdown end
  server.start
end

desc 'compile the docs'
task :compile do
  Asciidoctor .convert_file(
    'index.adoc', to_dir: '_build/', mkdirs: true, safe: :safe,
    backend: :html5, header_footer: true, sourcemap: true,
    stylesdir: 'stylesheets',
    base_dir: './',
    attributes: {
      'stylesdir' => 'styles',
      'imagesdir' => './images',
      'nofooter' => true,
      'allow-uri-read' => true,
      'stylesheet' => 'style.css'
    })
  `cp images ./_build/images -rv`
  Asciidoctor .convert_file(
    'index.adoc', to_dir: '_build/', mkdirs: true, safe: :safe,
    backend: :pdf, header_footer: true, sourcemap: true, to_file: 'hellhound.pdf',
    stylesdir: 'stylesheets',
    base_dir: './',
    attributes: {
      'stylesdir' => 'styles',
      'imagesdir' => './images',
      'nofooter' => true,
      'allow-uri-read' => true,
      'stylesheet' => 'style.css'
    })

  Asciidoctor .convert_file(
    'index.adoc', to_dir: '_build/', mkdirs: true, safe: :safe,
    backend: :epub3, header_footer: true, sourcemap: true, to_file: 'hellhound.epub',
    stylesdir: 'stylesheets',
    base_dir: './',
    attributes: {
      'stylesdir' => 'styles',
      'imagesdir' => './images',
      'nofooter' => true,
      'allow-uri-read' => true,
      'stylesheet' => 'style.css'
    })

  puts "You need to set KINDLEGEN env."
  Asciidoctor .convert_file(
    'index.adoc', to_dir: '_build/', mkdirs: true, safe: :safe,
    backend: :epub3, header_footer: true, sourcemap: true, to_file: 'hellhound.mobi',
    stylesdir: 'stylesheets',
    base_dir: './',
    attributes: {
      'ebook-format' => 'kf8',
      'stylesdir' => 'styles',
      'imagesdir' => './images',
      'nofooter' => true,
      'allow-uri-read' => true,
      'front-cover-image' => 'image:cover.svg[Front Cover,1050,1050]',
      'stylesheet' => 'style.css'
    })
  `cp images ./_build/ -rv`
  `rm -rf hellhound-kf8.epub`
end

task default: :serve
