#!/usr/bin/env ruby

$LOAD_PATH.push File.expand_path("../lib", File.dirname(__FILE__)) # add sprite factory library to load path

require 'sprite_factory'
require 'optparse'

options   = { :report => true }
op        = OptionParser.new
op.banner = "#{SpriteFactory::DESCRIPTION}\nUsage: sprite <dir> [options]"

op.on("-h", "--help") do
  puts op.to_s
  exit
end

op.on("-v", "--version") do
  puts SpriteFactory::VERSION
  exit
end

layout_help       = "specify layout orientation   ( horizontal, vertical, packed )"
style_help        = "specify stylesheet syntax  ( css, scss, sass )"
library_help      = "specify image library to use ( rmagic, chunkypng )"
selector_help     = "specify custom selector to use for each css rule ( default: 'img.' )"
cssurl_help       = "specify custom string to use for css image urls ( default: 'url(output image basename)' )"
output_image_help = "specify output location for generated image ( default: <input folder>.png )"
output_style_help = "specify output location for generated stylesheet ( default: <input folder>.<style>)"
pngcrush_help     = "use pngcrush to optimize generated image"
padding_help      = "add padding to each sprite"
margin_help       = "add margin to each sprite"
glob_help         = "specify glob pattern used to find images ( default: '*' )"
nocomments_help   = "suppress comments in generated stylesheet"
sanitizer_help    = "strip non-word characters out of filenames when converting to css selector"

op.on("--layout       [ORIENTATION]", layout_help)       {|value| options[:layout]       = value }
op.on("--style        [STYLE]",       style_help)        {|value| options[:style]        = value }
op.on("--library      [LIBRARY]",     library_help)      {|value| options[:library]      = value }
op.on("--selector     [SELECTOR]",    selector_help)     {|value| options[:selector]     = value }
op.on("--cssurl       [CSSURL]",      cssurl_help)       {|value| options[:cssurl]       = value }
op.on("--output-image [PATH]",        output_image_help) {|value| options[:output_image] = value }
op.on("--output-style [PATH]",        output_style_help) {|value| options[:output_style] = value }
op.on("--pngcrush",                   pngcrush_help)     {|value| options[:pngcrush]     = value }
op.on("--padding      [PIXELS]",      padding_help)      {|value| options[:padding]      = value.to_i }
op.on("--margin       [PIXELS]",      margin_help)       {|value| options[:margin]       = value.to_i }
op.on("--glob         [PATTERN]",     glob_help)         {|value| options[:glob]         = value }
op.on("--nocomments",                 nocomments_help)   {|value| options[:nocomments]   = true  }
op.on("--sanitizer",                  sanitizer_help)    {|value| options[:sanitizer]    = true  }

begin
  op.parse!(ARGV)
  raise "a single argument must be specified containing images to be sprited" if ARGV.empty?
  SpriteFactory.run!(ARGV[0], options)
rescue Exception => ex
  puts ex.message
  exit
end
