#!/bin/sh

_root="${IPKG_INSTROOT:-}"
_map_live="$_root/lib/netifd/proto/map.sh"
_map_backup="${_map_live}.old"
_luci_dslite="$_root/www/luci-static/resources/protocol/dslite.js"
_luci_dslite_backup="${_luci_dslite}.old"

# This commercial upload IPK supports a fresh installation only. A retained
# platform backup means Auto-IPoE is already installed or removal did not finish.
if [ -e "$_map_backup" ] || [ -L "$_map_backup" ]; then
	echo "error: Auto-IPoE IPK replacement is unsupported; remove the installed package first" >&2
	exit 1
fi

[ -f "$_map_live" ] && [ ! -L "$_map_live" ] || {
	echo "error: platform MAP protocol is unavailable or unsafe" >&2
	exit 1
}

# Preserve the QSDK 12.2 LuCI protocol file before opkg checks the package
# payload. This restores the established standalone-IPK install behavior.
if [ -f "$_luci_dslite" ] && ! mv "$_luci_dslite" "$_luci_dslite_backup"; then
	echo "error: could not preserve the platform LuCI DS-Lite protocol" >&2
	exit 1
fi

exit 0
