# Dangerous Pi - pi-gen Stage Integration

This directory contains the pi-gen stage scripts for installing Dangerous Pi into the custom Raspberry Pi OS image.

## Files

- `00-run.sh` - Pre-chroot script that prepares application files
- `00-run-chroot.sh` - Chroot script that installs Dangerous Pi and dependencies

## What Gets Installed

The stage performs the following actions:

### 1. Python Dependencies

Installs all required Python packages via pip:
- FastAPI and Uvicorn (web framework and ASGI server)
- AsyncIO libraries (aiosqlite, aiohttp)
- Pydantic (data validation)
- SSE-Starlette (Server-Sent Events)
- psutil (system monitoring)
- smbus2 (I2C communication for UPS)

### 2. Application Installation

- Creates `/opt/dangerous-pi` directory
- Copies application files (backend, frontend, plugins)
- Creates data and logs directories
- Sets proper ownership (pi:pi)

### 3. Configuration

- Creates environment file from template at `/opt/dangerous-pi/.env`
- Configures default settings (can be customized post-install)

### 4. Systemd Service

- Installs systemd service unit
- Enables service to start on boot
- Configures service with security hardening

### 5. Hardware Access

- Enables I2C interface in `/boot/config.txt` (for UPS HAT)
- Adds `pi` user to hardware groups:
  - `i2c` - I2C bus access
  - `bluetooth` - Bluetooth access
  - `gpio` - GPIO pin access
  - `dialout` - Serial device access (for Proxmark3)

## Build Process

During the pi-gen build:

1. `00-run.sh` executes outside the chroot:
   - Copies application files to temporary location
   - Prepares directory structure

2. `00-run-chroot.sh` executes inside the chroot:
   - Installs Python dependencies
   - Copies files to `/opt/dangerous-pi`
   - Configures system
   - Installs and enables systemd service

## Integration with Existing pi-pm3 Stages

Dangerous Pi (stage 04) runs after:
- Stage 01: Proxmark3 installation
- Stage 02: RaspAP installation
- Stage 03: ttyd installation

This order ensures all dependencies are available.

## Port Conflict Handling

The ttyd-bash service (from stage 03) uses port 8000, which conflicts with Dangerous Pi's default port. See the port conflict handling task for resolution options.

## Post-Build Configuration

After the image is built and booted, you can customize:

1. Edit `/opt/dangerous-pi/.env` to configure settings
2. Restart service: `sudo systemctl restart dangerous-pi`
3. View status: `sudo systemctl status dangerous-pi`
4. View logs: `sudo journalctl -u dangerous-pi -f`

## Testing the Stage

To test this stage during development:

1. Make changes to Dangerous Pi code
2. Run pi-gen build (this stage will copy the latest code)
3. Flash image to SD card
4. Boot Raspberry Pi
5. Check service status

## Customization

### Change Installation Directory

Edit `00-run-chroot.sh`:
```bash
mkdir -p /your/custom/path
cd /your/custom/path
```

Also update the systemd service file paths.

### Add Additional Dependencies

Edit `00-run-chroot.sh` and add to the pip install command:
```bash
pip3 install --break-system-packages \
    <existing packages> \
    your-new-package==1.0.0
```

### Skip Service Auto-Start

Comment out or remove from `00-run-chroot.sh`:
```bash
# systemctl enable dangerous-pi.service
```

## Troubleshooting

### Build Fails - Python Package Error

Check that all package versions in the pip install command match `requirements.txt`.

### Build Fails - Permission Error

Ensure scripts are executable:
```bash
chmod +x 00-run.sh 00-run-chroot.sh
```

### Service Doesn't Start After Boot

Check systemd logs:
```bash
sudo journalctl -u dangerous-pi -b
```

Common issues:
- Missing dependencies
- Incorrect file permissions
- Port conflict with ttyd
- Missing environment variables

### I2C Not Working

Verify I2C is enabled:
```bash
grep i2c_arm /boot/config.txt
ls -l /dev/i2c*
```

### Can't Access Proxmark3

Verify user is in dialout group:
```bash
groups pi | grep dialout
ls -l /dev/ttyACM*
```

## Development Workflow

1. Make changes to Dangerous Pi code in main repository
2. Test locally with `python3 -m app.backend.main`
3. When ready to test in image:
   - Run pi-gen build (or incremental build for this stage)
   - Flash to SD card and test
4. Iterate as needed

## Version Management

The VERSION file is copied from the repository root. If it doesn't exist, version defaults to "0.1.0".

To set a specific version:
```bash
echo "1.0.0" > /path/to/dangerous-pi/VERSION
```

Then rebuild the image.
