#!/usr/bin/env ruby
$LOAD_PATH.unshift(File.expand_path(File.join('..', 'lib'))) unless $LOAD_PATH.include?(File.expand_path(File.join('..', 'lib')))

require 'core'

options = {}
options[:open] = false
options[:ignore_files] = nil
# TODO: convert the tasks to use thor or Rex::Parser::Arguments from 'rex'
OptionParser.new do |opts|
  opts.banner = "Usage: #{$PROGRAM_NAME} [options]\nRise version: #{Rise::Constants::VERSION}"
  opts.separator Paint["\nGeneral Options: ", '#95a5a6']

  opts.on('-d DIR', '--dir DIR', String, 'Upload files in DIR (Defaults to the current directory)') do |d|
    options[:directory] = d unless d.nil?
  end

  opts.on('-v', '--version', 'Show the rise version and exit') do
    puts "Rise version: #{Paint[Rise::Constants::VERSION, '#2ecc71']}"
    exit 0
  end

  opts.on('--verbose', 'Run verbosely') do |v|
    ENV['RISE_VERBOSE'] = 'yes'
  end

  opts.on('-i', '--ignore FILES', Array, 'Ignore the given files in the upload. These will be ignored if there is a .riseignore file.') do |a|
    options[:ignored_files] = a unless a.nil?
    puts "Reminder: You can add the files to .riseignore instead of using the -i flag"
  end

  opts.on('-o', '--open', 'Open the deployment in a browser if possible') do
    options[:open] = true
  end

  opts.on('-u', '--update', 'Check if rise has a newer version and install it') do
    Rise::Util.check_for_update!
    exit 0
  end

  opts.on('-h', '--help', 'Show this help message') do
    puts opts
    puts Rise::Text::TASKS_HELP
    exit
  end

  opts.separator Paint["\nTasks: ", '#95a5a6']

end.parse!(ARGV)

# Ladies and gentlemen, this is what
# happens when optparse doesn't have
# good subcommand/task syntax support
while opt = ARGV.shift do
  case opt
  when 'init'
    Rise::Util.setup(false)
    exit 0
  when 'update'
    Rise::Util.check_for_update!
    exit 0
  end
end


if Rise::Util.first_run?
  Rise::Util.setup
  exit 0
end
Rise::Util.check_for_update!
dir = options[:directory] || Dir.pwd
ignored = nil
result_url = ''

begin
  ignored = File.read(File.join(dir, '.riseignore')).split("\n").map { |a| a.gsub!(' ', '')}
rescue Errno::ENOENT
  ignored = options[:ignored_files]
end

uploader = Rise::Transport::Uploader.new(dir, ignored)

if uploader.total_files_size > 52428800
  puts Paint["Max file size reached (#{uploader.total_files_size} > 50MB)", '#FF0000']
  exit 0
end

puts Paint['Thanks for using Rise! Your local source for serverless deployment!', '#95a5a6']

Whirly.start(spinner: 'dots', status: "Uploading files (#{uploader.total_files} total files)") do
  beginning_time = Time.now
  result_url = uploader.upload!(options[:verbose]) # Do the file upload

  Whirly.status = "Done!\n"
  Clipboard.copy(result_url)
  print Paint["Your url is: #{result_url} (copied to clipboard) ", :bold]
  puts Paint["[#{((Time.now - beginning_time)).round(2)}s]", '#95a5a6']

  puts Paint['Deployment successful!', '#3498db']

  Rise::Util.open_deployment_in_browser(result_url) if options[:open]
end