#!/bin/bash

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

# Load common variables for all scripts.
source _common.sh
source /usr/share/yunohost/helpers

ynh_abort_if_errors

#=================================================
# RETRIEVE ARGUMENTS
#=================================================

app=$YNH_APP_INSTANCE_NAME

final_path=$(ynh_app_setting_get $app final_path)

#=================================================
# SPECIFIC CODE
#=================================================
# DECLARE GENERIC FUNCTION
#=================================================

config_file="$final_path/settings.json"

get_config_value() {
	option_name="$1"
	# Get the value of this option in the config file
	grep "^ *\"$option_name\" *:" "$config_file" | cut -d':' -f2 | sed s'/[ \"]//g' | cut -d',' -f1
}

#=================================================
# LOAD VALUES
#=================================================

# Load the real value from the app config or elsewhere.
# Then get the value from the form.
# If the form has a value for a variable, take the value from the form,
# Otherwise, keep the value from the app config.

# Export
old_export="$(ynh_app_setting_get $app export)"
export="${YNH_CONFIG_MAIN_EXPORT_EXPORT:-$old_export}"

# padOptions noColors
old_pad_config_nocolors="$(get_config_value noColors)"
pad_config_nocolors="${YNH_CONFIG_MAIN_PAD_CONFIGURATION_PAD_CONFIG_NOCOLORS:-$old_pad_config_nocolors}"

# padOptions showLineNumbers
old_pad_config_showlinenumbers="$(get_config_value showLineNumbers)"
pad_config_showlinenumbers="${YNH_CONFIG_MAIN_PAD_CONFIGURATION_PAD_CONFIG_SHOWLINENUMBERS:-$old_pad_config_showlinenumbers}"

# padOptions chatAndUsers
old_pad_config_chatandusers="$(get_config_value chatAndUsers)"
pad_config_chatandusers="${YNH_CONFIG_MAIN_PAD_CONFIGURATION_PAD_CONFIG_CHATANDUSERS:-$old_pad_config_chatandusers}"

# padOptions alwaysShowChat
old_pad_config_alwaysshowchat="$(get_config_value alwaysShowChat)"
pad_config_alwaysshowchat="${YNH_CONFIG_MAIN_PAD_CONFIGURATION_PAD_CONFIG_ALWAYSSHOWCHAT:-$old_pad_config_alwaysshowchat}"

# Plugin option ep_markdown_default
old_pad_config_show_markdown="$(get_config_value ep_markdown_default)"
pad_config_show_markdown="${YNH_CONFIG_MAIN_PAD_CONFIGURATION_PAD_CONFIG_SHOW_MARKDOWN:-$old_pad_config_show_markdown}"

# MyPads
if [ -d $final_path/node_modules/ep_mypads ]
then
    # Enable
	old_mypads=1
else
    # Disable
	old_mypads=0
fi
mypads="${YNH_CONFIG_MAIN_MYPADS_CONFIGURATION_MYPADS:-$old_mypads}"

# LDAP for MyPads
if grep -q "//noldap" $config_file
then
    # Disable
	old_useldap=0
else
    # Enable
	old_useldap=1
fi
useldap="${YNH_CONFIG_MAIN_MYPADS_CONFIGURATION_USELDAP:-$old_useldap}"

# Overwrite settings.json file
old_overwrite_settings="$(ynh_app_setting_get --app=$app --key=overwrite_settings)"
overwrite_settings="${YNH_CONFIG_MAIN_OVERWRITE_FILES_OVERWRITE_SETTINGS:-$old_overwrite_settings}"

# Overwrite credentials.json file
old_overwrite_credentials="$(ynh_app_setting_get --app=$app --key=overwrite_credentials)"
overwrite_credentials="${YNH_CONFIG_MAIN_OVERWRITE_FILES_OVERWRITE_CREDENTIALS:-$old_overwrite_credentials}"

# Overwrite nginx configuration
old_overwrite_nginx="$(ynh_app_setting_get --app=$app --key=overwrite_nginx)"
overwrite_nginx="${YNH_CONFIG_MAIN_OVERWRITE_FILES_OVERWRITE_NGINX:-$old_overwrite_nginx}"

# Overwrite systemd configuration
old_overwrite_systemd="$(ynh_app_setting_get --app=$app --key=overwrite_systemd)"
overwrite_systemd="${YNH_CONFIG_MAIN_OVERWRITE_FILES_OVERWRITE_SYSTEMD:-$old_overwrite_systemd}"

#=================================================
# SHOW_CONFIG FUNCTION FOR 'SHOW' COMMAND
#=================================================

show_config() {
	# here you are supposed to read some config file/database/other then print the values
	# ynh_return "YNH_CONFIG_${PANEL_ID}_${SECTION_ID}_${OPTION_ID}=value"

	ynh_return "YNH_CONFIG_MAIN_EXPORT_EXPORT=$export"

	ynh_return "YNH_CONFIG_MAIN_PAD_CONFIGURATION_PAD_CONFIG_NOCOLORS=$pad_config_nocolors"
	ynh_return "YNH_CONFIG_MAIN_PAD_CONFIGURATION_PAD_CONFIG_SHOWLINENUMBERS=$pad_config_showlinenumbers"
	ynh_return "YNH_CONFIG_MAIN_PAD_CONFIGURATION_PAD_CONFIG_CHATANDUSERS=$pad_config_chatandusers"
	ynh_return "YNH_CONFIG_MAIN_PAD_CONFIGURATION_PAD_CONFIG_ALWAYSSHOWCHAT=$pad_config_alwaysshowchat"
	ynh_return "YNH_CONFIG_MAIN_PAD_CONFIGURATION_PAD_CONFIG_SHOW_MARKDOWN=$pad_config_show_markdown"
	ynh_return "YNH_CONFIG_MAIN_MYPADS_CONFIGURATION_MYPADS=$mypads"
	ynh_return "YNH_CONFIG_MAIN_MYPADS_CONFIGURATION_USELDAP=$useldap"
	ynh_return "YNH_CONFIG_MAIN_OVERWRITE_FILES_OVERWRITE_SETTINGS=$overwrite_settings"
	ynh_return "YNH_CONFIG_MAIN_OVERWRITE_FILES_OVERWRITE_CREDENTIALS=$overwrite_credentials"
	ynh_return "YNH_CONFIG_MAIN_OVERWRITE_FILES_OVERWRITE_NGINX=$overwrite_nginx"
	ynh_return "YNH_CONFIG_MAIN_OVERWRITE_FILES_OVERWRITE_SYSTEMD=$overwrite_systemd"
}

#=================================================
# MODIFY THE CONFIGURATION
#=================================================

apply_config() {

    #=================================================
    # MODIFY ETHERPAD CONFIG
    #=================================================

	restart_etherpad=0

	# Change configuration if needed
	# padOptions noColors
	if [ "$pad_config_nocolors" != "$old_pad_config_nocolors" ]
	then
		ynh_replace_string --match_string="\(\"noColors\" *: \).*," --replace_string="\1$pad_config_nocolors," --target_file="$config_file"
		ynh_app_setting_set --app=$app --key=pad_config_nocolors --value="$pad_config_nocolors"
		restart_etherpad=1
	fi

	# padOptions showLineNumbers
	if [ "$pad_config_showlinenumbers" != "$old_pad_config_showlinenumbers" ]
	then
		ynh_replace_string --match_string="\(\"showLineNumbers\" *: \).*," --replace_string="\1$pad_config_showlinenumbers," --target_file="$config_file"
		ynh_app_setting_set --app=$app --key=pad_config_showlinenumbers --value="$pad_config_showlinenumbers"
		restart_etherpad=1
	fi

	# padOptions chatAndUsers
	if [ "$pad_config_chatandusers" != "$old_pad_config_chatandusers" ]
	then
		ynh_replace_string --match_string="\(\"chatAndUsers\" *: \).*," --replace_string="\1$pad_config_chatandusers," --target_file="$config_file"
		ynh_app_setting_set --app=$app --key=pad_config_chatandusers --value="$pad_config_chatandusers"
		restart_etherpad=1
	fi

	# padOptions alwaysShowChat
	if [ "$pad_config_alwaysshowchat" != "$old_pad_config_alwaysshowchat" ]
	then
		ynh_replace_string --match_string="\(\"alwaysShowChat\" *: \).*," --replace_string="\1$pad_config_alwaysshowchat," --target_file="$config_file"
		ynh_app_setting_set --app=$app --key=pad_config_alwaysshowchat --value="$pad_config_alwaysshowchat"
		restart_etherpad=1
	fi

	# Plugin option ep_markdown_default
	if [ "$pad_config_show_markdown" != "$old_pad_config_show_markdown" ]
	then
		ynh_replace_string --match_string="\(\"ep_markdown_default\" *: \).*," --replace_string="\1$pad_config_show_markdown," --target_file="$config_file"
		ynh_app_setting_set --app=$app --key=pad_config_show_markdown --value="$pad_config_show_markdown"
		restart_etherpad=1
	fi

	# Export
	if [ "$export" != "$old_export" ]
	then
		if [ "$export" = "abiword" ]
		then
			# if Abiword isn't installed, call the action add_remove_abiword.
			if ! which abiword > /dev/null
			then
				yunohost app action run $app add_remove_abiword
			fi
			ynh_replace_string --match_string="\(\"abiword\" *: \).*," --replace_string="\1\"$(which abiword)\"," --target_file="$config_file"
			ynh_replace_string --match_string="\(\"soffice\" *: \).*," --replace_string="\1null," --target_file="$config_file"
		elif [ "$export" = "libreoffice" ]
		then
			# if LibreOffice isn't installed, call the action add_remove_libreoffice.
			if ! which soffice > /dev/null
			then
				yunohost app action run $app add_remove_libreoffice
			fi
			ynh_replace_string --match_string="\(\"abiword\" *: \).*," --replace_string="\1null," --target_file="$config_file"
			ynh_replace_string --match_string="\(\"soffice\" *: \).*," --replace_string="\1\"$(which soffice)\"," --target_file="$config_file"
		else
			ynh_replace_string --match_string="\(\"abiword\" *: \).*," --replace_string="\1null," --target_file="$config_file"
			ynh_replace_string --match_string="\(\"soffice\" *: \).*," --replace_string="\1null," --target_file="$config_file"
		fi

		ynh_app_setting_set --app=$app --key=export --value="$export"

		restart_etherpad=1
	fi

	# MyPads
	if [ "$mypads" != "$old_mypads" ]
	then
		ynh_use_nodejs
		#pushd "$final_path/src"
		#ynh_secure_remove --file="$final_path/src/package-lock.json" #added to fix saveError ENOENT: no such file or directory
		if [ "$mypads" = "1" ]
		then
			ynh_exec_as $app env "$ynh_node_load_PATH" npm install ep_mypads@${mypads_version}
		else
			ynh_exec_as $app env "$ynh_node_load_PATH" npm uninstall ep_mypads
		fi
		popd
		chown -R $app: $final_path/node_modules

		ynh_app_setting_set --app=$app --key=mypads --value="$mypads"

		restart_etherpad=1
	fi

	# LDAP for MyPads
	if [ "$useldap" != "$old_useldap" ]	
	then
		if [ "$useldap" = "1" ]
		then
			ynh_replace_string --match_string="//noldap\(.*\)" --replace_string="\1 //useldap" --target_file="$final_path/settings.json"
		else
			ynh_replace_string --match_string="\(.*\) //useldap" --replace_string="//noldap\1" --target_file="$final_path/settings.json"
		fi

		ynh_app_setting_set --app=$app --key=useldap --value="$useldap"

		restart_etherpad=1
	fi


	if [ $restart_etherpad -eq 1 ]
	then
		# Wait for etherpad to be fully started
		ynh_systemd_action --action=restart --line_match="You can access your Etherpad instance at" --log_path="/var/log/$app/etherpad.log" --timeout="120"
	fi

    #=================================================
    # MODIFY OVERWRITTING SETTINGS
    #=================================================

	# Set overwrite_settings
	ynh_app_setting_set --app=$app --key=overwrite_settings --value="$overwrite_settings"
	# Set overwrite_credentials
	ynh_app_setting_set --app=$app --key=overwrite_credentials --value="$overwrite_credentials"
	# Set overwrite_nginx
	ynh_app_setting_set --app=$app --key=overwrite_nginx --value="$overwrite_nginx"
	# Set overwrite_systemd
	ynh_app_setting_set --app=$app --key=overwrite_systemd --value="$overwrite_systemd"
}

#=================================================
# GENERIC FINALIZATION
#=================================================
ynh_app_config_run $1