+extensions_directory = "/srv/supporting.openstreetmap.org/wp-content/plugins/civicrm-extensions"
+
+directory extensions_directory do
+ owner "wordpress"
+ group "wordpress"
+ mode "755"
+end
+
+node[:civicrm][:extensions].each_value do |details|
+ if details[:repository]
+ git "#{extensions_directory}/#{details[:name]}" do
+ action :sync
+ repository details[:repository]
+ revision details[:revision]
+ user "wordpress"
+ group "wordpress"
+ end
+ elsif details[:zip]
+ remote_file "#{cache_dir}/#{details[:name]}.zip" do
+ source details[:zip]
+ owner "root"
+ group "root"
+ mode "644"
+ backup false
+ end
+
+ archive_file "#{cache_dir}/#{details[:name]}.zip" do
+ action :nothing
+ destination "#{extensions_directory}/#{details[:name]}"
+ strip_components 1
+ owner "wordpress"
+ group "wordpress"
+ overwrite true
+ subscribes :extract, "remote_file[#{cache_dir}/#{details[:name]}.zip]", :immediately
+ end
+ end
+end
+
+settings = edit_file "#{civicrm_directory}/civicrm/templates/CRM/common/civicrm.settings.php.template" do |line|
+ line.gsub!(/%%cms%%/, "WordPress")
+ line.gsub!(/%%CMSdbUser%%/, "civicrm")
+ line.gsub!(/%%CMSdbPass%%/, database_password)
+ line.gsub!(/%%CMSdbHost%%/, "localhost")
+ line.gsub!(/%%CMSdbName%%/, "civicrm")
+ line.gsub!(/%%dbUser%%/, "civicrm")
+ line.gsub!(/%%dbPass%%/, database_password)
+ line.gsub!(/%%dbHost%%/, "localhost")
+ line.gsub!(/%%dbName%%/, "civicrm")
+ line.gsub!(/%%crmRoot%%/, "#{civicrm_directory}/civicrm/")
+ line.gsub!(/%%templateCompileDir%%/, "/srv/supporting.openstreetmap.org/wp-content/uploads/civicrm/templates_c/")
+ line.gsub!(/%%baseURL%%/, "http://supporting.openstreetmap.org/")
+ line.gsub!(/%%siteKey%%/, site_key)
+ line.gsub!(/%%credKeys%%/, cred_keys)
+ line.gsub!(/%%signKeys%%/, sign_keys)
+ line.gsub!(%r{// *define\('CIVICRM_CMSDIR', '/path/to/install/root/'\);}, "define('CIVICRM_CMSDIR', '/srv/supporting.openstreetmap.org');")
+ # Don't recompile smarty templates on every call https://docs.civicrm.org/sysadmin/en/latest/setup/optimizations/#disable-compile-check
+ line.gsub!(%r{// define\('CIVICRM_TEMPLATE_COMPILE_CHECK', FALSE\);},"define('CIVICRM_TEMPLATE_COMPILE_CHECK', FALSE);")
+ # Upgrade smarty 2 to smarty 4
+ line.gsub!(%r{if \(strpos\(CIVICRM_UF_BASEURL, 'localhost'\) !== FALSE \|\| strpos\(CIVICRM_UF_BASEURL, 'demo\.civicrm\.org'\) !== FALSE\) \{}, "if (true) {")
+
+ line
+end
+
+file "#{civicrm_directory}/civicrm.settings.php" do
+ owner "wordpress"
+ group "wordpress"
+ mode "644"
+ content settings
+end
+
+systemd_service "osmf-crm-jobs" do
+ description "Run CRM jobs"
+ exec_start "/usr/bin/php #{civicrm_directory}/civicrm/bin/cli.php -s supporting.openstreetmap.org -u batch -p \"#{passwords['batch']}\" -e Job -a execute"
+ user "www-data"
+ sandbox :enable_network => true
+ memory_deny_write_execute false
+ restrict_address_families "AF_UNIX"
+ read_write_paths "/srv/supporting.openstreetmap.org/wp-content/uploads/civicrm"
+end
+
+systemd_timer "osmf-crm-jobs" do
+ description "Run CRM jobs"
+ on_boot_sec "15m"
+ on_unit_inactive_sec "15m"
+end
+
+service "osmf-crm-jobs.timer" do
+ action [:enable, :start]