#!/bin/bash

#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================

source _common.sh
source /usr/share/yunohost/helpers

#=================================================
# INITIALIZE AND STORE SETTINGS
#=================================================

path="/"
random_key=$(ynh_string_random --length=64)
ynh_app_setting_set --app="$app" --key=random_key --value="$random_key"
signing_salt=$(ynh_string_random --length=8)
ynh_app_setting_set --app="$app" --key=signing_salt --value="$signing_salt"
admin_email=$(ynh_user_get_info --username="$admin" --key="mail")
ynh_app_setting_set --app="$app" --key=admin_email --value="$admin_email"

## Bypass package_checker name not compatible with pleroma
if [ "${PACKAGE_CHECK_EXEC:-0}" -eq 1 ]; then
    admin="test"
fi

#=================================================
# APP "BUILD" (DEPLOYING SOURCES, VENV, COMPILING ETC)
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
ynh_script_progression --message="Setting up source files..." --weight=1

# Download, check integrity, uncompress and patch the source from manifest.toml
ynh_setup_source --dest_dir="$install_dir/live"

chown -R "$app:www-data" "$install_dir"

#=================================================
# UPDATE THE POSTGRESQL DATABASE
#=================================================
ynh_script_progression --message="Configuring the PostgreSQL database..." --weight=1

ynh_psql_execute_as_root --sql="CREATE EXTENSION IF NOT EXISTS unaccent;" --database="$db_name"
ynh_psql_execute_as_root --sql="CREATE EXTENSION IF NOT EXISTS pg_trgm;" --database="$db_name"
ynh_psql_execute_as_root --sql="CREATE EXTENSION IF NOT EXISTS citext;" --database="$db_name"
ynh_psql_execute_as_root --sql="CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\";" --database="$db_name"

#=================================================
# SYSTEM CONFIGURATION
#=================================================
ynh_script_progression --message="Adding system configurations related to $app..." --weight=1

if [ "$cache" -eq 1 ]; then
    ynh_add_config --template="../conf/cache.conf" --destination="/etc/nginx/conf.d/$app-cache.conf"
    cat ../conf/media.conf >> ../conf/nginx.conf
fi
# Create a dedicated NGINX config
ynh_add_nginx_config

# Create a dedicated systemd config
ynh_add_systemd_config
yunohost service add "$app" --description="$app daemon for Pleroma"

#=================================================
# MAKE SETUP
#=================================================
ynh_script_progression --message="Making setup..." --weight=1

config="/etc/$app/config.exs"
mkdir -p "/etc/$app"
chown "$app:$app" "/etc/$app"

# Generate instance
ynh_exec_warn_less ynh_exec_as "$app" -i \
"$install_dir/live/bin/pleroma_ctl" instance gen \
    --force \
    --output "$config" \
    --output-psql /tmp/setup_db.psql \
    --domain "$domain" \
    --instance-name "$instance_name" \
    --admin-email "$admin_email" \
    --notify-email "$admin_email" \
    --dbhost localhost \
    --dbname "$db_name" \
    --dbuser "$db_user" \
    --dbpass "$db_pwd" \
    --rum N \
    --indexable Y \
    --db-configurable Y \
    --uploads-dir "$data_dir/uploads" \
    --static-dir "$data_dir/static" \
    --listen-ip 127.0.0.1 \
    --listen-port "$port" \
    --strip-uploads-location Y \
    --read-uploads-description Y \
    --anonymize-uploads Y \
    --dedupe-uploads Y

cat "../conf/ldap.exs" >> "$config"

ynh_replace_string --target_file="$config" \
    --match_string="config :pleroma, configurable_from_database: false" \
    --replace_string="config :pleroma, configurable_from_database: true"

ynh_replace_string --target_file="$config" \
    --match_string="registrations_open: true" \
    --replace_string="registrations_open: $(boolstr "$registration")"

ynh_exec_warn_less ynh_exec_as "$app" -i "$install_dir/live/bin/pleroma_ctl" migrate
ynh_systemd_action --service_name="$app" --action="start" --log_path=systemd --line_match="Access Pleroma.Web.Endpoint"

# Add user
ynh_exec_warn_less ynh_exec_as "$app" -i "$install_dir/live/bin/pleroma_ctl" user new "$admin" "$admin_email" --password "$password" --moderator --admin -y
ynh_systemd_action --service_name="$app" --action="stop" --log_path=systemd

# Calculate and store the config file checksum into the app settings
ynh_store_file_checksum --file="$config"

chmod 400 "$config"
chown "$app:$app" "$config"

#=================================================
# START SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Starting a systemd service..." --weight=1

# Start a systemd service
ynh_systemd_action --service_name="$app" --action="start" --log_path=systemd --line_match="Access Pleroma.Web.Endpoint"

#=================================================
# POST INSTALL
#=================================================

# Correct path to 'static dir' in DB
# This must be done when Pleroma is running (i.e. after install and start)
ynh_exec_warn_less ynh_exec_as "$app" -i "$install_dir/live/bin/pleroma_ctl" config migrate_to_db

#=================================================
# END OF SCRIPT
#=================================================

ynh_script_progression --message="Installation of $app completed" --last
