#!/usr/pkg/bin/ruby193

begin
   require 'rubygems'
   require 'uv'
rescue LoadError
   $:.unshift File.join( File.dirname(__FILE__), '..', 'lib' )
   require 'uv'
end

require 'optparse'
require 'ostruct'

o = OpenStruct.new
o.copy_files = false 
o.output = "xhtml"
o.headers = false
o.theme = "espresso_libre"
o.lines = true

options = OptionParser.new

options.banner =<<END

Usage: uv [options] input_file

Parses input_file (or stdin, if no input_file is given) and 
outputs the coresponding HTML to stdout. If no syntax is specified, 
it tries to guess the best one. If no theme is specified, defaults 
to espresso_libre.

Example:

   uv -t amy -h ~/.bashrc > bashrc.html
   
   Renders the contents of ~/.bashrc to file bashrc.html
   as a standalone web page.

All options are non-mandatory

Options:

END

options.on( "-c DIR", "--copy-files DIR", <<DESCRIPTION ) {|val| o.copy_files = val}
Copy the required files to the specified output directory (css, images, etc).
DESCRIPTION

options.on( "-s SYNTAX", "--syntax SYNTAX", <<DESCRIPTION, Uv.syntaxes) {|val| o.syntax = val}
The file's syntax (e.g. ruby, c++, etc.) 
DESCRIPTION

options.on( "-o OUTPUT", "--output OUTPUT", <<DESCRIPTION) {|val| o.output = val}
The output format (xhtml, latex, etc.) default to xhtml
DESCRIPTION

options.on( "-t THEME", "--theme THEME", <<DESCRIPTION, Uv.themes) {|val| o.theme = val}
The theme to be used (e.g. amy, espresso_libre, etc.)
DESCRIPTION

options.on( "-n", "--no-lines", <<DESCRIPTION ) {|val| o.lines = false}
Produces output without line numbers
DESCRIPTION


options.on( "-h", "--headers", <<DESCRIPTION ) {|val| o.headers = val}
Include headers, outputs a self-contained web page/document
DESCRIPTION

options.on( "-d", "--debug", <<DESCRIPTION ) {|val| o.debug = val}
Outputs debug information instead of normal page rendering
DESCRIPTION
   
options.on( "-l [syntaxes|themes]", "--list [syntaxes|themes]", ['syntaxes', 'themes'], <<DESCRIPTION ) {|val| o.list = val}
Lists all the available syntaxes/themes
DESCRIPTION
   
   
options.on( "-?", "--help", <<DESCRIPTION ) {|val| o.help = val}
Show this message
DESCRIPTION

rest = options.parse( ARGV )


if o.help
   puts options
   exit
elsif o.list
   if o.list == 'syntaxes'
      puts "Available syntaxes:\n"
      Uv.syntaxes.sort.each{ |s| puts "   - #{s}"}
   elsif o.list == 'themes'
      puts "Available themes:\n"
      Uv.themes.sort.each{ |t| puts "   - #{t}"}
   else
      STDERR.puts "Option #{o.list} is not valid should be one of [syntaxes, themes]"
   end
   exit
end

if o.copy_files
   unless File.exists?(o.copy_files)
      STDERR.puts "The specified output directory: #{o.copy_files} does not exist."
      exit -1
   end
   Uv.copy_files o.output, o.copy_files
end

o.filename = rest[0]

if o.filename && ! o.syntax
   candidates = Uv.syntax_for_file o.filename
  if candidates.size > 1
      STDERR.puts "Many syntaxes match, please specify"
      STDERR.puts "\nMatching syntaxes:"
      candidates.sort.each { |name, syntax| STDERR.puts "\t - " + name}
      exit -1
   end
   o.syntax = candidates.first.first unless candidates.size == 0
end

unless o.syntax
   STDERR.puts "No default syntax found, please specify"
   exit -1
end

if o.filename
   o.text = File.read(o.filename) 
else
   o.text = STDIN
end

if o.debug
   Uv.debug( o.text, o.syntax )
   exit
end

puts Uv.parse( o.text, o.output,  o.syntax, o.lines, o.theme, o.headers )

