testsAndMisc/linux_configuration/utils/lib/bl9000_device.sh
Krzysztof kuhy Rudnicki 4f2b0ccdf7 Dissolve linux_configuration/scripts/ and single_use/
Two levels of pure taxonomy carrying no information, and a 2-directory
cap leaves no room for them. Categories now sit directly under
linux_configuration/: features, fixes, utils, misc, fresh-install,
installers, diagnostics, gaming, boot_recovery, periodic_background, lib,
meta, tests. A script's own lib/ and tests/ are exempt from the cap, so
those stay put.

Depth violations 248 -> 139; roughly 40 of the remainder are inside
digital_wellbeing and hosts, deferred until the guard-lib repoint.

The moves are the easy half. What breaks is every path that counted
levels, and three classes of it had to be found by resolving paths rather
than reading diffs:

  - 52 relative `source` lines for lib/common.sh and lib/android.sh. The
    correct prefix differs per file: 40 were ../../ and 12 were ../../../,
    so a blanket replace would have fixed one set and broken the other.
  - REPO_ROOT in 7 fixes/lib/tests harnesses and setup_syncyomi_guard.sh,
    each walking up four levels from a directory now two levels shallower.
    This caused a real regression -- the fixes suite went red with
    "log_warn: command not found" -- caught by running it and comparing
    against the pre-move baseline worktree.
  - conftest.py's sys.path entries, which took the pytest gate down with
    14 collection errors, and the `# shellcheck source=` directives, which
    took shellcheck down with SC1091.

16 symlinks in $HOME broke, including the entire i3blocks bar. All
repointed, and cpu_monitor/memory/disk/time/volume were run to confirm
they emit real output rather than merely resolving.

Verified: 1798 tests pass at 100% coverage, 8 of 9 lib/tests suites pass
(the ninth needs the coverage jail, identically at baseline), pre-commit
is clean, and the two untracked secret configs are still ignored after
their gitignore paths changed.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PJrqKERAo5QUeEHWS5Asgp
2026-08-23 18:53:30 +02:00

195 lines
6.6 KiB
Bash
Executable File

#!/bin/bash
# Device detection and connection checks.
#
# Sourced by root_bl9000.sh; split out to keep it under the 250-line
# cap. Sourced rather than run, so it inherits the caller's strict mode
# and the variables defined above the source line.
check_device() {
print_header "Checking Device Connection"
log "Starting ADB server..."
adb start-server > /dev/null 2>&1 || true
log "Waiting for device..."
if ! adb wait-for-device; then
error "Failed to detect device via ADB."
echo
echo "Troubleshooting steps:"
echo "1. Make sure USB debugging is enabled on your phone"
echo " Settings → About Phone → Tap Build Number 7 times"
echo " Settings → Developer Options → Enable USB Debugging"
echo "2. Connect your phone via USB cable"
echo "3. Accept the 'Allow USB debugging' prompt on your phone"
echo "4. Run: adb devices"
echo
return 1
fi
local device_info
device_info=$(adb devices -l | grep -v "List of devices" | grep -v "^$" | head -n1)
if [[ -z $device_info ]]; then
error "No device detected"
return 1
fi
log "Device connected: $device_info"
# Check device properties
local model
model=$(adb shell getprop ro.product.model 2> /dev/null | tr -d '\r\n' || echo "Unknown")
log "Model: $model"
local android_version
android_version=$(adb shell getprop ro.build.version.release 2> /dev/null | tr -d '\r\n' || echo "Unknown")
log "Android version: $android_version"
local battery_level
battery_level=$(adb shell dumpsys battery | grep level | awk '{print $2}' | tr -d '\r\n' || echo "Unknown")
log "Battery level: ${battery_level}%"
if [[ $battery_level != "Unknown" && $battery_level -lt 50 ]]; then
warn "Battery level is below 50%. Charge your phone before proceeding."
if ! confirm "Continue anyway?"; then
return 1
fi
fi
# Check if bootloader is unlocked
local unlock_status
unlock_status=$(adb shell getprop ro.boot.verifiedbootstate 2> /dev/null | tr -d '\r\n' || echo "unknown")
if [[ $unlock_status == "orange" || $unlock_status == "red" ]]; then
log "Bootloader unlock status: ${GREEN}UNLOCKED${NC}"
else
warn "Bootloader appears to be LOCKED. You'll need to unlock it to root."
fi
# Check if OEM unlocking is enabled
local oem_unlock
oem_unlock=$(adb shell getprop sys.oem_unlock_allowed 2> /dev/null | tr -d '\r\n' || echo "unknown")
if [[ $oem_unlock == "1" ]]; then
log "OEM unlocking: ${GREEN}ENABLED${NC}"
else
warn "OEM unlocking is not enabled in Developer Options."
echo "Enable it at: Settings → Developer Options → OEM unlocking"
fi
return 0
}
unlock_bootloader() {
print_header "Unlocking Bootloader"
echo
echo -e "${RED}╔═══════════════════════════════════════════════════════════════╗${NC}"
echo -e "${RED}║ WARNING ║${NC}"
echo -e "${RED}║ ║${NC}"
echo -e "${RED}║ Unlocking the bootloader will ERASE ALL DATA on your phone! ║${NC}"
echo -e "${RED}║ ║${NC}"
echo -e "${RED}║ This includes: ║${NC}"
echo -e "${RED}║ - All apps and app data ║${NC}"
echo -e "${RED}║ - Photos, videos, and files ║${NC}"
echo -e "${RED}║ - System settings ║${NC}"
echo -e "${RED}║ - Everything else on internal storage ║${NC}"
echo -e "${RED}║ ║${NC}"
echo -e "${RED}║ Make sure you have backed up important data! ║${NC}"
echo -e "${RED}╚═══════════════════════════════════════════════════════════════╝${NC}"
echo
if ! confirm "Have you backed up all important data and want to proceed?"; then
log "Bootloader unlock cancelled by user."
return 1
fi
if ! confirm "Are you ABSOLUTELY SURE? This cannot be undone!"; then
log "Bootloader unlock cancelled by user."
return 1
fi
log "Rebooting device to bootloader..."
adb reboot bootloader || die "Failed to reboot to bootloader"
log "Waiting for fastboot mode..."
sleep 5
if ! fastboot devices | grep -q .; then
error "Device not detected in fastboot mode."
echo
echo "If the device doesn't enter fastboot automatically:"
echo "1. Power off the phone completely"
echo "2. Hold Volume Down + Power buttons simultaneously"
echo "3. Release when you see the bootloader/fastboot screen"
echo "4. Run: fastboot devices"
echo
return 1
fi
log "Device in fastboot mode"
# Check current bootloader status
local bl_status
bl_status=$(fastboot getvar unlocked 2>&1 | grep "unlocked:" | awk '{print $2}' || echo "unknown")
if [[ $bl_status == "yes" ]]; then
log "Bootloader is already unlocked."
fastboot reboot
return 0
fi
log "Attempting to unlock bootloader..."
# Try different unlock commands (varies by device)
if fastboot flashing unlock 2>&1 | grep -qi "okay\|finished"; then
log "Bootloader unlock command sent successfully."
elif fastboot oem unlock 2>&1 | grep -qi "okay\|finished"; then
log "Bootloader unlock command sent successfully."
else
error "Bootloader unlock command may have failed."
echo
echo "On your phone:"
echo "1. Use volume buttons to select 'Unlock the bootloader'"
echo "2. Press power button to confirm"
echo
if ! confirm "Did you complete the unlock on the device?"; then
fastboot reboot
return 1
fi
fi
log "Rebooting device..."
fastboot reboot || true
log "Bootloader unlocked successfully!"
log "Device will now boot up and perform factory reset..."
log "Waiting for device to come back online..."
sleep 10
adb wait-for-device || true
log "Please complete the initial setup on your phone, then re-enable USB debugging."
echo
return 0
}
download_magisk() {
print_header "Downloading Magisk"
local magisk_apk="$WORK_DIR/magisk.apk"
if [[ -f $magisk_apk ]]; then
log "Magisk APK already downloaded at $magisk_apk"
return 0
fi
log "Downloading latest Magisk APK..."
if ! curl -L -o "$magisk_apk" "$MAGISK_APK_URL"; then
error "Failed to download Magisk APK"
return 1
fi
log "Magisk downloaded successfully: $magisk_apk"
return 0
}