#!/usr/bin/ruby.ruby4.0 

$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
require 'parade'
require 'parade/version'
require 'parade/commands/commands'

require 'rubygems'
require 'gli'

include GLI

version Parade::VERSION

desc 'Generate assets (e.g. sections or slides) for your presentation'
arg_name 'asset_name'
long_desc "#{Parade::Commands.generators.map {|k,v| "#{k} - #{v.description}" }.join("\n\n")}"
command [:g,:gen,:generate] do |c|

  c.desc "Force creation of the asset, even if one already exists with a similar name"
  c.switch [:f, :force]

  c.action do |global,local,args|

    asset_name = args.shift
    key_value_args = args.map {|arg| key, value = arg.split(/:|=/) ; [ key, value ] }.flatten
    parsed_arguments = Hash[*key_value_args]
    all_parameters = global.merge(local).merge(parsed_arguments)

    Parade::Commands.execute(:generator,asset_name,all_parameters) if asset_name
  end

end

desc 'Serves the parade presentation in the specified (or current) directory'
arg_name "[pres_dir]"
default_value "."
command [:s,:serve,:server] do |c|

  c.desc 'Show verbose messaging'
  c.switch :verbose

  c.desc 'Port on which to run'
  c.default_value "9090"
  c.flag [:p,:port]

  c.desc 'Host or ip to run on'
  c.default_value "localhost"
  c.flag [:h,:host]

  c.action do |global_options,options,args|

    url = "http://#{options[:h]}:#{options[:p].to_i}"
    puts "
-------------------------

  Your Parade presentation is now starting up.

  To view it visit [ #{url} ]
  To view a printable version [ #{url}/print ]

-------------------------

"
    presentation_filepath = args[0]

    unless File.directory?(presentation_filepath)
      filename = File.basename(presentation_filepath)
      presentation_filepath = File.dirname(presentation_filepath)
    end

    Parade::Server.run! :host => options[:h],
      :port => options[:p].to_i,
      :presentation_directory => presentation_filepath,
      :presentation_file => filename,
      :verbose => options[:verbose]

  end
end

desc 'Output the presentation in a particular format'
arg_name 'format_type'
arg_name '[pres_dir]'
long_desc "#{Parade::Commands.statics.map {|k,v| "#{k} - #{v.description}" }.join("\n\n")}"
command [:static] do |c|

  c.desc "Force creation of the asset, even if one already exists with a similar name"
  c.switch [:f, :force]

  c.desc 'Output file'
  c.flag [:o,:output]

  c.action do |global,local,args|

    format_type = args.shift
    presentation_filepath = args.shift || "."

    unless File.directory?(presentation_filepath)
      filename = File.basename(presentation_filepath)
      presentation_filepath = File.dirname(presentation_filepath)
    end

    key_value_args = args.map {|arg| key, value = arg.split(/:|=/) ; [ key, value ] }.flatten
    parsed_arguments = Hash[*key_value_args]
    all_parameters = global.merge(local).merge(parsed_arguments)

    all_parameters.merge!('filepath' => presentation_filepath, 'parade_file' => filename)
    Parade::Commands.execute(:static,format_type,all_parameters) if format_type
  end

end

# To allow an easier command-line to launch parade, the following format
#
#     `parade` is converted to `parade server .`
#     `parade [directoryname] is convereted `parade server [directoryname]`
#

parameters = ARGV

parameters = [ "server", "." ] if parameters.empty?
parameters.unshift "server" if parameters.count == 1 and not parameters.include?("help")

exit GLI.run(parameters)
