#!/usr/bin/ruby

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

require 'fileutils'
require 'optparse'
require 'yaml'
require 'kafo/configuration'


# Option Parsing
options = {}
OptionParser.new do |opts|
  opts.banner = "Usage: kafofy"
  options[:config_dir] = './config/installer-scenarios.d/'
  opts.on("-c", "--config_dir DIR", "location of the scenarios configuration directory [./config/installer-scenarios.d/]") do |config_dir|
    options[:config_dir] = config_dir
  end
  opts.on("-s", "--scenario SCENARIO", "scenario file name (without extension) [default]") do |scenario|
    options[:scenario] = scenario
  end
  opts.on("-a", "--answer_file ANSWERS", "answer file file name (without extension) [default-answers]") do |answer_file|
    options[:answer_file] = File.join(options[:config_dir], answer_file + '.yaml')
  end
  opts.on("-n", "--name NAME", "installer name [kafo-configure]") do |name|
    options[:name] = name
  end
end.parse!

config = Kafo::Configuration::DEFAULT
options[:scenario] ||= 'default'
options[:answer_file] ||= File.join(options[:config_dir],  options[:scenario] + '-answers.yaml')
options[:name] ||= "kafo-configure"
options[:config_file] ||= File.join(options[:config_dir], options[:scenario] + '.yaml')

# Create directory structure
dirs = %w(bin config modules hooks) << options[:config_dir]
dirs.each do |dir|
  FileUtils.mkdir_p dir
end

# Copy config files
src = File.expand_path(File.join(File.dirname(__FILE__), '..'))
%w(config_header.txt kafo.yaml.example).each do |file|
  FileUtils.cp src + "/config/#{file}", 'config/'
end

# Create default config file
puts "creating #{options[:config_file]} as a default scenario file"
if !File.exists?(options[:config_file])
  puts "... creating config file #{options[:config_file]}"
  FileUtils.touch options[:config_file]
  File.chmod 0600, options[:config_file]
  FileUtils.cp('config/kafo.yaml.example', options[:config_file])
  if options[:answer_file]
    `sed -i 's/^# :answer_file.*$/:answer_file: #{options[:answer_file].gsub('/', '\/')}/' #{options[:config_file]}`
    `sed -i 's/^# :name.*$/:name: #{options[:scenario]}/' #{options[:config_file]}`
  end
end

# Installer script
script_name = "bin/#{options[:name]}"
puts "... creating #{script_name}"
if !File.exists?(script_name)
  content = <<EOS
#!/usr/bin/env ruby
require 'rubygems'
require 'kafo'

# where to find scenarios
CONFIG_DIR = '#{options[:config_dir]}'

# Run the install
@result = Kafo::KafoConfigure.run

# handle exit code when help was invoked or installer ended with '2' (success in puppet)
if @result.nil? || (!@result.config.app[:detailed_exitcodes] && @result.exit_code == 2)
  exit(0)
else
  exit(@result.exit_code)
end
EOS
  File.open(script_name, 'w') { |file| file.write(content) }
  FileUtils.chmod 0755, script_name
end

puts "Your directory was kafofied"

puts "Now you should:"
puts "  1. upload your puppet modules to modules directory (you can use librarian-puppet project)"
puts "  2. create default #{options[:answer_file]} or modify #{options[:config_file]} to load another answer file"
puts "  3. run #{script_name} to install your modules"
puts "  Note: You can add more scenarios by running kafofy multiple times"