#!/bin/sh
[ -n "${IPKG_INSTROOT}" ] && exit 0

WEBUSER=uhttpd
id "$WEBUSER" >/dev/null 2>&1 || WEBUSER=root
mkdir -p /etc/ipman
if [ ! -f /etc/ipman/settings ]; then
	for k in iface username password http_port allow_nonlan; do
		v="$(uci -q get "ipman.settings.$k")"
		[ -n "$v" ] && echo "$k=$v" >> /etc/ipman/settings
	done
fi
touch /etc/ipman/settings
# The camera password is in here, so keep it off limits to everyone else.
chown -R "$WEBUSER" /etc/ipman 2>/dev/null
chmod 0700 /etc/ipman
chmod 0600 /etc/ipman/settings

# The store now holds the credentials, so drop the copy in UCI: that one is
# picked up by RutOS configuration backups, and a camera password does not
# belong in every backup archive.
uci -q delete ipman.settings.password && uci commit ipman

# One route shape, chosen for the firmware actually running.
#
# Shipping both shapes was the original 7.22/7.24 compatibility trick, but
# 7.24.3 normalises {?sid} and {sid} to the same route and refuses the second
# with "Add routes error: Duplicate route handler for ipman/{service_group}/{sid}".
# The first still registers so the page works, but it is an error in the log on
# current firmware, and a release that makes it fatal would break the package.
# 7.22 matches only the required-parameter form; 7.24+ wants the optional one.
PATHD=
for _d in /usr/local/usr/share/vuci/path.d /usr/share/vuci/path.d; do
	[ -f "$_d/ipman.json" ] && PATHD="$_d/ipman.json" && break
done
if [ -n "$PATHD" ]; then
	# Everything after the last underscore, which is the same rule the Package
	# Manager's own _check_package_fw uses.  Taking the text after "_R_"
	# instead reads empty on a GPL build string, and an unparsed version must
	# not silently pick the wrong shape.  Anything unrecognised falls through
	# to the current-firmware form, which is the safer default for a release
	# newer than this package knows about.
	FW=$(sed -n 's/.*_\([0-9][0-9.]*\)$/\1/p' /etc/version 2>/dev/null)
	case "$FW" in
	00.07.0*|00.07.1*|00.07.2[0-3]*)
		printf '%s\n' '[' '  {"/ipman/{service_group}/{sid}": "services/ipman"}' ']' > "$PATHD"
		;;
	*)
		printf '%s\n' '[' '  {"endpoint": "/ipman/{service_group}/{?sid}", "lua_service": "services/ipman", "backup_order": 900}' ']' > "$PATHD"
		;;
	esac
fi

# rpcd keeps serving the ACL set it read at boot, so without this a newly
# installed scope is never granted and every API call returns Unauthorized —
# with nothing in any log to explain it.
[ -x /etc/init.d/rpcd ] && ubus call session reload_acls 2>/dev/null

# Restart uhttpd — but not while the caller is still waiting on it.
#
# It has to be a full restart, not a HUP: on RutOS 7.22 a HUP does not make
# uhttpd re-read the API route table, so every endpoint answers "Endpoint not
# implemented" until something restarts it, which looks exactly like a broken
# package.  But when this install came from the WebUI's Package Manager, that
# restart kills the very HTTP connection the Package Manager is waiting on for
# its result, and it reports "Package installation failed" for an install that
# in fact succeeded — a technician sees a red error and retries a working
# install.  So detach it and let the response go out first.
(
	sleep 5
	/etc/init.d/uhttpd restart >/dev/null 2>&1 || killall -q -1 uhttpd 2>/dev/null
	# Tell the web UI to re-read its menu, so the new page appears without the
	# operator having to reload or log out.
	ubus send vuci.notify '{"event": "reload_routes"}' 2>/dev/null
) </dev/null >/dev/null 2>&1 &

exit 0
