#!/bin/sh













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
}

# A single-architecture package carries one core build. Refuse a router that
# needs another one instead of installing a core that cannot run there.
need=$(ezlink_router_arch "${IPKG_INSTROOT:-}")
case "$need" in
""|"aarch64")
	;;
unsupported)
	echo "luci-app-quicksilver: this router's processor is not supported." >&2
	exit 1
	;;
*)
	echo "luci-app-quicksilver: this package is for aarch64 routers, but this router needs the $need package (or the full package)." >&2
	exit 1
	;;
esac
# Login migration from the shell-era 1.x packages of llGNework and
# Quicksilver, which have this package's names. They kept the account and its
# password in plaintext UCI (<brand>.config.account and .password), in the
# /etc/config/<brand> this package replaces with its own file. preinst copies
# the login before any file is replaced; postinst logs in with it, so the user
# does not have to after the upgrade. The build concatenates this file into
# both scripts.
#
# The copy lives in /var/run, which is in memory and where only root can create
# files, and postinst deletes it before it logs in. A login that fails leaves
# the user logged out, as an upgrade did before.
#
# ezlink_stash_shell_login <brand> <file> writes the account and the password,
# one per line, to file, or removes file when there is no shell-era login.
# ezlink_login_from_stash <file> <core> deletes file and logs in with it in the
# background: the login waits on the network for up to 45 seconds.

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
		# The password goes through a pipe; only the account is on the command line.
		(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
}

# Hand a shell-era login over to postinst before the old configuration file is
# replaced (see shell-login.sh). An image build has none to take over.
[ -n "${IPKG_INSTROOT:-}" ] || ezlink_stash_shell_login quicksilver /var/run/quicksilver-shell-login
exit 0
