#!/bin/bash

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

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

#=================================================
# CREATE DIRECTORIES
#=================================================

if [ -d "$config_path" ]; then
	ynh_die --message="There is already a directory: $config_path "
fi
ynh_app_setting_set --app="$app" --key=config_path --value="$config_path"

#=================================================
# SYSTEM CONFIGURATION
#=================================================รง
ynh_script_progression --message="Configuring the dedicated system user..." --weight=1

if getent group render && ! id -Gn "$app" | grep -qw "\brender\b" >/dev/null; then
	# Add user to render group
	adduser "$app" render
fi

#=================================================
# OPEN PORTS
#=================================================
ynh_script_progression --message="Checking whether to open ports..." --weight=1
configure_jellyfin_discovery_ports install

ports_args=()
if [[ "${opened_ports:-__NOTHING__}" != "__NOTHING__" ]]; then
	ports_args=( "--needs_exposed_ports" "${opened_ports[@]}" )
fi

#=================================================
# INSTALL PACKAGES
#=================================================
ynh_script_progression --message="Installing packages..." --weight=1

install_jellyfin_packages

#=================================================
# ADD A CONFIGURATION
#=================================================
ynh_print_info --message="Waiting 30s to let Jellyfin fully start a first time..."
sleep 30

ynh_script_progression --message="Adding a configuration file..." --weight=1

ynh_systemd_action --service_name="$app" --action="stop" --log_path="systemd" --timeout=15

ynh_add_config --template="system.xml" --destination="$config_path/system.xml"
ynh_add_config --template="network.xml" --destination="$config_path/network.xml"
ynh_add_config --template="logging.json" --destination="$config_path/logging.json"

#=================================================
# INSTALL LDAP PLUGIN
#=================================================
ynh_script_progression --message="Installing LDAP plugin..." --weight=2

ynh_setup_source --dest_dir="/var/lib/jellyfin/plugins/LDAP Authentication" --source_id=plugin_ldap

mkdir -p /var/lib/jellyfin/plugins/configurations/
ynh_add_config --template="LDAP-Auth.xml" --destination="/var/lib/jellyfin/plugins/configurations/LDAP-Auth.xml"

#=================================================
# SECURE FILES AND DIRECTORIES
#=================================================
ynh_script_progression --message="Securing files and directories..."

# Set permissions to app files
chown -R "$app:" "$data_path"
chown -R "$app:" "$config_path"

#=================================================
# YUNOHOST MULTIMEDIA INTEGRATION
#=================================================
ynh_script_progression --message="Adding multimedia directories..." --weight=2

# Build YunoHost multimedia directories
ynh_multimedia_build_main_dir

# Allow Jellyfin to write into these directories
ynh_multimedia_addaccess "$app"

#=================================================
# SYSTEM CONFIGURATION
#=================================================
ynh_script_progression --message="Adding system configurations related to $app..." --weight=1

# Create a dedicated NGINX config using the conf/nginx.conf template
ynh_add_nginx_config

ynh_add_config --template="systemd.service" --destination="/etc/systemd/system/jellyfin.service.d/baseurl.service.conf"
systemctl daemon-reload
yunohost service add "$app" --description="Jellyfin media center" "${ports_args[@]}"

# Use logrotate to manage application logfile(s)
ynh_use_logrotate

#=================================================
# START SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Starting $app's systemd service..." --weight=1

# Start a systemd service
ynh_systemd_action --service_name="$app" --action="start" --log_path="systemd" --timeout=15

#=================================================
# END OF SCRIPT
#=================================================

ynh_script_progression --message="Installation of $app completed" --last