require 'English'
require 'html-proofer'
require 'jekyll'
require 's3_website'

# Extend string to allow for bold text.
class String
  def bold
    ""
  end
end

# Usage: bundle exec rake deploy.  Clean, build, optimize, push to S3, and notify.
task :deploy => [ :git_master, :git_fetch, :clean, :build, :optimize, :push, :notify ] do
end

task :clean do
  puts 'Cleaning up _site...'.bold
  Jekyll::Commands::Clean.process({})
end

desc "build the site"
task :build do
  sh "bundle exec jekyll build"
end

# Usage: rake optimize
task default: %w[imageOptimize]
task :optimize do
  puts 'Compressing images...'.bold
  sh "image_optim -r ./_site/assets/images/"
end

task :push do
  puts 'Push site to Amazon S3...'.bold
  sh "bundle exec s3_website push --force"
end

# Usage: rake notify
task :notify => ["notify:pingomatic", "notify:google", "notify:bing", "notify:pingpubsubhubbub"]
desc "Notify various services that the site has been updated"
namespace :notify do

text = File.read("_config.yml")
    matchdata = text.match(/^url: (.*)$/)
        deploy_notify = matchdata[1]


  desc "Notify Ping-O-Matic"
  task :pingomatic do
    begin
      require 'xmlrpc/client'
      puts "* Notifying Ping-O-Matic that the site has updated"
      XMLRPC::Client.new('rpc.pingomatic.com', '/').call('weblogUpdates.extendedPing', '#{deploy_notify}' , '#{deploy_notify}', '#{deploy_notify}/feed.xml')
    rescue LoadError
      puts "! Could not ping ping-o-matic, because XMLRPC::Client could not be found."
    end
  end

  desc "Notify Google of updated sitemap"
  task :google do
    begin
      require 'net/http'
      require 'uri'
      puts "* Notifying Google that the site has updated"
      Net::HTTP.get('www.google.com', '/webmasters/tools/ping?sitemap=' + URI.escape('#{deploy_notify}/sitemap.xml'))
    rescue LoadError
      puts "! Could not ping Google about our sitemap, because Net::HTTP or URI could not be found."
    end
  end

  desc "Notify Bing of updated sitemap"
  task :bing do
    begin
      require 'net/http'
      require 'uri'
      puts '* Notifying Bing that the site has updated'
      Net::HTTP.get('www.bing.com', '/webmaster/ping.aspx?siteMap=' + URI.escape('#{deploy_notify}/sitemap.xml'))
    rescue LoadError
      puts "! Could not ping Bing about our sitemap, because Net::HTTP or URI could not be found."
    end
  end
    
  desc 'Notify pubsubhubbub server.'
  task :pingpubsubhubbub do
    cfg = YAML.load_file("_config.yml")
    deploy_notify = URI.extract(deploy_notify, ['http', 'https'])
#    data = 'hub.mode=publish&hub.url=' + "https://hubworks.com" + CGI::escape("/feed.xml")
    data = 'hub.mode=publish&hub.url=' + "#{deploy_notify[0]}" + CGI::escape("/feed.xml")
      
    headers = {'Content-Type' => 'application/x-www-form-urlencoded'}
    puts '* Pinging pubsubhubbub server'
    request = Net::HTTP.new('pubsubhubbub.appspot.com', 80)
    response = request.post('http://pubsubhubbub.appspot.com/publish', data, headers)
    if response.code == "200" || response.code == "204"
      puts "Successfully submitted sitemap to pubsubhubbub"
    else
      puts "pubsubhubbub response: #{response.body}"
      fail
    end
  end
end

# Usage: rake html_proofer
desc 'html-proofer checks the site for broken links, missing tags, etc.'
task :html_proofer do
  sh "bundle exec jekyll build"
  options = { :assume_extension => true, :check_favicon => true, :check_opengraph => true}
  HTMLProofer.check_directory(
    '_site/', options).run
end

# Usage: rake amp
desc 'Test website AMP validation'
task :amp do
  puts 'Running AMP Validator...'.bold
  system('say "running a.m.p. validator"')
  amp_dir = '_site/amp'
  system("find #{amp_dir} -iname *.html | xargs -L1 amphtml-validator")
  if $CHILD_STATUS.exitstatus.zero?
    puts 'AMP Validator finished successfully.'
    system('say "a.m.p validator finished successfuly"')
  else
    puts 'AMP Validator FAILED.'
    system('say "a m p validator found errors"')
    exit($CHILD_STATUS.exitstatus)   
  end
end

# Usage: rake git_master
desc 'Pull any new commits from the repository'
task :git_master do
  puts 'Switching To Github master branch...'.bold
  sh 'git checkout master'
end

# Usage: rake git_fetch
desc 'Pull any new commits from the repository'
task :git_fetch do
  puts 'Pulling in new commits...'.bold
  sh 'git fetch'
end<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?>
require 'English'
require 'html-proofer'
require 'jekyll'
require 's3_website'

# Extend string to allow for bold text.
class String
  def bold
    ""
  end
end

# Usage: bundle exec rake deploy.  Clean, build, optimize, push to S3, and notify.
task :deploy => [ :git_master, :git_fetch, :clean, :build, :optimize, :push, :notify ] do
end

task :clean do
  puts 'Cleaning up _site...'.bold
  Jekyll::Commands::Clean.process({})
end

desc "build the site"
task :build do
  sh "bundle exec jekyll build"
end

# Usage: rake optimize
task default: %w[imageOptimize]
task :optimize do
  puts 'Compressing images...'.bold
  sh "image_optim -r ./_site/assets/images/"
end

task :push do
  puts 'Push site to Amazon S3...'.bold
  sh "bundle exec s3_website push --force"
end

# Usage: rake notify
task :notify => ["notify:pingomatic", "notify:google", "notify:bing", "notify:pingpubsubhubbub"]
desc "Notify various services that the site has been updated"
namespace :notify do

text = File.read("_config.yml")
    matchdata = text.match(/^url: (.*)$/)
        deploy_notify = matchdata[1]


  desc "Notify Ping-O-Matic"
  task :pingomatic do
    begin
      require 'xmlrpc/client'
      puts "* Notifying Ping-O-Matic that the site has updated"
      XMLRPC::Client.new('rpc.pingomatic.com', '/').call('weblogUpdates.extendedPing', '#{deploy_notify}' , '#{deploy_notify}', '#{deploy_notify}/feed.xml')
    rescue LoadError
      puts "! Could not ping ping-o-matic, because XMLRPC::Client could not be found."
    end
  end

  desc "Notify Google of updated sitemap"
  task :google do
    begin
      require 'net/http'
      require 'uri'
      puts "* Notifying Google that the site has updated"
      Net::HTTP.get('www.google.com', '/webmasters/tools/ping?sitemap=' + URI.escape('#{deploy_notify}/sitemap.xml'))
    rescue LoadError
      puts "! Could not ping Google about our sitemap, because Net::HTTP or URI could not be found."
    end
  end

  desc "Notify Bing of updated sitemap"
  task :bing do
    begin
      require 'net/http'
      require 'uri'
      puts '* Notifying Bing that the site has updated'
      Net::HTTP.get('www.bing.com', '/webmaster/ping.aspx?siteMap=' + URI.escape('#{deploy_notify}/sitemap.xml'))
    rescue LoadError
      puts "! Could not ping Bing about our sitemap, because Net::HTTP or URI could not be found."
    end
  end
    
  desc 'Notify pubsubhubbub server.'
  task :pingpubsubhubbub do
    cfg = YAML.load_file("_config.yml")
    deploy_notify = URI.extract(deploy_notify, ['http', 'https'])
#    data = 'hub.mode=publish&hub.url=' + "https://hubworks.com" + CGI::escape("/feed.xml")
    data = 'hub.mode=publish&hub.url=' + "#{deploy_notify[0]}" + CGI::escape("/feed.xml")
      
    headers = {'Content-Type' => 'application/x-www-form-urlencoded'}
    puts '* Pinging pubsubhubbub server'
    request = Net::HTTP.new('pubsubhubbub.appspot.com', 80)
    response = request.post('http://pubsubhubbub.appspot.com/publish', data, headers)
    if response.code == "200" || response.code == "204"
      puts "Successfully submitted sitemap to pubsubhubbub"
    else
      puts "pubsubhubbub response: #{response.body}"
      fail
    end
  end
end

# Usage: rake html_proofer
desc 'html-proofer checks the site for broken links, missing tags, etc.'
task :html_proofer do
  sh "bundle exec jekyll build"
  options = { :assume_extension => true, :check_favicon => true, :check_opengraph => true}
  HTMLProofer.check_directory(
    '_site/', options).run
end

# Usage: rake amp
desc 'Test website AMP validation'
task :amp do
  puts 'Running AMP Validator...'.bold
  system('say "running a.m.p. validator"')
  amp_dir = '_site/amp'
  system("find #{amp_dir} -iname *.html | xargs -L1 amphtml-validator")
  if $CHILD_STATUS.exitstatus.zero?
    puts 'AMP Validator finished successfully.'
    system('say "a.m.p validator finished successfuly"')
  else
    puts 'AMP Validator FAILED.'
    system('say "a m p validator found errors"')
    exit($CHILD_STATUS.exitstatus)   
  end
end

# Usage: rake git_master
desc 'Pull any new commits from the repository'
task :git_master do
  puts 'Switching To Github master branch...'.bold
  sh 'git checkout master'
end

# Usage: rake git_fetch
desc 'Pull any new commits from the repository'
task :git_fetch do
  puts 'Pulling in new commits...'.bold
  sh 'git fetch'
endrequire 'English'
require 'html-proofer'
require 'jekyll'
require 's3_website'

# Extend string to allow for bold text.
class String
  def bold
    ""
  end
end

# Usage: bundle exec rake deploy.  Clean, build, optimize, push to S3, and notify.
task :deploy => [ :git_master, :git_fetch, :clean, :build, :optimize, :push, :notify ] do
end

task :clean do
  puts 'Cleaning up _site...'.bold
  Jekyll::Commands::Clean.process({})
end

desc "build the site"
task :build do
  sh "bundle exec jekyll build"
end

# Usage: rake optimize
task default: %w[imageOptimize]
task :optimize do
  puts 'Compressing images...'.bold
  sh "image_optim -r ./_site/assets/images/"
end

task :push do
  puts 'Push site to Amazon S3...'.bold
  sh "bundle exec s3_website push --force"
end

# Usage: rake notify
task :notify => ["notify:pingomatic", "notify:google", "notify:bing", "notify:pingpubsubhubbub"]
desc "Notify various services that the site has been updated"
namespace :notify do

text = File.read("_config.yml")
    matchdata = text.match(/^url: (.*)$/)
        deploy_notify = matchdata[1]


  desc "Notify Ping-O-Matic"
  task :pingomatic do
    begin
      require 'xmlrpc/client'
      puts "* Notifying Ping-O-Matic that the site has updated"
      XMLRPC::Client.new('rpc.pingomatic.com', '/').call('weblogUpdates.extendedPing', '#{deploy_notify}' , '#{deploy_notify}', '#{deploy_notify}/feed.xml')
    rescue LoadError
      puts "! Could not ping ping-o-matic, because XMLRPC::Client could not be found."
    end
  end

  desc "Notify Google of updated sitemap"
  task :google do
    begin
      require 'net/http'
      require 'uri'
      puts "* Notifying Google that the site has updated"
      Net::HTTP.get('www.google.com', '/webmasters/tools/ping?sitemap=' + URI.escape('#{deploy_notify}/sitemap.xml'))
    rescue LoadError
      puts "! Could not ping Google about our sitemap, because Net::HTTP or URI could not be found."
    end
  end

  desc "Notify Bing of updated sitemap"
  task :bing do
    begin
      require 'net/http'
      require 'uri'
      puts '* Notifying Bing that the site has updated'
      Net::HTTP.get('www.bing.com', '/webmaster/ping.aspx?siteMap=' + URI.escape('#{deploy_notify}/sitemap.xml'))
    rescue LoadError
      puts "! Could not ping Bing about our sitemap, because Net::HTTP or URI could not be found."
    end
  end
    
  desc 'Notify pubsubhubbub server.'
  task :pingpubsubhubbub do
    cfg = YAML.load_file("_config.yml")
    deploy_notify = URI.extract(deploy_notify, ['http', 'https'])
#    data = 'hub.mode=publish&hub.url=' + "https://hubworks.com" + CGI::escape("/feed.xml")
    data = 'hub.mode=publish&hub.url=' + "#{deploy_notify[0]}" + CGI::escape("/feed.xml")
      
    headers = {'Content-Type' => 'application/x-www-form-urlencoded'}
    puts '* Pinging pubsubhubbub server'
    request = Net::HTTP.new('pubsubhubbub.appspot.com', 80)
    response = request.post('http://pubsubhubbub.appspot.com/publish', data, headers)
    if response.code == "200" || response.code == "204"
      puts "Successfully submitted sitemap to pubsubhubbub"
    else
      puts "pubsubhubbub response: #{response.body}"
      fail
    end
  end
end

# Usage: rake html_proofer
desc 'html-proofer checks the site for broken links, missing tags, etc.'
task :html_proofer do
  sh "bundle exec jekyll build"
  options = { :assume_extension => true, :check_favicon => true, :check_opengraph => true}
  HTMLProofer.check_directory(
    '_site/', options).run
end

# Usage: rake amp
desc 'Test website AMP validation'
task :amp do
  puts 'Running AMP Validator...'.bold
  system('say "running a.m.p. validator"')
  amp_dir = '_site/amp'
  system("find #{amp_dir} -iname *.html | xargs -L1 amphtml-validator")
  if $CHILD_STATUS.exitstatus.zero?
    puts 'AMP Validator finished successfully.'
    system('say "a.m.p validator finished successfuly"')
  else
    puts 'AMP Validator FAILED.'
    system('say "a m p validator found errors"')
    exit($CHILD_STATUS.exitstatus)   
  end
end

# Usage: rake git_master
desc 'Pull any new commits from the repository'
task :git_master do
  puts 'Switching To Github master branch...'.bold
  sh 'git checkout master'
end

# Usage: rake git_fetch
desc 'Pull any new commits from the repository'
task :git_fetch do
  puts 'Pulling in new commits...'.bold
  sh 'git fetch'
end