# vim: ft=ruby:sts=2:expandtab

namespace :typo3 do

namespace :homestead do

  desc "init site dirs from scratch (new installs)"
  task "init_site_dirs" do
    invoke 'git:check_branch'

    on roles(:all) do
      ## PURGE IF NEEDED
      execute "sudo rm -Rf #{fetch(:deploy_to)}/shared/fileadmin #{fetch(:deploy_to)}/shared/typo3temp #{fetch(:deploy_to)}/shared/uploads"

      ## CREATE
      execute "sudo mkdir -p #{fetch(:deploy_to)}/shared/fileadmin #{fetch(:deploy_to)}/shared/typo3temp #{fetch(:deploy_to)}/shared/uploads"

      ['fileadmin','typo3temp','uploads'].each do|dir|
        begin
          execute "sudo chown -Rfv vagrant.vagrant #{fetch(:deploy_to)}/shared/#{dir}"
        rescue
          print 'could not CHOWN #{dir}. Propably already owned by vagrant.vagrant'
        end
      end

    end
  end

  desc "init site database from scratch (new installs) (danger: drops old)"
  task "init_site_database" do
    invoke 'git:check_branch'

    on roles(:all) do
      execute "sudo mysql -e 'DROP DATABASE IF EXISTS #{fetch(:dbname)}'"
      execute "sudo mysql -e 'CREATE DATABASE #{fetch(:dbname)} CHARACTER SET utf8 COLLATE utf8_general_ci;'"
    end
  end

  desc "setup site (existing installs)"
  task "setup_site" do
    invoke 'typo3:homestead:init_site_dirs'
    invoke 'typo3:homestead:init_site_database'

    invoke 'deploy:fixknownhosts'
    invoke 'typo3:content:sync_db_from_production'
    invoke 'typo3:content:flush_cache_in_db'
    invoke 'typo3:helper:update_localconf'
    invoke 'typo3:helper:write_tsconstants'
    invoke 'typo3:helper:restart_webserver'
    invoke 'typo3:content:sync_files_from_production'
  end

end

end