namespace :sentinel do

namespace :config do

  desc "Adds adapter configs on database.yml file and generate the structure for the use."

  task :database, [:dbms, :encoding, :username] => [:environment] do |t, params|

    database_conf = "config/database.yml"
    br3 = "\n\n\n"
    br = "\n"

    test = {
      :sentinel_test => {
        :adapter => params[:dbms],
        :encoding => params[:encoding],
        :host => "localhost",
        :database => "sentinel_test",
        :username => params[:username],
      }
    }

    dev = {
      :sentinel_development => {
        :adapter => params[:dbms],
        :encoding => params[:encoding],
        :host => "localhost",
        :database => "sentinel_development",
        :username => params[:username]
      }
    }

    production = {
      :sentinel_production => {
        :adapter => params[:dbms],
        :encoding => params[:encoding],
        :host => "localhost",
        :database => "sentinel_production",
        :username => params[:username],
        :password => nil
      }
    }

    File.open(database_conf, 'a+') do |f|

      if f
        f.write br3
        # remova 4 caracteres do inicio, e imprima até -1(todos os caracteres restante)
        f.write test.deep_stringify_keys.to_yaml[4..-1]
        f.write br
        f.write dev.deep_stringify_keys.to_yaml[4..-1]
        f.write br
        f.write production.deep_stringify_keys.to_yaml[4..-1]
        puts "config/database.yml configurado com sucesso!"
      else
        puts "Um erro foi encontrado. Tente novamente, ou contate o desenvolvedor da gem."
      end

    end

  end

end

end