#!/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}"

  var_set = {}

  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
  if ARGV.first
    puts "Generate from #{ARGV.first} template"
  else
    Neurogami::Rhesus::Core.templates.each_with_index do |template, idx|
      puts "#{idx+1}: #{template}"
    end
    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 Neurogami::Rhesus::Core.templates[item-1]
  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
  else
    puts "Nothing known about #{ARGV[0]}"
  end
end

