#!/usr/bin/env ruby

require 'rubygems'
require 'bind'
require 'bind/command_helpers'
require 'commander'

program :name, 'Bind'
program :version, Bind::VERSION::STRING
program :description, 'Bind actions to filesystem events'

command :to do |c|
  set_common_options c
  c.syntax = 'bind to <file> [file ...] [options] '
  c.summary = 'Bind to modification of a file'
  c.description = 'Bind to modification of a file or all files within a directory.'
  c.example 'Bind to a single file, logging its path when changed', "bind change style.css -e 'puts file'"
  c.option '-e', '--eval STRING', String, 'Evaluate a string of Ruby in context of Bind, so the file local variable is available.'
  c.when_called do |args, options|
    populate_common_options options
    if options.eval
      options.action = lambda { |file| eval options.eval }
    else
      abort 'invalid option. --eval switch is required in order to perform any action on the bound file(s)'
    end
    options.files = args
    listener(options).run!
  end
end

command :refresh do |c|
  set_common_options c
  c.syntax = 'bind refresh <uri> [options]'
  c.summary = 'Bind to <uri>, refreshing browsers.'
  c.description = 'Bind to <uri>, refreshing browsers when the files you are watching change.'
  c.example = 'Bind a few css files for quick editing in multiple browsers', 'bind refresh http://localhost/page --files style.css,reset.css --browsers Safari,Firefox'
  c.example = 'Bind local static html (no scheme)', 'bind refresh examples/demo.html -f style.css -b Safari'
  c.option '-b', '--browsers BROWSERS', Array, 'List of browsers you wish to refresh. Defaults to Safari'
  c.option '-f', '--files FILES', Array, 'List of files to bind to.'
  c.when_called do |args, options|
    populate_common_options options
    path = expand_path args.shift
    browsers = options.browsers || ['Safari']
    options.action = Bind::Actions::RefreshBrowsers.new path, *browsers
    abort 'invalid option. --files switch is required in order to bind' if options.files.nil?
    listener(options).run!
  end
end