#!/usr/bin/env ruby

require File.expand_path(File.join(File.dirname(__FILE__), %w[.. lib rhesus]))
require File.expand_path(File.join(File.dirname(__FILE__), %w[.. lib version]))

require 'fileutils'

def help(message = nil)
  puts message + "\\\n-----------------------------------\\\n" unless message.nil?
  puts <<-ENDL
  Usage:
      rhesus -h/--help
      rhesus -v/--version
      rhesus --setup

      rhesus command [options]

  Commands:
      list 
        lists available templates.

      gen <template-name>
        Generates files fom given template name.

  ENDL
end

def process_template template_name
  puts " - Using template #{template_name.inspect}"

  var_set = {}
  Neurogami::Rhesus::Core.load_options  template_name

  Neurogami::Rhesus::Core.required_vars_for_template_set(template_name).each do |var|
    print "Value for #{var}: "
    var_set[var] = gets.strip
  end

  if var_set.empty?
    puts "The templates have no variables. "
    print "Is this OK? [Y/n]"

    if gets.strip =~ /n|N/
      puts "OK, exiting"
      return
    end

  else
    puts "Using values "
    var_set.each { |k,v| puts "#{k}: #{v}" }

    print "Is this OK? [Y/n]"

    if gets.strip =~ /n|N/
      puts "OK, exiting"
      return
    end

  end

  print "Under what directory do you want these files written? "
  location = gets.strip
  location = './' if location.empty?

  Neurogami::Rhesus::Core::selected_template_files(template_name).each do |path|
    next unless File.file?(path)
    Neurogami::Rhesus::Core::process template_name, var_set, location, path
  end

end

def list_templates
  puts  Neurogami::Rhesus::Core.templates
end

def setup
  # Needs to create ~/.rhesus if it does not exist,
  # then copy over some default templates, such as one for
  # creating gems that bundle a template
  warn "Setting up Rhesus ..."
  create_rhesus_dir
  copy_over_default_templates
end

def create_rhesus_dir
  return if Neurogami::Rhesus::Core.user_dir_exists?
  FileUtils.mkdir_p  Neurogami::Rhesus::Core.user_template_directory

end

def copy_over_default_templates
  raise "Can not find your .rhesus directory!" unless Neurogami::Rhesus::Core.user_dir_exists?
  FileUtils.cp_r File.dirname(__FILE__) + "/../default-templates/.", Neurogami::Rhesus::Core.user_template_directory
end

def generate_from_template filter = /.*/
  
  filter = Regexp.new(filter) unless filter.is_a?(Regexp)

  if ARGV.first
    ARGV.first.strip!
    t = Neurogami::Rhesus::Core.templates.find{ |t| t == ARGV.first }  
    ARGV.clear
    process_template  t
  else
    templates = {}
    Neurogami::Rhesus::Core.templates.select{ |t| t =~ filter }.each_with_index do |template, idx|
      templates[idx+1] = template
      puts "#{idx+1}: #{template}"
    end
    if templates.empty?
      warn "No templates matched on #{filter.pretty_inspect.strip}."
    else
      item =  0
      while item < 1 || item-1 > Neurogami::Rhesus::Core.templates.size
        print "Enter the number of the template to use: "
        item = gets.to_i
      end

      process_template templates[item] # Neurogami::Rhesus::Core.templates[item-1]
    end

  end
end

#-------------------------------------------------

if ARGV.empty?
  help
else
  option = ARGV.shift
  case option 
  when "-h" || "--help"
    help
  when "-v" || "--version"
    puts "Rhesus version #{Neurogami::Rhesus::VERSION}"
  when "list"
    puts "Templates"
    list_templates
  when "gen"
    generate_from_template

  when '--setup'
    setup

  when '--install' || '-i'
     install_template_from_repo
  else
    #puts "Nothing known about #{ARGV[0]}"
    # Treat the same as gen, but use the argument as a pattern filter to reduce the number of items show
    generate_from_template option.strip
  end
end

