#!/bin/sh
# Which device this is: f50, u30air or v50. boot/init writes it to /run/mu300/device; a boot image from before that
# (an older release's or a mainline kernel) does not, so then the device tree tells, the way init decides:
# only the U30 Air's has the charger policy service. (MU300_SYSROOT is for tests/: a fake / to read from.)
# The ZTE V50 (MU3351; experimental, docs/FINDINGS.md 95) is told by its device tree whatever the label says: its LED
# controller, an AW9523B at 0x5b on I2C3, is in no F50's or U30 Air's (kano's reading of a V50, 2026-10-14). A V50
# that an older installer labelled f50 is a v50 here; a v50 label on a board without that node is not believed. A
# U30 Air (the charger policy service) is never a V50, and an F50 or U30 Air answers as it always did.
#   mu300-device          f50, u30air or v50
#   mu300-device --json   one line of JSON for programs (the web installer, a GUI: "adb shell mu300-device --json"):
#                         the device, the system that runs, its release, the kernel, whether the caller is root and
#                         what adb does. The fields are a contract: docs/ADB-INTERFACE.md. New fields may be added;
#                         "mu300" (the version of this format) changes when one is taken away or changes meaning.
R=${MU300_SYSROOT:-}
DT=$R/proc/device-tree
# --- v50 begin (the same rule in boot/init; tests/test_device_scripts.py holds the two to it)
v50_dt() {  # the V50's AW9523B node; its parent is soc:ap-ahb or ap-ahb, by kernel
    for _n in "$DT"/soc/*/i2c@2270000/aw9523b_led@5b; do [ -d "$_n" ] && return 0; done
    return 1
}
v50_fix() {  # v50_fix LABEL: the label, unless the device tree says otherwise about a V50
    case $1 in
        f50) if [ ! -d "$DT/charger_policy_service" ] && v50_dt; then echo v50; else echo f50; fi ;;
        v50) if [ ! -d "$DT" ] || { [ ! -d "$DT/charger_policy_service" ] && v50_dt; }; then echo v50
             elif [ -d "$DT/charger_policy_service" ]; then echo u30air; else echo f50; fi ;;
        *) echo "$1" ;;
    esac
}
# --- v50 end
device() {
    if [ -s "$R/run/mu300/device" ]; then
        v50_fix "$(cat "$R/run/mu300/device")"
    elif [ -d "$DT/charger_policy_service" ]; then
        echo u30air
    else
        v50_fix f50
    fi
}
[ "${1:-}" = --json ] || { device; exit 0; }

DISK=${MU300_DISK:-/mnt/mu300-disk}
BIN=${MU300_BIN:-/opt/mu300/bin}
# a value as it goes between two quotes: nothing that JSON would want escaped
clean() { tr -cd 'A-Za-z0-9._+-' | cut -c1-64; }
# the system that runs: the directory of the Linux partition that is / (ubuntu, openwrt, openwrt-luci, or one of the
# user's own; between "mu300-update apply" and the reboot / is the kept copy, NAME.old), else what / looks like
system() {
    for d in "$DISK"/*/; do
        d=${d%/}
        [ -d "$d/etc" ] && [ "${R:-/}" -ef "$d" ] || continue
        n=${d##*/}; echo "${n%.old}"; return
    done
    if [ ! -e "$R/etc/openwrt_release" ]; then echo ubuntu
    elif [ -d "$R/www/luci-static" ]; then echo openwrt-luci
    else echo openwrt; fi
}
# off, unavailable (no adbd or no FunctionFS in this system), open (every computer gets in) or keys
adb=$("$BIN/mu300-adbd" state 2>/dev/null)
case $adb in off|open|keys) ;; *) adb=unavailable ;; esac
[ "$(id -u)" = 0 ] && root=true || root=false
release=$(cat "$R/etc/mu300/image-version" 2>/dev/null | clean)
printf '{"mu300":1,"device":"%s","system":"%s","version_id":"%s","release":"%s","kernel":"%s","root":%s,"adb":"%s"}\n' \
    "$(device | clean)" "$(system | clean)" \
    "$(sed -n 's/^VERSION_ID="\{0,1\}\([^"]*\)"\{0,1\}$/\1/p' "$R/etc/os-release" 2>/dev/null | head -n1 | clean)" \
    "${release:-unknown}" "$(uname -r | clean)" "$root" "$adb"
