#!/bin/sh
MODPATH=${0%/*}
PATH=$PATH:/data/adb/ap/bin:/data/adb/magisk:/data/adb/ksu/bin

# log
exec 2> $MODPATH/logs/utils.log
set -x

function check_android_server_is_up() {
    [ ! -z "$1" ] && timeout="$1" || timeout=4
    counter=0

    while [ $counter -lt $timeout ]; do
        local result="$(busybox pgrep 'android_server')"
        if [ $result -gt 0 ]; then
            echo "[-] IDA Pro android_server is running... ✅"
            string="description=Automatically deploys and manages IDA Pro Android server for remote debugging and reverse engineering on Android devices: ✅ (active)"
            break
        else
            echo "[-] Checking IDA Pro android_server status: $counter"
            counter=$((counter + 1))
        fi
        sleep 1.5
    done

    if [ $counter -ge $timeout ]; then
        string="description=Automatically deploys and manages IDA Pro Android server for remote debugging and reverse engineering on Android devices: ❌ (failed)"
    fi

    sed -i "s/^description=.*/$string/g" $MODPATH/module.prop
}

function android_server() {
    echo "[-] Starting IDA Pro android_server..."
    /system/bin/android_server &
    sleep 2
}

wait_for_boot() {
  while true; do
    local result="$(getprop sys.boot_completed)"
    if [ $? -ne 0 ]; then
      exit 1
    elif [ "$result" = "1" ]; then
      break
    fi
    sleep 3
  done
}

#EOF
