#!/bin/bash

# TODO 
# - which service to register to ynuhosto? diaspora.target only ? All of them ?
# - a setting to enable / disable registration
# - say something about the registration to https://the-federation.info/ 
# - ...

## vars for remove script
can_remove_db=0
can_remove_home=0
can_remove_user=0

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

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

#=================================================
# MANAGE SCRIPT FAILURE
#=================================================
# Exit if an error occurs during the execution of the script
ynh_clean_setup() {
  ynh_clean_check_starting
}
ynh_abort_if_errors

#=================================================
# RETRIEVE ARGUMENTS FROM THE MANIFEST
#=================================================

domain=$YNH_APP_ARG_DOMAIN
admin=$YNH_APP_ARG_ADMIN
admin_password=$YNH_APP_ARG_ADMIN_PASSWORD
admin_email=$(ynh_user_get_info --username=$admin --key=mail)

app=$YNH_APP_INSTANCE_NAME
final_path=/var/www/$app

#=================================================
# CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS
#=================================================
ynh_script_progression --message="Validating installation parameters..." --weight=1
# check path availability
test ! -e "$final_path" || ynh_die "This path already contains a folder"
can_remove_home=1
# Register (book) web path
ynh_webpath_register --app=$app --domain=$domain --path_url=/

#=================================================
# STORE SETTINGS FROM MANIFEST
#=================================================
ynh_script_progression --message="Saving app settings..." --weight=1
ynh_app_setting_set --app=$app --key=domain --value=$domain
ynh_app_setting_set --app=$app --key=path --value=/
ynh_app_setting_set --app=$app --key=final_path --value=$final_path

#=================================================
# STANDARD MODIFICATIONS
#=================================================

#=================================================
# INSTALL DEPENDENCIES
#=================================================
ynh_script_progression --message="Installing dependencies..." --weight=5
ynh_install_app_dependencies $pkg_dependencies $ruby_build_dependencies

#=================================================
# CHECK DB AVAILABILITY
#=================================================
ynh_script_progression --message="Check DB availability"
# now that we have psql for sure, test db existence
ynh_script_progression --message="Checking DB availability" --weight=1
ynh_psql_database_exists --database $app && ynh_die --message="There is already a database: $app"
can_remove_db=1

#=================================================
# CREATE DEDICATED USER
#=================================================
ynh_script_progression --message="Creating user..." --weight=1
# Create a system user
ynh_system_user_create --username=$app --home_dir=$final_path --use_shell
can_remove_user=1
mkdir -p $final_path
chmod 0750 $final_path -R
chown $app:www-data $final_path

#=================================================
# INSTALL RVM AND RUBY FOR CURRENT USER
#=================================================
ynh_script_progression --message="Installing rvm and ruby... (will take a long time)" --weight=20
source ./install_ruby

#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
# Download, check integrity, unucompress and patch the source from app.src
pushd $final_path
ynh_script_progression --message="Download the sources..." --weight=10
sudo -u $app git clone https://github.com/diaspora/diaspora.git -b $current_tag
popd

#=================================================
# CREATE A POSTGRESQL DATABASE
#=================================================
ynh_script_progression --message="Creating database..." --weight=1
db_name=$(ynh_sanitize_dbid $app)
ynh_app_setting_set --app=$app --key=db_name --value=$db_name
ynh_psql_test_if_first_run
ynh_psql_setup_db --db_user=$db_name --db_name=$db_name
db_pass=$(ynh_app_setting_get --app=$app --key=psqlpwd)

#=================================================
# EXPORT VARIABLES FOR TEMPLATING
#=================================================
export app
export domain
export db_pass
export final_path
export admin

#=================================================
# CONFIGURE DIASPORA
#=================================================
ynh_script_progression --message="Configure diaspora..." --weight=1
ynh_render_template ../conf/diaspora.yml $final_path/diaspora/config/diaspora.yml
ynh_render_template ../conf/database.yml $final_path/diaspora/config/database.yml

#=================================================
# STORE THE CHECKSUM OF THE CONFIG FILE
#=================================================
# Calculate and store the config file checksum into the app settings
ynh_store_file_checksum --file="$final_path/diaspora/config/diaspora.yml"
ynh_store_file_checksum --file="$final_path/diaspora/config/database.yml"

#=================================================
# Bundle the ruby app
#=================================================
ynh_script_progression --message="Precompile assets (will take a long time)..." --weight=40
source ./bundle_app

#=================================================
# NGINX CONFIGURATION
#=================================================
# Create a dedicated nginx config
ynh_script_progression --message="configure nginx..." --weight=1
ynh_add_nginx_config

#=================================================
# SETUP SYSTEMD
#=================================================
# Create a dedicated systemd config
ynh_script_progression --message="configure systemd unit..." --weight=1
source ./create_services

#=================================================
# ADVERTISE SERVICE IN ADMIN PANEL
#=================================================
yunohost service add $app.target \
  --log $final_path/diaspora/log/production.log \
        $final_path/diaspora/log/unicorn-stderr.log\
        $final_path/diaspora/log/unicorn-stdout.log\
        $final_path/diaspora/log/sidekiq.log\
  --description "Diaspora service (unicorn web and sidekiq)"

#=================================================
# SETUP SSOWAT
#=================================================



#=================================================
# CREATE AN ADMIN
#=================================================
ynh_script_progression --message="Create admin..." --weight=1
pushd $final_path/diaspora
sudo -u $app /bin/bash --login << EOF
RAILS_ENV=production bundle exec rails console << END
user = User.build({username: '$admin', email: '$admin_email', password: '$admin_password', password_confirmation: '$admin_password' })
user.save
user.seed_aspects
Role.add_admin user.person
END
EOF
popd