require 'guard/compat/plugin'
require 'webrick'

module ::Guard
  class Serv < Plugin
	def initialize(options = {})
		super
		@options = {
			host: '127.0.0.1',
			port: 3000
		}.merge(options)
		@server = ::WEBrick::HTTPServer.new Port: @options[:port], DocumentRoot: Dir::pwd
	end

	def start
		@thread = Thread.new { @server.start }
	end

	def stop
		@server.shutdown
	end
  end
end

require 'erb'
require 'ostruct'

watch /index\.(md|erb)/ do
	erb = File.read 'index.erb'
	erb = ERB.new erb, nil, '-'
	namespace = OpenStruct.new dev: true
	html = erb.result namespace.instance_eval { binding }
	File.write 'index.html', html
end

guard :serv

guard :livereload, apply_css_live: true, override_url: false  do
	watch 'index.html'
	watch /.*\.css/
	watch /.*\.png/
end
