#!/system/bin/sh

MODDIR=${0%/*}
# shellcheck disable=SC1090
. "$MODDIR/common.sh"

json_escape() {
    printf '%s' "$1" | sed \
        -e 's/\\/\\\\/g' \
        -e 's/"/\\"/g' \
        -e ':a;N;$!ba;s/\n/\\n/g' \
        -e 's/\r/\\r/g' \
        -e 's/\t/\\t/g'
}

prop_value() {
    key="$1"
    sed -n "s/^${key}=//p" "$MODDIR/module.prop" 2>/dev/null | head -n 1
}

tail_text() {
    file="$1"
    lines="${2:-40}"
    if [ -f "$file" ]; then
        tail -n "$lines" "$file" 2>/dev/null \
            | tr -d '\000' \
            | awk '{ gsub(/[\001-\010\013\014\016-\037\177]/, ""); print }'
    fi
}

resolve_actual_daemon_log_file() {
    if [ -d "$RUNTIME_DIR" ]; then
        latest_file="$(find "$RUNTIME_LOG_DIR" -maxdepth 1 -type f -name 'cloudphone_*.log' 2>/dev/null | sort | tail -n 1)"
        if [ -n "$latest_file" ]; then
            printf '%s' "$latest_file"
            return 0
        fi
    fi
    if [ -f "$DAEMON_LOG_FILE" ]; then
        printf '%s' "$DAEMON_LOG_FILE"
        return 0
    fi
    printf '%s' "$DAEMON_LOG_FILE"
}

load_config

module_id="$(prop_value id)"
module_name="$(prop_value name)"
module_version="$(prop_value version)"
module_version_code="$(prop_value versionCode)"
module_author="$(prop_value author)"
module_description="$(prop_value description)"

device_model="$(getprop_value ro.product.model)"
device_brand="$(getprop_value ro.product.brand)"
device_name="$(getprop_value ro.product.device)"
android_version="$(getprop_value ro.build.version.release)"
sdk_int="$(getprop_value ro.build.version.sdk)"
device_abi="$(getprop_value ro.product.cpu.abi)"
device_id="$(resolve_device_id)"

root_manager="Unknown"
if command -v magisk >/dev/null 2>&1; then
    root_manager="Magisk"
elif [ -x /data/adb/ksu/bin/ksud ] || command -v ksud >/dev/null 2>&1; then
    root_manager="KernelSU"
elif [ -x /data/adb/ap/bin/apd ] || command -v apd >/dev/null 2>&1; then
    root_manager="APatch"
fi

running="false"
pid=""
if [ -f "$PID_FILE" ]; then
    pid="$(cat "$PID_FILE" 2>/dev/null)"
    if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
        running="true"
    fi
fi

if [ "$running" != "true" ]; then
    pid="$(pidof cloudphone_daemon 2>/dev/null | awk '{print $1}')"
    if [ -n "$pid" ]; then
        running="true"
    fi
fi

ws_url_masked="$WS_URL"
ws_url_masked="$(printf '%s' "$ws_url_masked" | sed -E 's/(credentials=)[^&]+/\1***/g')"

ACTUAL_DAEMON_LOG_FILE="$(resolve_actual_daemon_log_file)"
service_log_tail="$(tail_text "$SERVICE_LOG_FILE" 30)"
daemon_log_tail="$(tail_text "$ACTUAL_DAEMON_LOG_FILE" 30)"

printf '{'
printf '"module":{"id":"%s","name":"%s","version":"%s","versionCode":"%s","author":"%s","description":"%s"},' \
    "$(json_escape "$module_id")" \
    "$(json_escape "$module_name")" \
    "$(json_escape "$module_version")" \
    "$(json_escape "$module_version_code")" \
    "$(json_escape "$module_author")" \
    "$(json_escape "$module_description")"
printf '"device":{"model":"%s","brand":"%s","device":"%s","android":"%s","sdk":"%s","abi":"%s","deviceId":"%s","rootManager":"%s"},' \
    "$(json_escape "$device_model")" \
    "$(json_escape "$device_brand")" \
    "$(json_escape "$device_name")" \
    "$(json_escape "$android_version")" \
    "$(json_escape "$sdk_int")" \
    "$(json_escape "$device_abi")" \
    "$(json_escape "$device_id")" \
    "$(json_escape "$root_manager")"
printf '"daemon":{"running":%s,"pid":"%s","runtimeDir":"%s","sockFile":"%s","pidFile":"%s","daemonLogFile":"%s","serviceLogFile":"%s"},' \
    "$running" \
    "$(json_escape "$pid")" \
    "$(json_escape "$RUNTIME_DIR")" \
    "$(json_escape "$SOCK_FILE")" \
    "$(json_escape "$PID_FILE")" \
    "$(json_escape "$ACTUAL_DAEMON_LOG_FILE")" \
    "$(json_escape "$SERVICE_LOG_FILE")"
printf '"config":{"wsUrl":"%s","heartbeatSec":"%s","frameWireProtocol":"%s","recordingCodec":"%s","recordingProfile":"%s","recordingBitrateKbps":"%s","recordingSize":"%s","recordingTimeLimitSec":"%s","bootDelaySec":"%s","wsDump":"%s","wsNoHostVerify":"%s"},' \
    "$(json_escape "$ws_url_masked")" \
    "$(json_escape "$HEARTBEAT_SEC")" \
    "$(json_escape "$FRAME_WIRE_PROTOCOL")" \
    "$(json_escape "$RECORDING_CODEC")" \
    "$(json_escape "$RECORDING_PROFILE")" \
    "$(json_escape "$RECORDING_BITRATE_KBPS")" \
    "$(json_escape "$RECORDING_SIZE")" \
    "$(json_escape "$RECORDING_TIME_LIMIT_SEC")" \
    "$(json_escape "$BOOT_DELAY_SEC")" \
    "$(json_escape "$WS_DUMP")" \
    "$(json_escape "$WS_NO_HOST_VERIFY")"
printf '"logs":{"serviceTail":"%s","daemonTail":"%s"}' \
    "$(json_escape "$service_log_tail")" \
    "$(json_escape "$daemon_log_tail")"
printf '}\n'
