#!/bin/bash

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

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

upgrade_type=$(ynh_check_app_version_changed)

#=================================================
# NGINX CONFIGURATION
#=================================================
ynh_script_progression --message="Upgrading NGINX web server configuration..." --weight=2

# Create a dedicated NGINX config
ynh_add_nginx_config

#=================================================
# SPECIFIC UPGRADE
#=================================================
# UPGRADING COUCHDB
#=================================================

if [ "$upgrade_type" == "UPGRADE_APP" ]
then
	ynh_script_progression --message="Upgrading CouchDB..." --weight=50

	echo "\
	couchdb couchdb/mode select standalone
	couchdb couchdb/mode seen true
	couchdb couchdb/bindaddress string 127.0.0.1
	couchdb couchdb/bindaddress seen true
	couchdb couchdb/cookie string $password
	couchdb couchdb/adminpass password $password
	couchdb couchdb/adminpass seen true
	couchdb couchdb/adminpass_again password $password
	couchdb couchdb/adminpass_again seen true" | debconf-set-selections
	DEBIAN_FRONTEND=noninteractive # apt-get install -y --force-yes couchdb

	ynh_install_extra_app_dependencies \
		--repo="deb https://apache.jfrog.io/artifactory/couchdb-deb/ $(lsb_release -c -s) main" \
		--key="https://couchdb.apache.org/repo/keys.asc" \
		--package="couchdb"
fi

#=================================================
# UPDATE A CONFIG FILE
#=================================================
ynh_script_progression --message="Updating a configuration file..." --weight=1

ynh_add_config --template="../conf/couch_ynh.ini" --destination="$install_dir/etc/local.d/couch_ynh.ini"

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

#=================================================
# GENERIC FINALIZATION
#=================================================
# SETUP LOGROTATE
#=================================================
ynh_script_progression --message="Upgrading logrotate configuration..." --weight=1

# Use logrotate to manage app-specific logfile(s)
ynh_use_logrotate --non-append

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

yunohost service add $app --description="Open-source document-oriented NoSQL database" --log="/var/log/$app/$app.log" --needs_exposed_ports "$port"

#=================================================
# UPGRADE FAIL2BAN
#=================================================
ynh_script_progression --message="Reconfiguring Fail2Ban..." --weight=3

# Create a dedicated Fail2Ban config
ynh_add_fail2ban_config --logpath="/var/log/couchdb/couchdb.log" --failregex="[warning] .*couch_httpd_auth: Authentication failed for user .+ from <HOST>" --max_retry=5

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

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