#!/bin/sh
set -e

# Save original stdout to file descriptor 3, then redirect stdout and stderr to /dev/null
exec 3>&1
exec 1>/dev/null 2>&1

# Define log function to output to original stdout
log() {
    echo "$1" >&3
}

HOST="https://aiden.tr/s/tangorpro/r67"
OTA=aosp_tangorpro_car-ota-root.zip
VENDOR=vendor.tar
BOOT=vendor_boot.img
APK=app-aiden.apk

# Check if required tools are available
log "0. Checking required tools..."

# Check for system-wide adb/fastboot first
if ! command -v adb >/dev/null 2>&1 || ! command -v fastboot >/dev/null 2>&1; then
    log " - System adb & fastboot not found."

    # Check if local platform-tools exist
    if [ -f "platform-tools/adb" ] && [ -f "platform-tools/fastboot" ]; then
        log " - Using local platform-tools for adb & fastboot."
        export PATH="./platform-tools:$PATH"
    else
        if [ ! -f "platform-tools-latest-darwin.zip" ]; then
            log " - Downloading platform-tools..."
            curl -o platform-tools-latest-darwin.zip https://dl.google.com/android/repository/platform-tools-latest-darwin.zip
        else
            log " - platform-tools zip already exists, skipping download."
        fi

        unzip -o platform-tools-latest-darwin.zip

        if [ -f "platform-tools/adb" ] && [ -f "platform-tools/fastboot" ]; then
            log " - platform-tools is ready for adb & fastboot."
            export PATH="./platform-tools:$PATH"
        else
            log " Error: Failed to extract platform-tools"
            exit 1
        fi
    fi
else
    log " - System adb and fastboot found"
fi


# Prepare Environment
log "1. Preparing Environment"

# Download vendor_boot.img if not exists
if [ ! -f "$BOOT" ]; then
    log " - Downloading $BOOT..."
    curl --output $BOOT $HOST/$BOOT
else
    log " - $BOOT already exists, skipping download"
fi

# Download vendor.tar if not exists
if [ ! -f "$VENDOR" ]; then
    log " - Downloading $VENDOR..."
    curl --output $VENDOR $HOST/$VENDOR
else
    log " - $VENDOR already exists, skipping download"
fi

# Download OTA package if not exists
if [ ! -f "$OTA" ]; then
    log " - Downloading $OTA..."
    curl --output $OTA $HOST/$OTA
else
    log " - $OTA already exists, skipping download"
fi

# Download APK file if not exists
if [ ! -f "$APK" ]; then
    log " - Downloading $APK..."
    curl --output $APK $HOST/$APK
else
    log " - $APK already exists, skipping download"
fi

rm -rf product && mkdir product && tar -xf vendor.tar -C product
export ANDROID_PRODUCT_OUT=product

# OEM Unlock
log "2. Unlocking OEM..."
adb wait-for-device reboot bootloader
fastboot flashing unlock
fastboot flash vendor_boot $BOOT
fastboot reboot recovery

adb wait-for-recovery

log "3. Flashing the ROM..."
log " - Select: 'Apply update from ADB' from the menu!"
log "    If 'No Command' message shows up, then press and hold Power Button, and press Volume Up button, then release both."
adb wait-for-sideload
log " - Sideload is in progress..."
adb sideload $OTA

log "4. Syncing vendor partition..."
# Temporary disable the userdata checkpoint
adb reboot
adb wait-for-device root
sleep 3
adb shell vdc checkpoint commitChanges
sleep 2

# Enable remount
adb remount
sleep 2
adb reboot
log " - Rebooting the device"
adb wait-for-device root
sleep 5
adb remount
adb sync vendor

# Boot when charging
adb reboot bootloader
fastboot oem off-mode-charge 1
fastboot reboot

log "5. Installation is complete, now configuring the device"
# Brightness
adb wait-for-device
sleep 5
adb shell settings put system screen_brightness 255
adb shell wm density 240

# Mock location
adb unroot
adb shell cmd location set-location-enabled true
adb root
adb shell appops set 0 android:mock_location allow
adb shell cmd location providers add-test-provider gps
adb shell cmd location providers set-test-provider-enabled gps true
adb shell cmd location providers set-test-provider-location gps --location 39.971368,32.9318579
#To verify
adb shell dumpsys location | grep "last location"

# Install APK
adb install -r $APK

adb reboot
adb wait-for-device
sleep 3
log "6. Configuration is complete, device is ready to use."
