#!/bin/bash

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

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

#=================================================
# LOAD SETTINGS
#=================================================

app=$YNH_APP_INSTANCE_NAME

domain=$(ynh_app_setting_get $app domain)
path_url=$(ynh_app_setting_get $app path)
admin=$(ynh_app_setting_get $app admin)
query_logging=$(ynh_app_setting_get $app query_logging)
final_path=$(ynh_app_setting_get $app final_path)
port=$(ynh_app_setting_get $app port)

#=================================================
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
#=================================================

# Backup the current version of the app
ynh_backup_before_upgrade
ynh_clean_setup () {
	# restore it if the upgrade fails
	ynh_restore_upgradebackup
}
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors

#=================================================
# CHECK THE PATH
#=================================================

path_url=$(ynh_normalize_url_path $path_url)	# Vérifie et corrige la syntaxe du path.

#=================================================
# STANDARD UPGRADE STEPS
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================

# Update la copie du repo de pihole (nécessaire pour Gravity)
pihole_local_repo="/etc/.pihole"
ynh_setup_source "$pihole_local_repo"
# Update le dashboard admin
ynh_setup_source "$final_path" admin_dashboard

#=================================================
# NGINX CONFIGURATION
#=================================================

ynh_add_nginx_config

#=================================================
# CREATE DEDICATED USER
#=================================================

ynh_system_user_create $app	# Create the dedicated user, if not exist

#=================================================
# PHP-FPM CONFIGURATION
#=================================================

ynh_add_fpm_config	# Créer le fichier de configuration du pool php-fpm et le configure.

#=================================================
# SPECIFIC UPGRADE
#=================================================
# UPDATE PI-HOLE SCRIPTS
#=================================================

# Update les scripts de Pi-hole
pihole_dir="/opt/pihole"
cp -a "$pihole_local_repo/gravity.sh" "$pihole_dir/"
cp -a $pihole_local_repo/advanced/Scripts/*.sh "$pihole_dir/"

#=================================================
# Copy the Pi-hole main script
#=================================================

cp -a "$pihole_local_repo/pihole" /usr/local/bin/
cp -a "$pihole_local_repo/advanced/bash-completion/pihole" /etc/bash_completion.d/pihole

#=================================================
# CREATE SUDOER FILE
#=================================================

# Cette configuration sudoers autorise pihole à exécuter /usr/local/bin/pihole en root sans mot de passe. Pas plus.
cp "$pihole_local_repo/advanced/pihole.sudo" /etc/sudoers.d/pihole
echo "$app ALL=NOPASSWD: /usr/local/bin/pihole" >> /etc/sudoers.d/pihole
chmod 0440 /etc/sudoers.d/pihole

#=================================================
# UPDATE LOGROTATE SCRIPT FOR PI-HOLE
#=================================================

pihole_storage="/etc/pihole"
cp "$pihole_local_repo/advanced/logrotate" "$pihole_storage/logrotate"
dnsmasq_user=$(grep DNSMASQ_USER= /etc/init.d/dnsmasq | cut -d'"' -f2)
sed -i "/# su #/d;" "$pihole_storage/logrotate"

#=================================================
# UPDATE OF PIHOLE-FTL
#=================================================

systemctl stop pihole-FTL

git clone https://github.com/pi-hole/FTL
# Plutôt que télécharger le binaire C, on le compile nous-même.
( cd FTL
SUPPRESS_WARNING make
SUPPRESS_WARNING make install )
cp -a $pihole_local_repo/advanced/pihole-FTL.service /etc/init.d/pihole-FTL
chmod +x /etc/init.d/pihole-FTL
SUPPRESS_WARNING systemctl enable pihole-FTL

#=================================================
# BUILD THE VARIABLES FILE
#=================================================

setupVars="$pihole_storage/setupVars.conf"

ynh_backup_if_checksum_is_different "$setupVars"	# Créé un backup du fichier de config si il a été modifié.

# Trouve l'interface réseau par défaut
main_iface=$(route | grep default | awk '{print $8;}' | head -n1)
echo "PIHOLE_INTERFACE=$main_iface" > $setupVars
echo "IPV4_ADDRESS=127.0.0.1" >> $setupVars
echo "IPV6_ADDRESS=" >> $setupVars
echo "PIHOLE_DNS_1=" >> $setupVars
echo "PIHOLE_DNS_2=" >> $setupVars
if [ $query_logging -eq 1 ]; then
	query_logging=true
else
	query_logging=false
fi
echo "QUERY_LOGGING=$query_logging" >> $setupVars
echo "INSTALL_WEB=true" >> $setupVars

ynh_store_file_checksum "$setupVars"	# Enregistre la somme de contrôle du fichier de config

#=================================================
# UPDATE THE CRON JOB
#=================================================

cp $pihole_local_repo/advanced/pihole.cron /etc/cron.d/pihole

#=================================================
# START PIHOLE-FTL
#=================================================

systemctl start pihole-FTL

#=================================================
# START PIHOLE-FTL
#=================================================

systemctl start pihole-FTL

#=================================================
# UPDATE THE CONF_REGEN HOOK
#=================================================

cp ../conf/dnsmasq_regenconf_hook /usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app

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

systemctl reload nginx
