#!/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

# 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

# Remove trailing "/" for next commands
path=${path%/}

# 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
    root_pwd=$(sudo cat /etc/yunohost/mysql)
    mysql -u root -p$root_pwd -e "DROP DATABASE $db_name ; 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 debian packages dependencies
sudo apt-get install -y -qq python-dev python-virtualenv supervisor libmysqlclient-dev
# Create the dedicated user
sudo useradd ihatemoney -d /opt/yunohost/ihatemoney/

# Prepare venv
sudo virtualenv /opt/yunohost/ihatemoney/venv
sudo /opt/yunohost/ihatemoney/venv/bin/pip install -r ../sources/budget/requirements.txt
sudo /opt/yunohost/ihatemoney/venv/bin/pip install gunicorn>=19.3.0 MySQL-python

# Install source
sudo cp -r ../sources/ /opt/yunohost/ihatemoney/src/
sudo find /opt/yunohost/ihatemoney/src/ -type f | while read LINE; do sudo chmod 640 "$LINE" ; done
sudo find /opt/yunohost/ihatemoney/src/ -type d | while read LINE; do sudo chmod 755 "$LINE" ; done
sudo chown -R ihatemoney:ihatemoney /opt/yunohost/ihatemoney/src
sudo chown -R www-data:www-data /opt/yunohost/ihatemoney/src/budget/static

# Create various dirs
sudo install -o ihatemoney -g ihatemoney -m 755 \
     -d /var/log/ihatemoney /etc/ihatemoney

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

# Configure supervisor
sudo install -o root -g root -m 644 \
     ../conf/supervisord.conf /etc/supervisor/conf.d/ihatemoney.conf
sudo yunohost service add supervisor

# Configure ihatemoney
sed -i "s@MY_SECRET_KEY@$secret_key@" ../conf/settings.py
sed -i "s/MY_EMAIL/$mails_sender/" ../conf/settings.py
sed -i "s@MY_MYSQL_PW@$db_pwd@" ../conf/settings.py
sed -i "s@MY_PATH@$path@" ../conf/settings.py
sudo install -o ihatemoney -g ihatemoney -m 640 \
     ../conf/settings.py /etc/ihatemoney/settings.py
# The settings have to be stored here (on python path)
sudo ln -s /etc/ihatemoney/settings.py /opt/yunohost/ihatemoney/src/budget/settings.py


# 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 and reload
sed -i "s@PATHTOCHANGE@$path@g" ../conf/nginx.conf
sudo install -o root -g root -m644 \
     ../conf/nginx.conf /etc/nginx/conf.d/$domain.d/ihatemoney.conf
sudo service nginx reload

# Start backend
sudo service supervisor restart

# Reconfigure sso
sudo yunohost app ssowatconf