# Export ADB Resources Script

This script downloads Android Debug Bridge (ADB) platform tools from Google and exports them to a specified target directory. The exported resources can be used by applications that depend on `@mcesystems/adb-kit`.

## Usage

After installing `@mcesystems/adb-kit`:

```bash
# Using the binary (recommended)
npx export-adb-resources <target-path> [options]

# Or run the script directly with tsx
npx tsx node_modules/@mcesystems/adb-kit/scripts/export-resources.ts <target-path> [options]
```

### Arguments

| Argument | Description |
|----------|-------------|
| `target-path` | Directory where resources will be exported |

### Options

| Option | Description |
|--------|-------------|
| `--all` | Download resources for all platforms (Windows, macOS, Linux) |
| `--platform <name>` | Download resources for a specific platform (`windows`, `darwin`, `linux`) |
| `--help`, `-h` | Show usage information |

### Examples

```bash
# Export for current platform only
npx tsx export-resources.ts ./my-app/resources/adb-kit

# Export for all platforms
npx tsx export-resources.ts ./my-app/resources/adb-kit --all

# Export for a specific platform
npx tsx export-resources.ts ./my-app/resources/adb-kit --platform windows
```

### Output Structure

The script creates the following directory structure:

```
<target-path>/
├── bin/
│   ├── darwin/          # macOS
│   │   └── adb
│   ├── windows/         # Windows
│   │   ├── adb.exe
│   │   ├── AdbWinApi.dll
│   │   └── AdbWinUsbApi.dll
│   └── linux/           # Linux
│       └── adb
└── licenses/
    └── APACHE-2.0.txt
```

---

## Prerequisites

### All Platforms

1. **Node.js 18+** with `tsx` available:
   ```bash
   npm install -g tsx
   # or use npx
   ```

2. **Internet connection** to download from Google's servers

### macOS / Linux

- **unzip** command available (usually pre-installed)

### Windows

- **PowerShell** available (included with Windows 10+)

---

## How It Works

1. Downloads the official platform-tools ZIP from Google's Android SDK repository
2. Extracts the required ADB binaries
3. Copies them to the target directory with proper permissions
4. Creates a license file for Apache 2.0 compliance

---

## Files Included

### Windows
| File | Description |
|------|-------------|
| `adb.exe` | Android Debug Bridge executable |
| `AdbWinApi.dll` | ADB Windows API library |
| `AdbWinUsbApi.dll` | ADB Windows USB API library |

### macOS / Linux
| File | Description |
|------|-------------|
| `adb` | Android Debug Bridge executable |

---

## Integrating with Your Application

After exporting resources, configure `adb-kit` to use them:

### Option 1: Environment Variable

Set `AdbBinPath` to point to the bin directory:

```bash
# macOS
export AdbBinPath=/path/to/resources/bin/darwin

# Linux
export AdbBinPath=/path/to/resources/bin/linux

# Windows (PowerShell)
$env:AdbBinPath = "C:\path\to\resources\bin\windows"
```

### Option 2: Bundle with Your App

Copy the exported resources into your application's resources folder. The `adb-kit` package will automatically detect binaries in:
- `<your-app>/resources/bin/darwin/` (macOS)
- `<your-app>/resources/bin/linux/` (Linux)
- `<your-app>/resources/bin/windows/` (Windows)

---

## Driver Requirements

### Windows

On Windows, you may need to install USB drivers for your Android device:

1. **Google USB Driver** (for Nexus/Pixel devices):
   - Download from [Android Developer site](https://developer.android.com/studio/run/win-usb)
   
2. **Manufacturer drivers** (for other devices):
   - Check your device manufacturer's website

### macOS / Linux

No additional drivers required. USB works out of the box.

### Linux udev Rules

On Linux, you may need to add udev rules for your device:

```bash
# Create udev rules file
sudo nano /etc/udev/rules.d/51-android.rules

# Add a rule for your device (example for Google devices)
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", MODE="0666", GROUP="plugdev"

# Reload udev rules
sudo udevadm control --reload-rules
sudo udevadm trigger
```

---

## License

ADB is licensed under **Apache License 2.0**.

The binaries are downloaded from Google's official Android SDK repository:
- https://developer.android.com/studio/releases/platform-tools

Source code:
- https://android.googlesource.com/platform/packages/modules/adb/

