 
 module.exports = (robot) ->
   github = require("githubot")(robot)
-  robot.respond /pages (\w+)( \w+\/\w+)?/i, (msg) ->
-    console.log("Triggered!", msg)
-    command   = msg.match[1].strip()
+  robot.respond /pages (\w+)( \w+\/[\w\.]+)?/i, (msg) ->
+    command   = msg.match[1]
     repo      = github.qualified_repo msg.match[2]
 
-    github.handleErrors (response) ->
-      msg.send "Error fetching info about GitHub Pages site for #{repo}: #{response.error}"
+    unless command in ALLOWED_COMMANDS
+      msg.send "Sorry, what was that? '#{command}' isn't a recognized command."
+      return
+
+    info = github_pages_info github, repo, command
+    unless info?
+      msg.send "No info found. Make sure you're authenticated and are the owner of the repo."
 
     if command in INFO_COMMANDS
-      msg.send JSON.stringify(fetch_from_info(command))
+      msg.send JSON.stringify fetch_from_info(info, command)
     else if command in BUILD_COMMANDS
-      info = github_pages_info(github, repo, command)
       msg.send (formatted_build_text(build) for build in info).join("\n")
     else if command in LATEST_COMMANDS
-      info = github_pages_info(github, repo, command)
-      msg.send formatted_build_text(info)
-
-console.log("hubot-github-pages: at the end!")
+      msg.send formatted_build_text info

#
# Commands:
#   hubot pages <command> [repo] - fetch <command> about [repo]
#   hubot pages info [repo] - fetch all info about repo (default: HUBOT_GITHUB_REPO)
#   hubot pages cname - fetch cname of 
#   hubot pages latest - fetch latest build
#   hubot pages builds - fetch 
#
# Author:
#   parkr

console.log("hubot-github-pages has been require'd. Initting.")

timeago = require('timeago')

INFO_KEYS       = ["cname", "status", "custom_404"]
INFO_COMMANDS   = ["info"] + INFO_KEYS
BUILD_COMMANDS  = ["builds"]
LATEST_COMMANDS = ["latest"]

endpoint = (repo, command) ->
  base = "/repos/#{repo}/pages"
  if command in INFO_COMMANDS
    base
  else if command in BUILD_COMMANDS

    "#{base}/builds/latest"

github_pages_info = (githubot, repo, command) ->
  githubot.handleErrors (response) ->
    console.log "Error fetching info about GitHub Pages site for #{repo}: #{reponse.statusCode} #{response.error}"
  pages_info = null
  githubot.get endpoint(repo, command), (info) ->
    pages_info = info
  pages_info

fetch_from_info = (info, command) ->
  if command in INFO_KEYS
    info[command]
  else
    info

