#!/bin/bash

PKGDEST=$(realpath "${SYNOPKG_PKGDEST}")
PKG_CENTER_LOCK=/run/synosdk/lock/lock_pkg
WEB_STATION_LOCK=/run/webstation_install_lock

PHP56_CONF_DIR="/usr/local/etc/php56/conf.d"
PHPSETTINGS_JSON="/usr/syno/etc/packages/WebStation/PHPSettings.json"

safe_install_package()
{
	# NOTE: Workaround of package center 3 minutes limitation.
	# Try to acquire flock first, then execute synopkg.
	PKGNAME=$1
	for i in $(/usr/bin/seq 5); do
		/usr/bin/flock -w 3600 -x "$PKG_CENTER_LOCK" -c "" && /usr/bin/flock -w 3600 -x "$WEB_STATION_LOCK" -c "/usr/syno/bin/synopkg install ${PKGDEST}/misc/${PKGNAME}.spk && /usr/syno/bin/synopkg start ${PKGNAME}"
		if [ $? -eq 0 ]; then
			break
		fi
	done
}

updater_0056()
{
	echo "0056 Updater..."
	/usr/bin/rm -f /usr/local/etc/httpd/sites-enabled/httpd-vhost.conf-user
	/usr/bin/cp "$PKGDEST"/misc/apache22-compatible.conf /usr/local/etc/httpd/sites-enabled/webstation-default.conf

	safe_install_package Apache2.2 &
	"${PKGDEST}"/tools/synowebstation --migrate 0056 "$PHP56_CONF_DIR" "$PHPSETTINGS_JSON"
}

updater_0103()
{
	echo "0103 Updater..."
	local cert_files=$(/usr/bin/find /usr/local/etc/certificate/WebStation/ -name "*.pem")
	for file in $cert_files; do
		/usr/bin/chown root:root "$file"
		/usr/bin/chmod 400 "$file"
	done
}

updater_0128()
{
	echo "0128 Updater..."
	"${PKGDEST}"/tools/synowebstation --migrate 0128
}


updater()
{
	local updater_file="$1"
	local old_version=""

	# upgrade
	if [ -s "$updater_file" ]; then
		old_version=$(<"$updater_file")
	else # uninstall then install again
		# before 0056, need to clean user-settings.ini
		local user_settings_ini="user-settings.ini"
		local is_from_0056=false

		if [ -s "$PHP56_CONF_DIR/$user_settings_ini" ]; then
			/usr/bin/rm "$PHP56_CONF_DIR/$user_settings_ini"
			is_from_0056=true
		fi

		if ! $is_from_0056 && [ -s "$PHPSETTINGS_JSON" ]; then
			old_version="0103"
		fi
	fi

	local avaliable_updaters="0056 0103 0128"

	for updater_ver in $avaliable_updaters; do
		if [ -n "$old_version" ] && [ "$old_version" -le "$updater_ver" ]; then
			updater_"$updater_ver"
		fi
	done

	/usr/bin/rm -f "$updater_file"
}

updater /usr/syno/etc/packages/WebStation/webstation_updater_info

# Force Upgrae PHP5.6 when installing WebStation 2.0.
if [ -d /var/packages/PHP5.6 ]; then
	current_php56_version=$(/usr/syno/bin/synopkg version PHP5.6 | /usr/bin/awk -F '-' '{print $2}')
	if [ $current_php56_version -lt 0051 ]; then
		safe_install_package PHP5.6 &

		for f in user-settings.ini apcu.ini opcache.ini extensions.ini;
		do
			ini_file="$PHP56_CONF_DIR/$f"
			if [ -f "${ini_file}" ]; then
				/usr/bin/rm  "${ini_file}"
			fi
		done
	fi
fi

# Updator to and after WebStation 3.0
"${PKGDEST}"/tools/synowebstation --migrate_ex

if [ ! -s /usr/syno/etc/packages/WebStation/WebStation.json ]; then
	/usr/bin/cp -f "$PKGDEST/misc/WebStation.json" /usr/syno/etc/packages/WebStation/WebStation.json
fi

if [ ! -s /usr/syno/etc/packages/WebStation/PHPSettings.json ]; then
	/usr/bin/cp -f "$PKGDEST/misc/PHPSettings.json" /usr/syno/etc/packages/WebStation/PHPSettings.json
fi

if [ ! -d "$PKGDEST"/etc/suphp ]; then
	/usr/bin/mkdir -pm755 "$PKGDEST"/etc/suphp
fi

if [ ! -d /usr/syno/etc/packages/WebStation/php_profile ]; then
	/usr/bin/mkdir -pm755 /usr/syno/etc/packages/WebStation/php_profile
fi

for version in 56 70; do
	if [ -s "/usr/local/etc/php${version}/conf.d/webstation-settings.ini" ]; then
		/usr/bin/rm "/usr/local/etc/php${version}/conf.d/webstation-settings.ini"
	fi
	if [ -s "/usr/local/etc/php${version}/conf.d/webstation-vhost.ini" ]; then
		/usr/bin/rm "/usr/local/etc/php${version}/conf.d/webstation-vhost.ini"
	fi

	webstation_etc="/usr/syno/etc/packages/WebStation"
	if [ ! -d "${webstation_etc}/php${version}/conf.d" ]; then
		/usr/bin/mkdir -pm755 "${webstation_etc}/php${version}/conf.d"
	fi

	if [ ! -s "${webstation_etc}/php${version}/php.ini" ]; then
		/usr/bin/cp -f "$PKGDEST/misc/php${version}.ini" "${webstation_etc}/php${version}/php.ini"
	fi
done

if [ -d "/usr/syno/numanod/conf" ]; then
	/usr/bin/cp -f "$PKGDEST/misc/conf_webstation_php" "/usr/syno/numanod/conf/conf_webstation_php"
	/usr/bin/chmod +x "/usr/syno/numanod/conf/conf_webstation_php"
fi

exit 0
