#!/system/bin/sh
# panel-diag.sh - on-panel diagnostics for the Savekey CM-001 (Android 5.1.1).
# Uses only mksh + toolbox commands (no grep/find/head on this Android version).
#
# Run inside Terminal Emulator on the panel:
#   sh /sdcard/panel-diag.sh           read-only report + copies of the Savekey APKs
#   sh /sdcard/panel-diag.sh appdata   also copies Savekey app settings/databases (root only;
#                                      may contain the Savekey account token - keep private)
# Output: /sdcard/panel-diag/  -> copy that folder to a USB stick with the File Manager.

OUT=/sdcard/panel-diag
R=$OUT/report.txt
mkdir $OUT 2>/dev/null
mkdir $OUT/apk 2>/dev/null
: > $R

sec() { echo "" >> $R; echo "===== $* =====" >> $R; }
run() { sec "$*"; "$@" >> $R 2>&1; }
say() { echo ">> $*"; }

# Savekey / RS485 / launcher candidates; skips stock Android and the known 3rd-party apps
interesting() {
    typeset -l s="$1"
    case "$s" in
        com.android.*|com.google.*|android|com.svox.*|jp.co.omronsoft.*) return 1 ;;
        *ucmobile*|com.uc.*|*alphainventor*|*easyvoicerecorder*|jackpal.*) return 1 ;;
    esac
    return 0
}

say "1/6 device info"
run id
run uname -a
run cat /proc/version
run cat /proc/cpuinfo
run getprop
run ls -l /system/bin/su /system/xbin/su /sbin/su /system/xbin/busybox /system/bin/busybox
run cat /proc/mounts

say "2/6 packages"
pm list packages -f > $OUT/packages.txt 2>&1
pm list packages -3 > $OUT/packages-3rdparty.txt 2>&1
sec "pm list packages -f"; cat $OUT/packages.txt >> $R

say "3/6 serial ports and devices"
run ls -l /dev
run cat /proc/tty/drivers

say "4/6 processes and network (port -> app mapping)"
ps > $OUT/ps.txt 2>&1
sec "ps"; cat $OUT/ps.txt >> $R
run cat /proc/net/tcp
run cat /proc/net/tcp6
run cat /proc/net/udp
run cat /proc/net/udp6
run netstat -an
run ip addr
run ip route
run ls -lR /sdcard/

say "5/6 copying APKs of Savekey/system-vendor apps"
while read line; do
    line=${line#package:}
    apk=${line%=*}
    pkg=${line##*=}
    interesting "$pkg" || continue
    sec "apk $pkg"
    ls -l "$apk" >> $R 2>&1
    cat "$apk" > "$OUT/apk/$pkg.apk" 2>> $R
done < $OUT/packages.txt

say "6/6 root checks"
SU=""
for s in /system/xbin/su /system/bin/su /sbin/su; do
    [ -x $s ] && SU=$s
done
ROOT=0
if [ -n "$SU" ]; then
    say "found $SU - if a Superuser dialog appears on screen, press Allow"
    case "$($SU -c id 2>&1)" in
        *uid=0*) ROOT=1 ;;
    esac
fi
sec "root: su=$SU root=$ROOT"; echo "su=$SU root=$ROOT" >> $R

if [ $ROOT = 1 ]; then
    say "root OK - collecting versions and open files of Savekey processes"
    while read line; do
        line=${line#package:}
        pkg=${line##*=}
        interesting "$pkg" || continue
        $SU -c "dumpsys package $pkg" > "$OUT/pkg_$pkg.txt" 2>&1
    done < $OUT/packages.txt

    while read user pid ppid vsz rss wchan pc state name; do
        [ -n "$name" ] || name=$state    # some toolbox builds print no state column
        interesting "$name" || continue
        case "$name" in *.*) ;; *) continue ;; esac
        sec "open files of $name (pid $pid, user $user)"
        $SU -c "ls -l /proc/$pid/fd" >> $R 2>&1
    done < $OUT/ps.txt

    if [ "$1" = "appdata" ]; then
        say "copying app settings/databases"
        while read line; do
            line=${line#package:}
            pkg=${line##*=}
            interesting "$pkg" || continue
            mkdir "$OUT/data_$pkg" 2>/dev/null
            for d in shared_prefs databases files; do
                for f in $($SU -c "ls /data/data/$pkg/$d" 2>/dev/null); do
                    $SU -c "cat /data/data/$pkg/$d/$f" > "$OUT/data_$pkg/${d}__$f" 2>/dev/null
                done
            done
        done < $OUT/packages.txt
    fi

    echo ""
    echo "=== Now do these actions on the PANEL and WALL SWITCH (10 s each) ==="
    M=$OUT/markers.txt
    : > $M
    for step in "PANEL app: turn ONE lamp ON" "PANEL app: turn the SAME lamp OFF" \
                "WALL switch: press Lamp1 (ON)" "WALL switch: press Lamp1 again (OFF)" \
                "WALL switch: press the smiley SCENE" "WALL switch: press the door SCENE"; do
        echo "$(date +%H:%M:%S)  $step" >> $M
        echo ">>> NOW: $step"
        sleep 10
    done
    $SU -c "logcat -d -v time" > $OUT/logcat.txt 2>&1
    say "logcat saved"
else
    say "no root: skipped versions, open files and logcat (still useful without them)"
fi

echo ""
say "DONE. Copy the folder $OUT to a USB stick (File Manager) and put it in D:\\BMS"
