#
# Copyright:: 2013, Adrien Waksberg
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License 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 'optparse'
require 'mpw/config'
require 'mpw/cli'

# --------------------------------------------------------- #
# Options
# --------------------------------------------------------- #

values                = {}
options               = {}
options[:text_editor] = true

OptionParser.new do |opts|
  opts.banner = "#{I18n.t('option.usage')}: mpw add [options]"

  opts.on('-c', '--config PATH', I18n.t('option.config')) do |config|
    options[:config] = config
  end

  opts.on('-C', '--comment COMMENT', I18n.t('option.comment')) do |comment|
    values[:comment] = comment
  end

  opts.on('-G', '--group NAME', I18n.t('option.new_group')) do |group|
    values[:group] = group
  end

  opts.on('-h', '--help', I18n.t('option.help')) do
    puts opts
    exit 0
  end

  opts.on('-o', '--otp-code CODE', I18n.t('option.otp_code')) do |otp|
    values[:otp_key] = otp
  end

  opts.on('-r', '--random', I18n.t('option.random_password')) do
    options[:password] = true
  end

  opts.on('-t', '--text-editor', I18n.t('option.text_editor')) do
    options[:text_editor] = true
  end

  opts.on('-u', '--url URL', I18n.t('option.url')) do |url|
    values[:url] = url
  end

  opts.on('-U', '--user USER', I18n.t('option.user')) do |user|
    values[:user] = user
  end

  opts.on('-w', '--wallet NAME', I18n.t('option.wallet')) do |wallet|
    options[:wallet] = wallet
  end
end.parse!

config = MPW::Config.new(options[:config])
cli    = MPW::Cli.new(config)

cli.load_config
cli.get_wallet(options[:wallet])
cli.decrypt
cli.add(options[:password], options[:text_editor], values)