#!/usr/bin/env ruby
#
#  Created on 2008-3-9.
#  Copyright (c) 2008. Daniel Cadenas. All rights reserved.
#  http://depgraph.rubyforge.org

begin
  require 'rubygems'
  gem 'optiflag'
  gem 'ruby-graphviz'
  require 'optiflag'
  require 'graphviz'
rescue LoadError
  puts "depgraph requires the optiflag RubyGem."
  puts "Installation: gem install optiflag -y"
  exit
end

$LOAD_PATH.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')))

require 'graph_creator'


module CommandLineParameters extend OptiFlagSet
  flag "type" do
    dependency_types = DepGraph::GraphCreator.types    
    description "Type of dependencies to analyze: #{dependency_types.join(', ')}"
    value_in_set dependency_types
  end
  
  optional_flag "exc" do
    description "The node names to be excluded.\nUse double quotes and commas for multiple nodes."
  end
  
  optional_flag "output" do
    description "The file name of the graph image."
  end

  optional_flag "from" do
    description "Regular expression that must be met by the dependency source."
  end
  
  optional_flag "to" do
    description "Regular expression that must be met by the dependency target."
  end
  
  optional_flag "location" do
    description "A string defining the place where the nodes to analyse are found.\nThis are directories for most dependency types.\n
Use double quotes and commas for more than one directory."
  end
  
  optional_switch_flag "trans" do
    description "Applies a transitive reduction to the graph"
  end
  
  usage_flag "h","help","?"
  
  and_process!
end 

type = CommandLineParameters.flags.type
output = CommandLineParameters.flags.output || 'dependency_graph.png'
location = CommandLineParameters.flags.location || '.'
from = CommandLineParameters.flags.from if CommandLineParameters.flags.from?
to = CommandLineParameters.flags.to if CommandLineParameters.flags.to?
exc = CommandLineParameters.flags.exc || ''
trans = CommandLineParameters.flags.trans?

puts "Creating #{type} dependency graph. Please wait..."

dep_grapher = DepGraph::GraphCreator.new(type)
dep_grapher.location = location.split(',')
dep_grapher.from = from
dep_grapher.to = to
dep_grapher.trans = trans
dep_grapher.excluded_nodes = exc.split(',')

if dep_grapher.create_image(output)
  puts "Done.\nResults in #{output}."
else
  puts "No dependency graph image created."
end
