#!/bin/bash

#=================================================
# GENERIC STARTING
#=================================================
# IMPORT GENERIC HELPERS
#=================================================

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

#=================================================
# MANAGE FAILURE OF THE SCRIPT
#=================================================

ynh_abort_if_errors # Active trap pour arrêter le script si une erreur est détectée.

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

domain=$YNH_APP_ARG_DOMAIN
path_url=$YNH_APP_ARG_PATH
is_public=$YNH_APP_ARG_IS_PUBLIC

app=$YNH_APP_INSTANCE_NAME

#=================================================
# CHECK IF THE APP CAN BE INSTALLED WITH THIS ARGS
#=================================================

final_path=/var/www/$app
test ! -e "$final_path" || ynh_die "This path already contains a folder"

# Normalize the url path syntax
path_url=$(ynh_normalize_url_path $path_url)

# Check web path availability
ynh_webpath_available $domain $path_url
# Register (book) web path
ynh_webpath_register $app $domain $path_url

#=================================================
# STORE SETTINGS FROM MANIFEST
#=================================================

ynh_app_setting_set $app domain "$domain"
ynh_app_setting_set $app is_public "$is_public"
ynh_app_setting_set $app path_url "$path_url"

#=================================================
# STANDARD MODIFICATIONS
#=================================================
# FIND AND OPEN A PORT
#=================================================

# Find a free port
port=$(ynh_find_port 7777)
# Open this port
yunohost firewall allow --no-upnp TCP $port 2>&1
ynh_app_setting_set $app port $port
#=================================================
# INSTALL NODEJS
#=================================================

ynh_install_nodejs 4 
#=================================================
# CREATE DEDICATED USER
#=================================================

ynh_system_user_create $app

#=================================================
# SPECIFIC SETUP
#=================================================
# HANDLE LOG FILES AND LOGROTATE
#=================================================

# Créer le dossier de log
mkdir -p /var/log/$app
touch /var/log/$app/$app.log
chown $app -R /var/log/$app

# Setup logrotate
ynh_use_logrotate


#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================

final_path=/var/www/$app
ynh_app_setting_set $app final_path $final_path
ynh_setup_source $final_path

# Set files ownership during installation
chown $app: $final_path -R
chmod 755 $final_path -R

#=================================================
# Modify Nginx configuration file and copy it to Nginx conf directory
#=================================================

ynh_add_nginx_config

#=================================================
# ADD SYSTEMD SERVICE
#=================================================

ynh_replace_string "__NODE__" "$nodejs_path" "../conf/systemd.service"
ynh_replace_string "__NODEJS__" "$nodejs_use_version" "../conf/systemd.service"
ynh_replace_string "__ENV_PATH__" "$PATH" "../conf/systemd.service"
ynh_add_systemd_config

#=================================================
# INSTALL HASTEBIN
#=================================================

ynh_use_nodejs
script_dir="$PWD"
pushd "$final_path"
chown -R $app: $final_path
npm install
popd
DATA_PATH="/home/yunohost.app/"$app
mkdir -p $DATA_PATH
chown -R "$app":"$app" $final_path $DATA_PATH

#=================================================
# Configure haste with config.js file
#=================================================

ynh_replace_string "YNH_DATA_PATH" "$DATA_PATH" "$final_path/config.js"

#=================================================
# ENABLE SERVICE IN ADMIN PANEL
#=================================================

# Ajoute le service au monitoring de Yunohost.
yunohost service add $app --log "/var/log/$app/$app.log"

#=================================================
# START HASTEBIN IN BACKGROUND
#=================================================

systemctl start $app

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

if [ $is_public -eq 1 ];
then
  ynh_app_setting_set "$app" unprotected_uris "/"
fi

#=================================================
# Add HASTE AS A BINARY FILE
#=================================================

ynh_replace_string "YNH_HASTE_URL" "${domain}${path_url%/}" "../conf/haste.sh"
sudo cp ../conf/haste.sh /usr/bin/"$app"
sudo chmod +x /usr/bin/"$app"

#=================================================
# RELOAD NGINX
#=================================================

systemctl reload nginx