set -eux

# Retrieve arguments
app=$YNH_APP_INSTANCE_NAME
number=$YNH_APP_INSTANCE_NUMBER
domain=$YNH_APP_ARG_DOMAIN
path=$YNH_APP_ARG_PATH
helper_to_test=$YNH_APP_ARG_HELPER_TO_TEST

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

# Check domain/path availability
sudo yunohost app checkurl $domain/$path -a $app

# Register mode of restoration
ynh_app_setting_set $app helper_to_test $helper_to_test

# Add config for nginx
sudo sed -i "s@PATHTOCHANGE@$path@g" ../conf/nginx.conf
sudo sed -i "s@FOLDER@$app/@g" ../conf/nginx.conf
sudo cp ../conf/nginx.conf /etc/nginx/conf.d/$domain.d/$app.conf

# Make directory for app web data
sudo mkdir -p /var/www/$app
sudo cp ../conf/index.html /var/www/$app

# Create a dummy mysql db
db_user=$app
db_pwd="yoloswag42"
ynh_mysql_create_db "$db_user" "$db_user" $db_pwd
ynh_app_setting_set $app mysqlpwd $db_pwd

# Other custom stuff
sudo cp ../sources/importantfile /etc/

# Reload Nginx and regenerate SSOwat conf
sudo service nginx reload
#sudo yunohost app ssowatconf
