#!/usr/bin/env ruby
# encoding: UTF-8
#require 'rubygems'
require 'optparse'
require "svn/client"
require "svn/ext/client"
# Esci Class
class Esci
	def initialize(repo, verbose, dry)
		@ctx = Svn::Client::Context.new
		@ctx.add_simple_provider
  	@ctx.add_ssl_server_trust_file_provider
		@repo = repo;	@dry = dry;	@verbose = verbose
	end
	# Debugger
	def test_load(first, last, limit)
		log(@repo, first, last, limit, true, true) do |paths|
			p paths.changed_paths
			puts
			p paths.changed_paths2
		end
	end
	# svn log
	def log(paths, start_rev, end_rev, limit, discover_changed_paths, strict_node_history, peg_rev=nil)
		paths = [paths] unless paths.is_a?(Array)
		receiver = Proc.new do |changed_paths|
			yield(changed_paths)
		end
		Svn::Ext::Client.svn_client_log5(paths, peg_rev, [[start_rev,end_rev],[start_rev,end_rev]], limit, discover_changed_paths, strict_node_history, true, nil, receiver, @ctx)
	end
	# Return valid URI
	def escape_uri(repo, path)
		URI.escape(File.join(repo, path).gsub(/\/\Z/,''))
	end
end
# OptionParser
options = {}
opts = OptionParser.new
opts.banner = "User mode: #{__FILE__} [options] URL\n\nOptions for settings [options]:"
opts.on "-h", "--help", "-?", "Helper. (This page)" do |v|
	options[:help] = v
	puts opts
	exit(1)
end
options[:f] = 1
opts.on "-f", "--from NUMBER", Integer, "Inicial Revision." do |v|
	options[:from] = v
end
options[:to] = 'HEAD'
opts.on "-t", "--to NUMBER", Integer, "Final Revision." do |v|
	options[:to] = v
end
options[:l] = 0
opts.on "-l", "--limit NUMBER", Integer, "Limite Revision." do |v|
	options[:l] = v
end
opts.on "-d", "--dry-run", "Dry run mode." do |v|
	options[:d] = v
end
opts.on "-v", "--verbose", "Verbose mode." do |v|
	options[:v] = v
end
# Get URL
URL = opts.parse!(ARGV).first
# Exit case don't pass URL
if ARGV.empty? or URL.nil?
	puts opts 
	exit(1)
end
# Main
ctx = Esci.new(URL, options[:v], options[:d])
ctx.test_load(options[:from], options[:to], options[:l])

