#!/bin/bash
. /usr/syno/bin/jsoncmd

DSM56_BACKUP_FILE="backup56.json"

WEBSTATION_CONFIG_PATH="/usr/syno/etc/packages/WebStation"
WEBSTATION_JSON_FILES="WebStation.json VirtualHost.json"

PHP56_CONFIG_PATH="/usr/local/etc/php56/conf.d"
PHP56_INI_FILES="user-settings.ini extensions.ini apcu.ini opcache.ini"

restore_from_5_X() {
	local value
	local backup56_json
	backup56_json="$(cat "${importPath}"/"${DSM56_BACKUP_FILE}")"

	local isEnableLocalWeb
	isEnableLocalWeb=$(jget "${backup56_json}" ".WebServ_isEnableLocalWeb")
	if [ "${isEnableLocalWeb}" -eq "1" ]; then
		value="true"
	else
		value="false"
	fi
	sed -i "s/\(\"userdir\"\s*:\s*\).*/\1${value}/" \
		"${WEBSTATION_CONFIG_PATH}/WebStation.json"

	local isEnableDisplayErr
	isEnableDisplayErr=$(jget "${backup56_json}" ".WebServ_isEnableDisplayErr")
	if [ "${isEnableDisplayErr}" -eq "1" ]; then
		value="On"
	else
		value="Off"
	fi
	sed -i "s/^\(display_errors\s*=\s*\).*$/\1${value}/" \
		"${PHP56_CONFIG_PATH}/user-settings.ini"

	local isEnableOpenBaseDir
	local OpenBaseDir
	isEnableOpenBaseDir=$(jget "${backup56_json}" ".WebServ_isEnableOpenBaseDir")
	if [ "${isEnableOpenBaseDir}" -eq "1" ]; then
		value="yes"
		OpenBaseDir=$(jget "${backup56_json}" ".WebServ_OpenBaseDir")
	else
		value="no"
		OpenBaseDir=$(cat "/var/packages/PHP5.6/target/${PHP56_CONFIG_PATH}/user-settings.ini" | grep open_basedir)
	fi

	sed -i "s/^\(php_openbasedir_customize\s*=\s*\).*$/\1\"${value}\"/" \
		"/etc/synoinfo.conf"
	sed -i "s,^\(open_basedir\s*=\s*\).*$,\1$OpenBaseDir," \
		"${PHP56_CONFIG_PATH}/user-settings.ini"

	local isEnablePHPCache
	isEnablePHPCache=$(jget "${backup56_json}" ".WebServ_isEnablePHPCache")
	if [ "${isEnablePHPCache}" -eq "1" ]; then
		/bin/cp -f "/var/packages/PHP5.6/target/${PHP56_CONFIG_PATH}/opcache.ini" \
			"${PHP56_CONFIG_PATH}/opcache.ini"
		/bin/cp -f "/var/packages/PHP5.6/target/${PHP56_CONFIG_PATH}/apcu.ini" \
			"${PHP56_CONFIG_PATH}/apcu.ini"
	else
		/bin/rm "${PHP56_CONFIG_PATH}/opcache.ini"
		/bin/rm "${PHP56_CONFIG_PATH}/apcu.ini"
	fi

	jdone
	exit 0
}

restore_from_6_X() {
	usercollection_json="$(cat "${importPath}/usercollection.json")"

	# restore openbase_dir settings in synoinfo.conf
	isEnableOpenBaseDir=$(jget "${usercollection_json}" ".php.custom_openbasedir")
	local value
	if [ "${isEnableOpenBaseDir}" == "true" ]; then
		value="yes"
	else
		value="no"
	fi
	sed -i "s/^\(php_openbasedir_customize\s*=\s*\).*$/\1\"${value}\"/" \
		"/etc/synoinfo.conf"

	# restore webstation config file
	for file in ${WEBSTATION_JSON_FILES}; do
		/bin/cp -f "${importPath}/${file}" "${WEBSTATION_CONFIG_PATH}/${file}"
	done

	# restore php settings
	for file in ${PHP56_INI_FILES}; do
		/bin/cp -f "${importPath}/${file}" "${PHP56_CONFIG_PATH}/${file}"
	done

	jdone
	exit 0
}

importPath=$(jget "${SYNOPKG_BKP_INPUT}" ".temp_path")
if [ $? -ne 0 ];then
	msg="error happened"
	error=$(printf "%s(%s)" "${msg}" "0x02")
	jerr "$error"
	exit 1
fi

if [ -s "${importPath}/${DSM56_BACKUP_FILE}" ]; then
	restore_from_5_X
else
	restore_from_6_X
fi
