namespace :typo3cms do

desc "Generate a basic configuration file"
task :generate_config do
  set :typo3_cms_database_name, ask('Enter the database name:', '')
  set :typo3_cms_database_username, ask('Enter the database username:', '')
  set :typo3_cms_database_password, ask('Enter the database password:', '', echo: false)
  set :typo3_cms_database_host, ask('Enter the database host:', 'localhost')
  set :typo3_cms_database_port, ask('Enter the database port:', '3306')

  set :typo3_cms_install_password, ask('Enter the install tool password:', '', echo: false)

  template = <<-EOF

<?php $GLOBALS['DB’] = '<%= fetch(:typo3_cms_database_name) %>'; $GLOBALS['DB’] = '<%= fetch(:typo3_cms_database_username) %>'; $GLOBALS['DB’] = '<%= fetch(:typo3_cms_database_password) %>'; $GLOBALS['DB’] = '<%= fetch(:typo3_cms_database_host) %>'; $GLOBALS['BE’] = '<%= Digest::MD5.hexdigest(fetch(:typo3_cms_install_password)) %>'; $GLOBALS['SYS’] = '<%= SecureRandom.hex 48 %>'; <%= fetch(:typo3_cms_configuration_additional, '') %> ?> EOF

  config = ERB.new(template)
  io = StringIO.new(config.result(binding))

  on release_roles(fetch(:typo3_cms_roles)) do
    invoke "deploy:check:make_linked_dirs"
    upload! io, File.join(shared_path, fetch(:typo3_cms_configuration_file))
  end
end

end