# Copyright 2011-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
#     http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.

require File.expand_path(File.dirname(__FILE__) + '/config/config')

require 's3_uploader'
require 'pg'



puts "\n Connecting to redshift to load data"
conn = PGconn.connect("dwprod01.ccvpswrgleoi.us-east-1.redshift.amazonaws.com",5439,"","","dev","masteruser","Orchard001")
res  = conn.exec("copy processed_dig_sales_delta from 's3://accounting_etl/processed_dig_sales' CREDENTIALS 'aws_access_key_id=#{config['access_key_id']};aws_secret_access_key=#{config['secret_access_key']}' delimiter '\t'  REMOVEQUOTES ESCAPE;")

puts "\n copy command executed with "
puts res.cmd_tuples()
puts "\n rows loaded"

abort('quitting, for now')


(bucket_name, file_path) = ARGV
unless bucket_name && file_path
  puts "Usage: upload_file.rb <BUCKET_NAME> <FILE_PATH>"
  exit 1
end
# get an instance of the S3 interface using the default configuration
s3 = AWS::S3.new

bucket = s3.buckets.create(bucket_name)
count_on_s3 = bucket.objects.with_prefix('processed_dig_sales').count



puts "bucket: #{bucket_name} has #{count_on_s3} objects, about to delete:"

puts "cleaning up bucket: #{bucket_name}"


bucket.objects.delete_all

count_on_s3 = bucket.objects.with_prefix('processed_dig_sales').count

puts "successfully? cleaned up bucket: #{bucket_name} there are now #{count_on_s3}"

count_on_s3 = bucket.objects.with_prefix('processed_dig_sales').count

path_to_upload = File.join(file_path,"**", "*")
to_upload_count = Dir.glob(path_to_upload).count

puts "there are #{to_upload_count} files to upload"

puts "copying #{file_path} to #{bucket_name} /processed_dig_sales"

S3Uploader.upload_directory(file_path, bucket_name, 
{ :s3_key => @config['access_key_id'],
        :s3_secret => @config['secret_access_key'], 
:destination_dir => 'processed_dig_sales/', :threads => 4 })
count_on_s3 = 0
while count_on_s3 < to_upload_count do
	count_on_s3 = bucket.objects.with_prefix('processed_dig_sales').count
	sleep(2)
	puts "there are #{count_on_s3} items in processed_dig_sales, and on the local filesystem: #{to_upload_count}"
	if count_on_s3 == to_upload_count
		break
	end
end


# create a bucket
#b = s3.buckets.create(bucket_name)

# upload a file
#basename = File.basename(file_name)
#o = b.objects[basename]
#o.write(:file => file_name)

puts "Uploaded #{file_path} to:"
#puts o.public_url

# generate a presigned URL
puts "\nUse this URL to download the file:"
#puts o.url_for(:read)

#puts "(press any key to delete the object)"
#$stdin.getc

#o.delete
#system("split -C 1M -d test.txt ''")
puts "\n Connecting to redshift to load data"
conn = PGconn.connect("dwprod01.ccvpswrgleoi.us-east-1.redshift.amazonaws.com",5439,"","","dev","masteruser","Orchard001")
res  = conn.exec("copy processed_dig_sales_delta from 's3://accounting_etl/processed_dig_sales' CREDENTIALS 'aws_access_key_id=#{config['access_key_id']};aws_secret_access_key=#{config['secret_access_key']}' delimiter '\t'  REMOVEQUOTES ESCAPE;")

puts "\n copy command executed with "
puts res.cmd_tuples()
puts "\n rows loaded" 
