#!/usr/bin/ruby
#
# This file is part of CPEE-EVAL-RUBY`.
#
# CPEE-EVAL-RUBY is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# CPEE-EVAL-RUBY is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# CPEE-EVAL-RUBY (file LICENSE in the main directory).  If not, see
# <http://www.gnu.org/licenses/>.

curpath = __dir__
require 'rubygems'
require 'optparse'
require 'fileutils'
require 'xml/smart'

def wrap(s, width=78, indent=18)
	lines = []
	line, s = s[0..indent-2], s[indent..-1]
  s.split(/\n/).each do |ss|
    ss.split(/[ \t]+/).each do |word|
      if line.size + word.size >= width
        lines << line
        line = (" " * (indent)) + word
      else
        line << " " << word
      end
    end
    lines << line if line
    line = (" " * (indent-1))
  end
	return lines.join "\n"
end

ARGV.options { |opt|
  opt.summary_indent = ' ' * 2
  opt.summary_width = 15
  opt.banner = "Usage:\n#{opt.summary_indent}#{File.basename($0)} [options] new DIR\n"
  opt.on("Options:")
  opt.on("--help", "-h", "This text") { puts opt; exit }
	opt.on("")
  opt.on(wrap("[new DIR]         scaffolds a sample eval ruby service. Send a request to it, see modifications to data elements and endpoints."))
  opt.parse!
}
if (ARGV.length <  2) ||
   (ARGV.length == 2  && !(%w(new).include?(ARGV[0]))) ||
   (ARGV.length >  2)
  puts ARGV.options
  exit
end
command = ARGV[0]
p1      = ARGV[1]

if command == 'new'
  insta = "#{curpath}/../server/"
  if !File.exist?(p1)
    FileUtils.cp_r(insta,p1)
  else
    FileUtils.cp_r(Dir.glob(File.join(insta,'*')).delete_if{|e| e =~ /\.conf/ },p1,remove_destination: true)
    puts 'Directory already exists, updating ...'
  end
end
