#!/bin/bash

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

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


#=================================================
# STANDARD RESTORATION STEPS
#=================================================
# RESTORE THE NGINX CONFIGURATION
#=================================================
ynh_script_progression --message="Restoring nginx configuration..." --weight=1
ynh_restore_file "/etc/nginx/conf.d/$domain.d/$app.conf"

#=================================================
# RESTORE THE APP MAIN DIR
#=================================================
ynh_script_progression --message="Restoring the app main directory..." --weight=1
ynh_restore_file --origin_path="$install_dir"

#=================================================
# RESTORE THE KEPUBIFY BINARIES
#=================================================
ynh_script_progression --message="Restoring Kepubify..." --weight=1
ynh_restore_file --origin_path="/opt/kepubify/$app"


#=================================================
# RESTORE USER RIGHTS
#=================================================
chown -R $app: $install_dir
chmod 740 $install_dir

#Use venv to install pip requirements - Inspired from https://github.com/YunoHost-Apps/pyinventory_ynh/blob/master/scripts/install
ynh_script_progression --message="Reinstalling pip requirements..." --weight=70
# Always recreate everything fresh with current python version
# When reinstalling on a new yunohost, this is required 
if [ -d "${install_dir}/venv" ] ; then
	ynh_secure_remove "${install_dir}/venv"
fi

# Skip pip because of: https://github.com/YunoHost/issues/issues/1960
python3 -m venv --without-pip "${install_dir}/venv"
chown -R "$app:" "$install_dir"

#run source in a 'sub shell'
(
	set +o nounset
	source "${install_dir}/venv/bin/activate"
	set -o nounset
	ynh_exec_as $app $install_dir/venv/bin/python3 -m ensurepip
	ynh_exec_as $app $install_dir/venv/bin/pip3 install --upgrade wheel pip setuptools
	ynh_exec_as $app $install_dir/venv/bin/pip3 install --no-cache-dir --upgrade -r "$install_dir/requirements.txt"
	ynh_exec_as $app $install_dir/venv/bin/pip3 install --no-cache-dir --upgrade -r "$install_dir/optional-requirements.txt"
)

#=================================================
# RESTORE SYSTEMD
#=================================================
ynh_script_progression --message="Restoring the systemd configuration..." --weight=1

ynh_restore_file --origin_path="/etc/systemd/system/$app.service"
systemctl enable $app.service --quiet

#=================================================
# INTEGRATE SERVICE IN YUNOHOST
#=================================================

yunohost service add $app --description="Browse eBook in the web" --log="$log_file"

#=================================================
# RESTORE THE LOGROTATE CONFIGURATION
#=================================================

ynh_restore_file --origin_path="/etc/logrotate.d/$app"

#=================================================
# RESTORE THE LOG FILE DIRECTORY
#=================================================
mkdir -p /var/log/$app
chown -R $app:$app /var/log/$app

#=================================================
# RESTORE THE DATA DIRECTORY
#=================================================

ynh_script_progression --message="Restoring data directory if required..." --weight=2
ynh_restore_file --origin_path="$calibre_dir" --not_mandatory

#=================================================
# RESTORE THE MULTIMEDIA DIR IF NOT EXISTING
#=================================================
ynh_script_progression --message="Restoring the multimedia directory..." --weight=5
ynh_multimedia_build_main_dir
ynh_multimedia_addaccess $app

#=================================================
# INITIALIZE DATA IF NOT EXISTING
#=================================================

if [ ! -e "$calibre_dir" ]; then
	ynh_print_info "Create calibre library folder $calibre_dir"
	mkdir -p $calibre_dir
	chown -R $app:$app $calibre_dir
fi
#Check if metadata.db file exists. If not create it (empty library)
if [ ! -e "$calibre_dir"/metadata.db ]; then
	cp -a ../settings/conf/metadata.db.empty $calibre_dir/metadata.db
	chown $app:$app $calibre_dir/*
fi

#===================================================
# SPECIFIC SETUP
#===================================================
#Update Imagick policy as per https://github.com/janeczku/calibre-web/wiki/FAQ#what-to-do-if-cover-pictures-are-not-extracted-from-pdf-files
ynh_replace_string --match_string="<policy domain="coder" rights="none" pattern="PDF" />" \
		--replace_string="<policy domain="coder" rights="read" pattern="PDF" />" \
		--target_file="/etc/ImageMagick-6/policy.xml"


# Remove the option backup_core_only if it's in the settings.yml file
ynh_app_setting_delete $app backup_core_only


#=================================================
# RESTORE THE FAIL2BAN CONFIGURATION
#=================================================
ynh_script_progression --message="Restoring the Fail2Ban configuration..." --weight=7

ynh_restore_file --origin_path="/etc/fail2ban/jail.d/$app.conf"
ynh_restore_file --origin_path="/etc/fail2ban/filter.d/$app.conf"

if [ ! -f "$log_file" ]; then
	touch "$log_file"
	chown $app: "$log_file"
fi

ynh_systemd_action --action=restart --service_name=fail2ban

#=================================================
# GENERIC FINALIZATION
#=================================================
# RELOAD NGINX
#=================================================
ynh_script_progression --message="Reloading nginx web server..." --weight=1
ynh_systemd_action --service_name=nginx --action=reload

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

ynh_systemd_action --service_name=$app --action="start" --line_match="Starting Gevent server on" -t 30

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

ynh_script_progression --message="Restoration completed for $app" --last
#=================================================
# WARNING FOR READ ACCESS
#=================================================
ynh_print_warn "Please, give required access to $app user to the $calibre_dir folder."

