#!/bin/bash

# Exit on command errors and treat unset variables as an error
set -eu

# Load common variables and helpers
. ./_common.sh

# Retrieve arguments
domain=$YNH_APP_ARG_DOMAIN
path=${YNH_APP_ARG_PATH%/}
admin_user=$YNH_APP_ARG_ADMIN
# Setup variables
app=$YNH_APP_INSTANCE_NAME
db_user=$app
db_name=$app

FINAL_PATH="/var/www/$app"

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

# if path do not begin with / add a / at the begining
if [ "${path:0:1}" != "/" ]; then
       path="/$path"
fi
# if path do not end with / add a / at the end
if [ "${path:${#path}-1}" == "/" ] && [ ${#path} -gt 1 ]; then
       path="${path:0:${#path}-1}"
fi


# Check user parameter if not empty
if [[ $admin_user != '' ]]; then
    ynh_user_exists $admin_user || ynh_die "Wrong user"
    ynh_app_setting_set $app admin_user $admin_user
fi

# Check domain/path availability
sudo yunohost app checkurl $domain$path -a $app || ynh_die "The path ${domain}${path} is not available for app installation."

#install php5-cli
ynh_package_install_from_equivs ../conf/${DEPS_PKG_NAME}.control \
  || ynh_die "Unable to install dependencies"

# Generate random DES key & password
deskey=$(ynh_string_random)
db_pwd=$(ynh_string_random)
app_salt=$(ynh_string_random)

# Initialize database and store mysql password for upgrade
ynh_mysql_create_db "$db_name" "$db_user" "$db_pwd"
ynh_app_setting_set "$app" mysqlpwd "$db_pwd"

# Copy files to the right place
TMPDIR=$(mktemp -d)
sudo chmod 755 "$TMPDIR"
extract_freshrss "$TMPDIR"

$TMPDIR/cli/do-install.php --default_user $admin_user --auth_type http_auth --environment production --base_url $domain/$path --title FreshRSS --api_enabled --db-type mysql --db-host localhost --db-user $db_user --db-password $db_pwd --db-base $db_name

# Add users
for myuser in $(ynh_user_list)
do
    user_token=$(ynh_string_random)
    $TMPDIR/cli/create-user.php --user $myuser --language en --token $user_token --no-default-feeds
done
# Move temp dir to final dir
sudo mv $TMPDIR $FINAL_PATH
# Modify Nginx configuration file and copy it to Nginx conf directory
sed -i "s@PATHTOCHANGE@$path@g" ../conf/nginx.conf
sed -i "s@ALIASTOCHANGE@$FINAL_PATH/@g" ../conf/nginx.conf
sudo cp ../conf/nginx.conf /etc/nginx/conf.d/$domain.d/$app.conf

#update hook for multi instance
sed -i "s@APPNAMETOCHANGE@$app@g" ../hooks/post_app_addaccess
sed -i "s@APPNAMETOCHANGE@$app@g" ../hooks/post_user_create
sed -i "s@APPNAMETOCHANGE@$app@g" ../hooks/post_user_delete

#install update cron
# Add cron job
cron_path="/etc/cron.d/$app"
sed -i "s@#DESTDIR#@${FINAL_PATH}@g" ../conf/freshrss.cron
sudo cp ../conf/freshrss.cron "$cron_path"
sudo chmod 644 "$cron_path"


# Set permissions to freshrss directory
sudo chown -R root:root $FINAL_PATH
sudo chown -R www-data: $FINAL_PATH/data/
sudo chown -R www-data: $FINAL_PATH/extensions/
#skip api directory
ynh_app_setting_set "$app" skipped_uris /api/greader.php
# Reload Nginx and regenerate SSOwat conf
sudo service nginx reload
sudo yunohost app ssowatconf