#!/bin/bash
set -eu

# Source YunoHost helpers
source /usr/share/yunohost/helpers

# Retrieve arguments
domain=$YNH_APP_ARG_DOMAIN
path=$YNH_APP_ARG_PATH
is_public=$YNH_APP_ARG_IS_PUBLIC
app=ihatemoney

# Source local utils
source _common.sh

path=$(ynh_normalize_url_path $path)

# Database settings
db_pwd=$(ynh_string_random)
db_name=$app
db_user=$app

# Constant arguments
db_user=ihatemoney
secret_key=`openssl rand -base64 32`
mails_sender="no-reply@${domain}"

sudo yunohost app checkurl $domain$path -a ihatemoney
if [[ ! $? -eq 0 ]]; then
  ynh_die "${domain}${path} is not available"
fi


# Configure database
ynh_mysql_create_db "$db_name" "$db_user" "$db_pwd"
ynh_app_setting_set $app mysqlpwd $db_pwd

# Delete db, user dirs and conf if exit with an error
# inspired from https://github.com/Kloadut/owncloud_ynh/blob/master/scripts/install#L37

function exit_properly
{
    set +e
    ynh_mysql_drop_db $db_name
    ynh_mysql_drop_user $db_user
    sudo userdel ihatemoney
    sudo rm -Rf /opt/yunohost/ihatemoney
    sudo rm -Rf /etc/ihatemoney
    sudo rm /etc/nginx/conf.d/$domain.d/ihatemoney.conf
    sudo rm /etc/supervisor/conf.d/ihatemoney.conf
    ynh_die "Install script failed, aborted and rolled back the installation"
}
trap exit_properly ERR

# Save app settings
ynh_app_setting_set $app domain $domain
ynh_app_setting_set $app is_public "$is_public"

install_apt_dependencies

create_unix_user

# Prepare venv
init_virtualenv
sudo /opt/yunohost/ihatemoney/venv/bin/pip install \
     'gunicorn>=19.3.0' \
     'PyMySQL>=0.9,<0.10' \
     'ihatemoney>=2,<3' \

create_system_dirs

# Configure gunicorn
sudo install -o ihatemoney -g ihatemoney -m 644 \
     ../conf/gunicorn.conf.py /etc/ihatemoney/gunicorn.conf.py

# Configure supervisor
configure_supervisor
# In case it was already installed before,
# so that it picks /etc/supervisor/conf.d/ihatemoney.conf:
supervisorctl update
sudo yunohost service add supervisor

# Configure ihatemoney
sed -i "s@MY_SECRET_KEY@$secret_key@" ../conf/ihatemoney.cfg
sed -i "s/MY_EMAIL/$mails_sender/" ../conf/ihatemoney.cfg
sed -i "s@MY_MYSQL_PW@$db_pwd@" ../conf/ihatemoney.cfg
sed -i "s@MY_PATH@$path@" ../conf/ihatemoney.cfg
# Remove the conf directive if served at root
sed -i "/APPLICATION_ROOT='\/'/d" ../conf/ihatemoney.cfg
sudo install -o ihatemoney -g ihatemoney -m 640 \
     ../conf/ihatemoney.cfg /etc/ihatemoney/ihatemoney.cfg

# If app is public, add url to SSOWat conf as skipped_uris
if [[ "$is_public" -ne 0 ]];
then
    ynh_app_setting_set $app unprotected_uris "/"
fi

# Configure Nginx
configure_nginx "$domain" "$path"

# Start backend
sudo service supervisor restart

# Reconfigure sso
sudo yunohost app ssowatconf

# Wait that gunicorn is ready to consider the install finished, that is to
# avoid HTTP 502 right after installation
for i in `seq 1 120`
do
    test -S /tmp/budget.gunicorn.sock && break
	sleep 1
done

sudo service nginx reload
# If socket not ready after 2 minutes waiting, ihatemoney will not work.
test -S /tmp/budget.gunicorn.sock || exit_properly