#!/bin/sh

case "${1:-remove}" in
	remove|purge)
		if [ -z "$IPKG_INSTROOT" ]; then
			if ! command -v lua >/dev/null 2>&1; then
				echo "error: Auto-IPoE lifecycle cleanup is unavailable" >&2
				exit 1
			fi
			lua /usr/lib/lua/mapv6.lua -disable || {
				echo "error: Auto-IPoE lifecycle cleanup failed" >&2
				exit 1
			}
			/etc/init.d/auto-ipoe-cbt-monitor disable >/dev/null 2>&1 || true
			/usr/libexec/auto-ipoe-cbt-monitor restore || {
				echo "error: Auto-IPoE connectivity monitor cleanup failed" >&2
				exit 1
			}
		fi
		;;
	*)
		echo "error: Auto-IPoE IPK upgrade is unsupported; remove the installed package first" >&2
		exit 1
		;;
esac

exit 0
