#!/bin/bash

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

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

#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
ynh_script_progression --message="Setting up source files..." --weight=1

ynh_setup_source --dest_dir="$install_dir"

chmod 750 "$install_dir"
chmod -R o-rwx "$install_dir"
chown -R $app:$app "$install_dir"

#=================================================
# NGINX CONFIGURATION
#=================================================
ynh_script_progression --message="Configuring NGINX web server..." --weight=1

# Create a dedicated NGINX config
ynh_add_nginx_config

#=================================================
# SPECIFIC SETUP
#=================================================
# INSTALL PYTHON DEPENDENCIES
#=================================================
ynh_script_progression --message="Installing Python dependencies..."

pushd $install_dir
	python3 -m venv venv
	chown -R "$app:" "$install_dir"
(
	source "$install_dir/venv/bin/activate"
	ynh_exec_as $app $install_dir/venv/bin/pip3 install --upgrade pip setuptools wheel
	ynh_exec_as $app $install_dir/venv/bin/pip3 install -r "$install_dir/requirements.txt"

	# Installing NLTK data
	mkdir -p "$install_dir/nltk_data"
	chown -R "$app:" "$install_dir/nltk_data"
	ynh_exec_warn_less ynh_exec_as $app $install_dir/venv/bin/python3 -m nltk.downloader -d "$install_dir/nltk_data" snowball_data
	ynh_exec_warn_less ynh_exec_as $app $install_dir/venv/bin/python3 -m nltk.downloader -d "$install_dir/nltk_data" stopwords
	ynh_exec_warn_less ynh_exec_as $app $install_dir/venv/bin/python3 -m nltk.downloader -d "$install_dir/nltk_data" punkt

	deactivate
)
popd

#=================================================
# CREATE DATA DIRECTORY
#=================================================
ynh_script_progression --message="Setting permissions for the data directory..." --weight=1

chmod 750 "$data_dir"
chmod -R o-rwx "$data_dir"
chown -R $app:$app "$data_dir"

#=================================================
# ADD A CONFIGURATION
#=================================================
ynh_script_progression --message="Adding a configuration file..." --weight=1

redis_db=$(ynh_redis_get_free_db)
ynh_app_setting_set --app=$app --key=redis_db --value=$redis_db

paperless_secret_key=$(ynh_string_random)
ynh_app_setting_set --app=$app --key=paperless_secret_key  --value=$paperless_secret_key

ocr_language="eng"
ynh_app_setting_set --app=$app --key=ocr_language  --value=$ocr_language

ynh_add_config --template="paperless.conf.example" --destination="$install_dir/paperless.conf"

chmod 400 "$install_dir/paperless.conf"
chown $app:$app "$install_dir/paperless.conf"

#=================================================
# SETUP THE DATABASE
#=================================================
ynh_script_progression --message="Setting up the database..." --weight=1

pushd "$install_dir/src"
(
	source "$install_dir/venv/bin/activate"
	ynh_exec_as $app $install_dir/venv/bin/python manage.py migrate
	deactivate
)
popd

#=================================================
# CREATE THE ADMIN USER
#=================================================
ynh_script_progression --message="Creating the admin user..." --weight=1

pushd "$install_dir/src"
(
	source "$install_dir/venv/bin/activate"
	email=$(ynh_user_get_info $admin 'mail')
	ynh_exec_as $app env "DJANGO_SUPERUSER_PASSWORD=$admin_pw" $install_dir/venv/bin/python3 manage.py createsuperuser --noinput --username "$admin" --email "$email"
	deactivate
)
popd

#=================================================
# SETUP SYSTEMD
#=================================================
ynh_script_progression --message="Configuring a systemd service..." --weight=1

# Create a dedicated systemd config
ynh_add_systemd_config --service="$app" --template="systemd.service"
ynh_add_systemd_config --service="$app-consumer" --template="systemd-consumer.service"
ynh_add_systemd_config --service="$app-scheduler" --template="systemd-scheduler.service"
ynh_add_systemd_config --service="$app-task-queue" --template="systemd-task-queue.service"

#=================================================
# GENERIC FINALIZATION
#=================================================
# SETUP LOGROTATE
#=================================================
ynh_script_progression --message="Configuring log rotation..." --weight=1

# Use logrotate to manage application logfile(s)
ynh_use_logrotate

#=================================================
# INTEGRATE SERVICE IN YUNOHOST
#=================================================
ynh_script_progression --message="Integrating service in YunoHost..." --weight=1

yunohost service add "$app" --log="/var/log/$app/$app.log"
yunohost service add "$app-consumer" --log="/var/log/$app/$app-consumer.log"
yunohost service add "$app-scheduler" --log="/var/log/$app/$app-scheduler.log"
yunohost service add "$app-task-queue" --log="/var/log/$app/$app-task-queue.log"

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

ynh_systemd_action --service_name="$app" --action="start" --log_path="/var/log/$app/$app.log"
ynh_systemd_action --service_name="$app-consumer" --action="start" --log_path="/var/log/$app/$app-consumer.log"
ynh_systemd_action --service_name="$app-scheduler" --action="start" --log_path="/var/log/$app/$app-scheduler.log"
ynh_systemd_action --service_name="$app-task-queue" --action="start" --log_path="/var/log/$app/$app-task-queue.log"

#=================================================
# SETUP FAIL2BAN
#=================================================
#ynh_script_progression --message="Configuring Fail2Ban..." --weight=1

# Create a dedicated Fail2Ban config
#ynh_add_fail2ban_config --logpath="/var/log/nginx/${domain}-error.log" --failregex="Regex to match into the log for a failed login"
# FIXME fail2ban
# ynh_add_fail2ban_config --logpath="/var/log/nginx/${domain}-access.log" --failregex="<HOST>.* \"POST /api/v1/token/ HTTP/1.1\" 400 68.*$" --max_retry=5

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

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