#!/bin/sh

















ezlink_stash_shell_login() {
	rm -f "$2"
	command -v uci >/dev/null 2>&1 || return 0
	ezlink_account=$(uci -q get "$1.config.account")
	ezlink_password=$(uci -q get "$1.config.password")
	if [ -n "$ezlink_account" ] && [ -n "$ezlink_password" ]; then
		(umask 077 && printf '%s\n%s\n' "$ezlink_account" "$ezlink_password" > "$2")
	fi
	ezlink_account=
	ezlink_password=
	return 0
}

ezlink_login_from_stash() {
	if [ ! -f "$1" ] || [ -L "$1" ]; then
		return 0
	fi
	ezlink_account=$(sed -n '1p' "$1")
	ezlink_password=$(sed '1d' "$1")
	rm -f "$1"
	if [ -n "$ezlink_account" ] && [ -n "$ezlink_password" ] && [ -x "$2" ]; then

		(printf '%s' "$ezlink_password" | "$2" login --email "$ezlink_account" --password-stdin >/dev/null 2>&1 &) >/dev/null 2>&1
	fi
	ezlink_account=
	ezlink_password=
	return 0
}













ezlink_elf_byte() {
	dd if="$1" bs=1 skip="$2" count=1 2>/dev/null
}

ezlink_arm_level() {
	distrib=$(sed -n "s/^DISTRIB_ARCH=['\"]*\([^'\"]*\).*/\1/p" "$1/etc/openwrt_release" 2>/dev/null)
	case "$distrib" in
	arm_cortex-a*) echo armv7; return ;;
	arm_*) echo unsupported; return ;;
	esac
	if [ -z "$1" ]; then
		case "$(uname -m 2>/dev/null)" in
		armv4*|armv5*|armv6*) echo unsupported; return ;;
		esac
	fi
	echo armv7
}

ezlink_router_arch() {
	root="${1:-}"
	elf=""
	for candidate in "$root/bin/busybox" "$root/bin/sh"; do
		if [ "$(ezlink_elf_byte "$candidate" 1)$(ezlink_elf_byte "$candidate" 2)$(ezlink_elf_byte "$candidate" 3)" = "ELF" ]; then
			elf="$candidate"
			break
		fi
	done
	if [ -z "$elf" ]; then
		[ -z "$root" ] || return 0
		case "$(uname -m 2>/dev/null)" in
		x86_64) echo x86_64 ;;
		aarch64) echo aarch64 ;;
		armv7*|armv8*) echo armv7 ;;
		mips*|"") ;;
		*) echo unsupported ;;
		esac
		return 0
	fi
	class=$(ezlink_elf_byte "$elf" 4)
	order=$(ezlink_elf_byte "$elf" 5)
	if [ "$order" = "$(printf '\001')" ]; then
		machine=$(ezlink_elf_byte "$elf" 18)
	elif [ "$order" = "$(printf '\002')" ]; then
		machine=$(ezlink_elf_byte "$elf" 19)
	else
		return 0
	fi
	if [ "$machine" = "$(printf '\076')" ] && [ "$class" = "$(printf '\002')" ]; then
		echo x86_64
	elif [ "$machine" = "$(printf '\267')" ] && [ "$class" = "$(printf '\002')" ]; then
		echo aarch64
	elif [ "$machine" = "$(printf '\050')" ] && [ "$class" = "$(printf '\001')" ] && [ "$order" = "$(printf '\001')" ]; then
		ezlink_arm_level "$root"
	elif [ "$machine" = "$(printf '\010')" ] && [ "$class" = "$(printf '\001')" ] && [ "$order" = "$(printf '\001')" ]; then
		echo mipsel
	else
		echo unsupported
	fi
}

# Full package: keep only the core this router needs. It replaces the
# dispatcher at /usr/sbin, so nothing has to be detected on every call, and the
# other cores no longer take up flash. When the router cannot be identified the
# dispatcher stays and decides at run time.
ezlink_root="${IPKG_INSTROOT:-}"
ezlink_cores="$ezlink_root/usr/lib/ezlink/quicksilver/cores"
ezlink_need=$(ezlink_router_arch "$ezlink_root")
case "$ezlink_need" in
x86_64|aarch64|armv7|mipsel)
	if [ -f "$ezlink_cores/$ezlink_need" ]; then
		chmod 0755 "$ezlink_cores/$ezlink_need"
		mv -f "$ezlink_cores/$ezlink_need" "$ezlink_root/usr/sbin/quicksilvercore" && rm -rf "$ezlink_cores"
	fi
	;;
unsupported)
	echo "luci-app-quicksilver-full: this router's processor is not supported." >&2
	;;
esac

if [ -n "$IPKG_INSTROOT" ]; then
	# Image build: enable the service in the target rootfs so it starts on
	# first boot. The links match START=99 and STOP=10 of the init script.
	mkdir -p "$IPKG_INSTROOT/etc/rc.d"
	ln -sf ../init.d/quicksilver "$IPKG_INSTROOT/etc/rc.d/S99quicksilver"
	ln -sf ../init.d/quicksilver "$IPKG_INSTROOT/etc/rc.d/K10quicksilver"
	exit 0
fi

# LuCI keeps a compiled menu index in /tmp. Remove the old one after an
# install or upgrade so the brand entry is visible on the next page load.
# OpenWrt 21.02 names it without a suffix.
rm -f /tmp/luci-indexcache /tmp/luci-indexcache.*
rm -rf /tmp/luci-modulecache

# llGNework and Quicksilver 1.x were shell packages with the same names, and an
# upgrade leaves their data in /etc/quicksilver: the session token, the
# subscription and the generated configuration in plaintext, with every node's
# credentials, plus their own kernel, rule data and dashboard (66 MB on a test
# router). This package seals what it keeps and never reads these files, and
# keep.d would carry them into every sysupgrade backup. The old prerm has
# already stopped that service and undone its DNS and firewall changes; a DNS
# takeover it failed to undo is repaired by the daemon. None of the names below
# is used by this package.
for name in core run token domain servers.json userinfo.json subscription.yaml last_sync core_version dnsmasq.backup; do
	rm -rf "/etc/quicksilver/$name"
done

# The control panel was removed from this package, so the shared copy of it in
# /usr/share/ezlink/zashboard is dead weight. An older package of another brand
# still looks for it before it starts the proxy, so it goes only when no other
# brand is installed; this brand's own plugin does not count. root is empty on a
# live router.
ezlink_drop_dashboard() {
	for ezlink_rpc in "$2"/usr/libexec/rpcd/ezlink.*; do
		[ -e "$ezlink_rpc" ] || continue
		[ "$ezlink_rpc" = "$2/usr/libexec/rpcd/ezlink.$1" ] && continue
		return 0
	done
	rm -rf "$2/usr/share/ezlink/zashboard"
}
ezlink_drop_dashboard quicksilver ""

# Kernel logs written before this version hid node addresses only partly.
rm -f /tmp/quicksilver/kernel.log /tmp/quicksilver/kernel.log.1

/etc/init.d/quicksilver enable >/dev/null 2>&1 || true
# On an upgrade the old daemon may still be running the replaced binary, and
# procd keeps an instance whose command line did not change. Restart it so the
# new core takes over; its stop keeps the intent and init resumes the proxy.
if /etc/init.d/quicksilver running >/dev/null 2>&1; then
	/etc/init.d/quicksilver restart >/dev/null 2>&1 || true
else
	/etc/init.d/quicksilver start >/dev/null 2>&1 || true
fi

# Log in with the account a shell-era package kept, handed over by preinst.
ezlink_login_from_stash /var/run/quicksilver-shell-login /usr/sbin/quicksilvercore

# Reload rpcd so the brand-specific login object is available immediately.
# A reload preserves the current LuCI session on supported OpenWrt releases.
[ -x /etc/init.d/rpcd ] && /etc/init.d/rpcd reload >/dev/null 2>&1 || true

# iStoreOS may keep LuCI's compiled menu tree in the web-service process.
# Reloading uhttpd is configuration-free and lets the currently signed-in
# user see the new entry with a normal browser refresh.
[ -x /etc/init.d/uhttpd ] && /etc/init.d/uhttpd reload >/dev/null 2>&1 || true
exit 0
