#!/bin/bash

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

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

#=================================================
# LOAD SETTINGS
#=================================================
ynh_script_progression --message="Loading installation settings..."

app=$YNH_APP_INSTANCE_NAME

domain=$(ynh_app_setting_get --app=$app --key=domain)
path_url=$(ynh_app_setting_get --app=$app --key=path)
is_public=$(ynh_app_setting_get --app=$app --key=is_public)
final_path=$(ynh_app_setting_get --app=$app --key=final_path)
db_name=$(ynh_app_setting_get --app=$app --key=db_name)
db_user=$db_name
phpversion=$(ynh_app_setting_get --app=$app --key=phpversion)

#=================================================
# CHECK VERSION
#=================================================
ynh_script_progression --message="Checking version..."

upgrade_type=$(ynh_check_app_version_changed)

#=================================================
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
#=================================================
ynh_script_progression --message="Backing up the app before upgrading (may take a while)..."

# 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

#=================================================
# STANDARD UPGRADE STEPS
#=================================================
# STOP SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Stopping a systemd service..."

yunohost service stop zabbix-server
yunohost service stop zabbix-agent

#=================================================
# ENSURE DOWNWARD COMPATIBILITY
#=================================================
ynh_script_progression --message="Ensuring downward compatibility..."

export mysqlconn="mysql -u$db_user -p$db_pwd $db_name"

disable_guest_user

#Patch timeout too short for zabbix agent if needed
change_timeoutAgent  

ynh_remove_logrotate

# Cleaning legacy permissions
if ynh_legacy_permissions_exists; then
	ynh_legacy_permissions_delete_all
fi

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

if [ "$upgrade_type" == "UPGRADE_APP" ]
then
	ynh_script_progression --message="Upgrading source files..."

	# Download, check integrity, uncompress and patch the source from app.src
	ln -s /usr/share/zabbix "$final_path"
fi

chmod 750 "$final_path"
chmod -R o-rwx "$final_path"
chown -R $app:www-data "$final_path"

#=================================================
# NGINX CONFIGURATION
#=================================================
ynh_script_progression --message="Upgrading NGINX web server configuration..."

# Create a dedicated NGINX config
ynh_add_nginx_config

#=================================================
# UPGRADE DEPENDENCIES
#=================================================
ynh_script_progression --message="Upgrading dependencies..."

if [ "$upgrade_type" == "UPGRADE_APP" ]
then
	cp -rp /etc/zabbix /tmp/
	cp -p /usr/share/zabbix/conf/zabbix.conf.php /tmp/

	DEBIAN_FRONTEND=noninteractive apt-mark unhold zabbix-server-mysql zabbix-frontend-php
	ynh_package_remove zabbix-server-mysql zabbix-frontend-php

	ynh_script_progression --message="Install Zabbix repository"
	install_zabbix_repo

	ynh_print_info "Update zabbix via apt package"
	ynh_install_app_dependencies $pkg_dependencies		
	ynh_secure_remove  --file="/usr/share/zabbix/conf/zabbix.conf.php"
	cp -rpf /tmp/zabbix /etc/
	cp -pf /tmp/zabbix.conf.php /usr/share/zabbix/conf/

	ynh_secure_remove  --file="/tmp/zabbix*"
fi

#=================================================
# PHP-FPM CONFIGURATION
#=================================================
ynh_script_progression --message="Upgrading PHP-FPM configuration..."

# Create a dedicated PHP-FPM config
ynh_add_fpm_config --package="$extra_php_dependencies"

#=================================================
# SPECIFIC UPGRADE
#=================================================
# INSTALL hook to verify if conf file is broken (after an update for example)
#=================================================

update_initZabbixConf

#=================================================
# Update db to utf8
#=================================================

convert_ZabbixDB

#=================================================
# Add settings for yunohost mail server
#=================================================

set_mediatype_default_yunohost

#=================================================
# IMPORT YUNOHOST TEMPLATE
#=================================================
ynh_script_progression --message="Importing YunoHost template in Zabbix"

import_template

#=================================================
# GENERIC FINALIZATION
#=================================================
# SETUP SSOWAT
#=================================================
ynh_script_progression --message="Configuring permissions..."

# Make app public if necessary
if [ $is_public -eq 1 ]
then
	# Everyone can access the app.
	# The "main" permission is automatically created before the install script.
	ynh_permission_update --permission="main" --add="visitors"
fi

#=================================================
# INTEGRATE SERVICE IN YUNOHOST
#=================================================
ynh_script_progression --message="Integrating service in YunoHost..."

yunohost service add snmpd --description="Management of SNMP Daemon"
yunohost service add zabbix-server --description="Management Zabbix server daemon : Collect, agregate, compute and notify"
yunohost service add zabbix-agent --description="Management Zabbix agent daemon : send informations about this host to the server"

#=================================================
# START SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Starting a systemd service..."

systemctl enable zabbix-agent --quiet && systemctl restart zabbix-agent
change_timeoutAgent
systemctl enable zabbix-server --quiet && systemctl restart zabbix-server

#test if zabbix server is started
check_proc_zabbixagent

#test if zabbix agent is started
check_proc_zabbixserver

#=================================================
# RELOAD NGINX
#=================================================
ynh_script_progression --message="Reloading NGINX web server..."

ynh_systemd_action --service_name=nginx --action=reload

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

ynh_script_progression --message="Upgrade of $app completed"
