#!/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=$1
path=$2
admin_user=$3
# Setup variables
app=$APPNAME
db_user=$app
db_name=$app

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


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

# Check domain/path availability
sudo yunohost app checkurl $domain$path -a freshrss || 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=$(ynh_mkdir_tmp)
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
#check wallabag
sharingEnable=0
if sudo yunohost app list --installed -f wallabag2 | grep -q id ; then
    echo "Detected wallabag V2"
    wallabagPath=$(sudo yunohost app setting wallabag2 path | sed 's#/*$##')
    wallabagUrl=$domain$wallabagPath
    sharingWallabag="'sharing' => \n\tarray( \n\t\t0 => \n\t\tarray(\n\t\t\t'type' => 'wallabagv2',\n\t\t\t'name' => 'Wallabag',\n\t\t\t'url' => 'https://$wallabagUrl',\n\t\t),"
    sharingEnable=1
elif sudo yunohost app list --installed -f wallabag | grep -q id ; then
    echo "Detected wallabag"
    wallabagPath=$(sudo yunohost app setting wallabag path)
    wallabagUrl=$domain$wallabagPath
    sharingWallabag="'sharing' => \n\tarray( \n\t\t0 => \n\t\tarray(\n\t\t\t'type' => 'wallabag',\n\t\t\t'name' => 'Wallabag',\n\t\t\t'url' => 'https://$wallabagUrl',\n\t\t),"
    sharingEnable=1
fi

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/freshrss.conf

#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 www-data: $FINAL_PATH/data/
sudo chown -R www-data: $FINAL_PATH/extensions/
#skip api directory
ynh_app_setting_set "$app" skipped_uris -v /api/greader.php

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