#!/system/bin/sh
# Driver Manager - Installation Script

SKIPUNZIP=0

ui_print "================================================"
ui_print " Driver Manager v1.0.0"
ui_print "================================================"
ui_print ""

DEVICE=$(getprop ro.product.device)
MODEL=$(getprop ro.product.model)
SOC=$(getprop ro.soc.model)
PLATFORM=$(getprop ro.board.platform)
GPU=$(getprop ro.hardware.egl)
API=$(getprop ro.build.version.sdk)

ui_print "- Device: $MODEL ($DEVICE)"
ui_print "- SoC: $SOC ($PLATFORM)"
ui_print "- GPU: $GPU"
ui_print "- API: $API"
ui_print ""

# Detect WiFi chip
WIFI_MOD=$(ls /sys/module/ 2>/dev/null | grep -iE "^bcmdhd" | head -1)
ui_print "- WiFi module: ${WIFI_MOD:-unknown}"

# Detect Bluetooth
BT_MOD=$(ls /sys/module/ 2>/dev/null | grep -iE "^(btqca|btusb|btbcm|hci)" | head -1)
ui_print "- BT module: ${BT_MOD:-unknown}"

ui_print ""

# Create default config
mkdir -p "$MODPATH/config"
echo "performance" > "$MODPATH/config/gpu_mode"
echo "standard" > "$MODPATH/config/wifi_mode"
echo "standard" > "$MODPATH/config/bt_mode"
echo "sdr" > "$MODPATH/config/sdr_mode"
echo "auto" > "$MODPATH/config/gamepad_mode"
echo "off" > "$MODPATH/config/decoder_mode"
echo "0" > "$MODPATH/config/spoof_enabled"
echo "0" > "$MODPATH/config/boot_timing"
echo "100.0M" > "$MODPATH/config/fm_freq"
echo "24M:1800M" > "$MODPATH/config/spectrum_range"

# Create modules directory for .ko files
mkdir -p "$MODPATH/modules"
mkdir -p "$MODPATH/firmware"

# Install rtl_tcp_andro APK if built
RTL_APK="$MODPATH/apk/rtl_tcp_andro.apk"
if [ -f "$RTL_APK" ]; then
    pm install -r "$RTL_APK" 2>/dev/null
    if [ $? -eq 0 ]; then
        ui_print "- RTL-SDR + HackRF driver app installed"
    else
        ui_print "! Failed to install RTL-SDR driver app"
        ui_print "! Install manually from apk/ directory"
    fi
else
    ui_print "- RTL-SDR driver app not built yet"
    ui_print "  Build with: cd tools/rtl_tcp_andro && ./gradlew assembleRelease"
fi

ui_print "- Default config written"
ui_print "- Kernel module dir: modules/"
ui_print "- Firmware dir: firmware/"
ui_print ""

# Check for kernel module support
if [ -d "/proc/modules" ]; then
    ui_print "- Kernel module loading: supported"
else
    ui_print "! Kernel module loading: not detected"
    ui_print "! SDR and controller .ko files may not load"
fi

# Check USB OTG
if [ -d "/sys/class/udc" ]; then
    ui_print "- USB OTG: supported"
else
    ui_print "! USB OTG: not detected"
fi

ui_print ""
ui_print "- Setting permissions..."

set_perm_recursive $MODPATH 0 0 0755 0644
set_perm $MODPATH/post-fs-data.sh 0 0 0755
set_perm $MODPATH/service.sh 0 0 0755
set_perm_recursive $MODPATH/config 0 0 0755 0644
set_perm_recursive $MODPATH/modules 0 0 0755 0644
set_perm_recursive $MODPATH/firmware 0 0 0755 0644
set_perm_recursive $MODPATH/system/etc 0 0 0755 0644

ui_print ""
ui_print "- Installation complete!"
ui_print "- Use WebUI in KernelSU to switch driver modes"
ui_print "- Place .ko modules in modules/ dir"
ui_print "- Place firmware in firmware/ dir"
ui_print ""
