#!/usr/bin/env ruby
require 'optparse'

require_relative '../lib/easy-swig'

options = {}

opt_parser = OptionParser.new do |opt|
  opt.banner = "Welcome to Easy Swig - SWIG made easy!!"
  opt.banner = "Usage: easy-swig COMMAND [OPTIONS]"
  opt.separator ""
  opt.separator "Commands (Use given options to override defaults)"
  opt.separator "     doxygen: Parses header files (.h) and generates XMLs using Doxygen. Tune with --input, --doxy, --name, --html"
  opt.separator "     generate: Generates SWIG Interface Files from XMLs. Tune with --csv, --doxy, --gen, --name, --lang, --custom,"
  opt.separator "     swig: Runs SWIG against the specified interface files. Tune with --target, --input, --output, --include, --lang"
  opt.separator ""
  opt.separator "Options"

  opt.on("-d", "--default DIRECTORY", "Uses the given directory for default values for all needed options") do |dir|
    options[:default] = dir
  end

  opt.on("-n", "--name MODULE_NAME", "Name of the module. Used to name generated resources") do |name|
    options[:mod_name] = name
  end

  opt.on("-c", "--csv FILE", "CSV configuration file input") do |file|
    options[:csvfile] = file
  end

  opt.on("-i", "--input DIRECTORY", "Directory for your project's header files") do |dir|
    options[:hdir] = dir
  end

  opt.on("-I", "--include DIRECTORY", "Directory for additional header files not part of your project. Example: /usr/include") do |dir|
  	options[:incdir] ||= []
    options[:incdir] << dir
  end

  opt.on("-x", "--doxy DIRECTORY", "Directory generated by Doxygen (which include a /xml subdirectory for all XML files") do |dir|
    options[:xdir] = dir
  end

  opt.on("-m", "--html", "Instructs Doxygen to also generate HTML documentation for the header files") do
    options[:html] = true
  end

  opt.on("-g", "--gen DIRECTORY", "Output directory for Easy-Swig generated .i file(s) aka. 'target'") do |dir|
    options[:gdir] = dir
  end

  opt.on("-o", "--output DIRECTORY", "Output directory for SWIG generated files") do |dir|
    options[:odir] = dir
  end

  opt.on("-l", "--lang LANGUAGE", "Target language for SWIG") do |lang|
    options[:lang] = lang
  end

  opt.on("-t", "--target PATH", "Target .i file as input for SWIG. For directories, SWIG will be invoked once for every .i file") do |path|
    options[:target] = path
  end

  opt.on("-u", "--custom FILE", "Custom .i file to be appended at the beginning of the generated .i target. Used for advanced configuration") do |file|
    options[:custom] = file
  end
  
  opt.on("-s", "--no_stl", "Disable support for the C++ Standard Template Library STL") do |file|
    options[:no_stl] = true
  end

  opt.on("-h", "--help", "Help") do
    puts opt_parser
  end

  opt.separator ""
  opt.separator "Default option values:"
  opt.separator ""
  opt.separator "-d (--default DIRECTORY) Defaults to the current working directory"
  opt.separator "-n (--name MODULE_NAME) Defaults to the name of the default directory"
  opt.separator "-c (--csv FILE) Defaults to: path/to/default/dir/api.csv"
  opt.separator "-i (--input DIRECTORY) Defaults to: path/to/default/dir/include"
  opt.separator "-I (--include DIRECTORY) Defaults to linux include path: usr/include"
  opt.separator "-x (--doxy DIRECTORY) Defaults to: path/to/default/dir/easy-swig/doxygen"
  opt.separator "-m (--html) Defaults to false"
  opt.separator "-s (--no_stl) Defaults to false"
  opt.separator "-g (--gen DIRECTORY) Defaults to path/to/default/dir/easy-swig/generate"
  opt.separator "-o (--output DIRECTORY) Defaults to : path/to/default/dir/easy-swig/swig"
  opt.separator "-l (--lang LANGUAGE) Defaults to C# (csharp)"
  opt.separator "-t (--target PATH) Defaults to path/to/default/dir/easy-swig/generate/<MODULE_NAME>.i"
  opt.separator "-u (--custom FILE) Defaults to path/to/default/dir/custom_config.i"

end

opt_parser.parse!

config = ::EasySwig::Config.new

if !options[:default].nil? && Dir.exists?(options[:default])
	puts 'Default directory: ' + options[:default]
	config.default_dir = options[:default]
else
  puts 'No valid default directory was provided: Use current working directory as default? (y/n)'
  $stdout.flush
  answer = $stdin.gets
  exit! unless answer =~ /^[yY]/
end

config.module_name = options[:mod_name]
config.doxy_dir = options[:xdir]
config.headers_dir = options[:hdir]
config.csv_file = options[:csvfile]
config.output_dir = options[:odir]
config.generate_dir = options[:gdir]
config.html = options[:html]
config.lang = options[:lang]
config.stl_support = options[:no_stl].nil? ? 'YES' : nil

options[:custom] ||= "custom_config.i"

if File.exists?(options[:custom])
  puts 'Custom configuration file: ' + options[:custom]
  config.custom_file = options[:custom]
else
	config.custom_file = nil
  puts 'No custom configuration file found' 
end

config.target_file = options[:target]
config.includes_dir = options[:incdir]

config.set_defaults

case ARGV[0]
  when "doxygen"
    EasySwig::doxygen(config)
  when "generate"
    EasySwig::generate(config)
  when "swig"
    EasySwig::swig(config)
else
puts opt_parser
end