#!/bin/bash
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source /usr/share/yunohost/helpers
#=================================================
# INITIALIZE AND STORE SETTINGS
#=================================================
admin_mail=$(ynh_user_get_info --username="$admin" --key="mail")
ynh_app_setting_set --app="$app" --key="password" --value="$password"
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
ynh_script_progression --message="Setting up source files..." --weight=9
# Download, check integrity, uncompress and patch the source from app.src
ynh_setup_source --dest_dir="$install_dir/app"
mkdir -p "$install_dir/app/sites/default/files"
mkdir -p "$install_dir/app/sites/default/files/tmp"
chmod -R o-rwx "$install_dir"
chown -R "$app:www-data" "$install_dir"
chmod 2775 "$install_dir/app/sites/default/files"
chmod -R o-rwx "$data_dir"
chown -R "$app:www-data" "$data_dir"
#=================================================
# CREATE DRUSH ALIAS
#=================================================
ynh_script_progression --message="Creating Drush alias..." --weight=2
mkdir -p "$install_dir/.drush"
drush_aliasconfig="$install_dir/.drush/$app.aliases.drushrc.php"
ynh_add_config --template="yoursite.aliases.drushrc.php" --destination="$drush_aliasconfig"
chmod 400 "$drush_aliasconfig"
chown "$app:$app" "$drush_aliasconfig"
#=================================================
# INSTALL COMPOSER
#=================================================
ynh_script_progression --message="Installing Composer..." --weight=31
mkdir -p "$install_dir/.composer"
ynh_add_config --template="composer.json" --destination="$install_dir/.composer/composer.json"
ynh_install_composer --phpversion="$phpversion" --workdir="$install_dir/.composer"
export PATH="$install_dir/.composer/vendor/bin:$PATH"
#=================================================
# ADD A CONFIGURATION
#=================================================
ynh_script_progression --message="Adding $app's configuration file..." --weight=1
ynh_add_config --template="default.settings.php" --destination="$install_dir/app/sites/default/settings.php"
chmod 600 "$install_dir/app/sites/default/settings.php"
chown "$app:$app" "$install_dir/app/sites/default/settings.php"
#=================================================
# INSTALLING FRAMAFORMS WITH DRUSH
#=================================================
ynh_script_progression --message="Installing database..." --weight=19
# Chown from composer/drush install
chown -R "$app:www-data" "$install_dir"
_ynh_exec_with_drush_php drush "@$app" site-install framaforms_org \
install_configure_form.site_contact_url="https://forum.yunohost.org/t/framaforms-create-polls-using-drag-and-drop/8208" \
install_configure_form.site_default_country=FR \
-y --locale="$language" --account-name="admin" --account-pass="$password" --site-name="Framaforms" --site-mail="$admin_mail" 2>&1
_ynh_exec_with_drush_php drush "@$app" variable-set update_notify_emails "$admin_mail"
_ynh_exec_with_drush_php drush "@$app" variable-set file_private_path "/home/yunohost.app/$app/data" 2>&1
_ynh_exec_with_drush_php drush "@$app" pm-enable framaforms_feature -y --resolve-dependencies 2>&1
_ynh_exec_with_drush_php drush "@$app" php-eval "module_load_include('inc', 'framaforms', 'includes/framaforms.pages');create_all_pages();" 2>&1 || true
#=================================================
# IMPORTING LANGUAGE PACK
#=================================================
ynh_script_progression --message="Importing language pack..." --weight=5
_ynh_exec_with_drush_php drush "@$app" pm-download -y drush_language 2>&1
_ynh_exec_with_drush_php drush "@$app" pm-download -y l10n_update 2>&1
_ynh_exec_with_drush_php drush "@$app" pm-enable -y l10n_update 2>&1
_ynh_exec_with_drush_php drush "@$app" language-add "$language" -y 2>&1
_ynh_exec_with_drush_php drush "@$app" language-default "$language" -y 2>&1
_ynh_exec_with_drush_php drush "@$app" cache-clear drush -y 2>&1
_ynh_exec_with_drush_php drush "@$app" l10n-update-refresh -y 2>&1
_ynh_exec_with_drush_php drush "@$app" l10n-update -y 2>&1
# We don't upgrade module to avoid to erase framaforms change
# in drupal core and modules code
# see https://framagit.org/framasoft/framaforms/-/wikis/modifications
# _ynh_exec_with_drush_php drush "@$app" pm-update -y
#=================================================
# REMOVING BRANDING AND CHANGING DEFAULT SETTINGS
#=================================================
ynh_script_progression --message="Removing branding and change default settings..." --weight=1
_ynh_exec_with_drush_php drush "@$app" vset error_level 0
_ynh_exec_with_drush_php drush "@$app" vset framaforms_notification_period_value "$expiration"
_ynh_exec_with_drush_php drush "@$app" vset framaforms_deletion_period_value "$deletion"
# Remove framaforms footer
_ynh_exec_with_drush_php drush "@$app" sql-query "UPDATE block SET region='-1', status=0 WHERE delta='framaforms_footer' AND region='footer'"
#=================================================
# CONFIGURING LDAP
#=================================================
ynh_script_progression --message="Configuring LDAP authentication..." --weight=1
_ynh_exec_with_drush_php drush "@$app" pm-download ldap
_ynh_exec_with_drush_php drush "@$app" pm-enable -y ldap_servers ldap_user ldap_authentication ldap_authorization ldap_authorization_drupal_role
# Generated with sudo -u postgres pg_dump -a -t ldap_servers --inserts framaforms
_ynh_exec_with_drush_php drush "@$app" sql-query "INSERT INTO public.ldap_servers VALUES ('localhost', 1, 'localhost', 1, 'default', 'ldap://127.0.0.1', 389, 0, 0, 3, '', NULL, 'a:2:{i:0;s:27:\"ou=users,dc=yunohost,dc=org\";i:1;s:32:\"ou=permission,dc=yunohost,dc=org\";}', 'uid', 'uid', 'mail', '', '', '', 0, 'cn=%username,ou=users,dc=yunohost,dc=org', '', '', '', 0, 'permissionynh', 0, 1, 'permission', 'memberuid', 'dn', 0, '', '$app.main', '', 0, 1000, 0);"
_ynh_exec_with_drush_php drush "@$app" sql-query "INSERT INTO public.ldap_authorization VALUES (1, 'localhost', 'drupal_role', 'ldap_authorization_drupal_role', 1, 1, 0, 'a:2:{i:0;a:6:{s:12:\"user_entered\";s:24:\"utilisateur authentifié\";s:4:\"from\";s:51:\"cn=framaforms.main,ou=permission,dc=yunohost,dc=org\";s:10:\"normalized\";s:24:\"utilisateur authentifié\";s:10:\"simplified\";s:24:\"utilisateur authentifié\";s:5:\"valid\";b:0;s:13:\"error_message\";s:112:\"Role utilisateur authentifié_name does not exist and role creation is not enabled.\";}i:1;a:6:{s:12:\"user_entered\";s:13:\"administrator\";s:4:\"from\";s:52:\"cn=framaforms.admin,ou=permission,dc=yunohost,dc=org\";s:10:\"normalized\";s:13:\"administrator\";s:10:\"simplified\";s:13:\"administrator\";s:5:\"valid\";b:0;s:13:\"error_message\";s:101:\"Role administrator_name does not exist and role creation is not enabled.\";}}', 1, 0, 1, 1, 1, 1);"
_ynh_exec_with_drush_php drush "@$app" variable-set --format=yaml ldap_authentication_conf - < ../conf/ldap.conf
#=================================================
# SYSTEM CONFIGURATION
#=================================================
ynh_script_progression --message="Adding system configurations related to $app..." --weight=1
# Create a dedicated PHP-FPM config
ynh_add_fpm_config --usage=low --footprint=low
# Create a dedicated NGINX config
ynh_add_nginx_config
ynh_add_config --template="cron" --destination="/etc/cron.d/$app"
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Installation of $app completed" --last