#!/usr/bin/env ruby
#-*-ruby-*-

# Copyright 2012 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.

$: << File.dirname(__FILE__)
require 'aws/elastic_beanstalk_config'

def prompt(message,field,default=nil)
  default = @config.send(field) || default
  message << " [default to #{default}]" unless default.nil?
  STDOUT.write "#{message}: "
  STDOUT.flush
  value = STDIN.gets.to_s.strip
  if value.empty?
    @changed_settings[field] = default unless default.nil?
  else
    @changed_settings[field] = value
  end
end

if __FILE__ == $0
  @config = AWS::ElasticBeanstalkConfig.new(Dir.pwd)
  @changed_settings = {}

  if @config.should_write_credential_file?
    prompt("AWS Access Key", :access_key_id)
    prompt("AWS Secret Key", :secret_access_key)
  end

  prompt("AWS Region", :region, "us-east-1")

  default_host = @config.dev_tools_endpoint_default(@changed_settings[:region])
  if default_host.nil? # means the region is unrecognized
    prompt("AWS Host",
           :dev_tools_endpoint,
           @config.dev_tools_endpoint_default("us-east-1"))
  else
    # always set the endpoint
    @changed_settings[:dev_tools_endpoint] = default_host
  end

  prompt("AWS Elastic Beanstalk Application", :application_name)
  prompt("AWS Elastic Beanstalk Environment", :environment_name)
  @config.write_settings(@changed_settings)

  # everything after this is just messaging about the state of the
  # credentials file.

  if @config.credential_file_configured? || @config.credential_file_exists?
    puts <<END

NOTE: "git aws.push" will use the credentials found in the following file:

#{@config.credential_file_path}

END
    if @config.access_key_id && @config.secret_access_key
      puts <<END
You can supply different credentials by editing that file or editing
.elasticbeanstalk/config to reference a different file.  The file should
have the following format:
END
    elsif !@config.credential_file_exists?
      puts <<END
This file doesn't exist.  You can supply credentials by creating the file
or editing .elasticbeanstalk/config to reference a different file.  The file
should have the following format:
END
    elsif !@config.credential_file_readable?
      puts <<END
This file is not readable.  You can fix the problem by granting read
permissions to the file or by editing .elasticbeanstalk/config to reference
a different file.  The file should have the following format:
END
    else
      # it doesn't have both AKID and secret, but it exists and is
      # readable.
      puts <<END
This file doesn't contain a full set of credentials.  You can fix the
problem by editing the file or by editing .elasticbeanstalk/config to
reference a different file.  The file should have the following format:
END
    end
    puts <<END

AWSAccessKeyId=your key
AWSSecretKey=your secret

END
  elsif ENV['HOME'].nil?
    puts <<END
NOTE: No credentials are configured. If you want to use a file to supply
credentials for "git aws.push" you can create a file with the following
format:

AWSAccessKeyId=your key
AWSSecretKey=your secret

You can use this file by adding the following line to
.elasticbeanstalk/config:

AwsCredentialFile=path_to_your_file
END
  elsif File.exists?(@config.default_credential_file_path)
    puts <<END

NOTE: No credentials are configured.  By default credentials are stored in
the following location:

#{@config.default_credential_file_path}

If you want to use this file to supply credentials for "git aws.push" you
can add the following line to .elasticbeanstalk/config:

AwsCredentialFile=#{@config.default_credential_file_path}

The credential file should have the following format:

AWSAccessKeyId=your key
AWSSecretKey=your secret
END
  else
    # if we get here, the user has been prompted for credentials and
    # they've been written to the default location
    # (~/.elasticbeanstalk/aws_credential_file)
  end
end
