#!/data/data/com.termux/files/usr/bin/bash
# Termux setup script for quicprochat.
#
# Run this ON your Android device inside Termux after copying the qpc binary.
#
# What it does:
#   1. Installs the qpc binary to $PREFIX/bin/
#   2. Creates a default config at ~/.config/qpc/config.toml
#   3. Sets up a data directory for state files
#
# Usage:
#   # If you have a pre-built binary on /sdcard:
#   bash termux-setup.sh
#
#   # If you want to build from source in Termux:
#   bash termux-setup.sh --build

set -euo pipefail

PREFIX="${PREFIX:-/data/data/com.termux/files/usr}"
CONFIG_DIR="$HOME/.config/qpc"
DATA_DIR="$HOME/.local/share/qpc"

echo "=== quicprochat Termux setup ==="
echo ""

# ── Install binary ────────────────────────────────────────────────────────────

if [ "${1:-}" = "--build" ]; then
    echo "Building from source (this will take a while)..."
    pkg install -y rust git
    if [ ! -d "$HOME/quicprochat" ]; then
        echo "Clone the repository first:"
        echo "  git clone <repo-url> ~/quicprochat"
        exit 1
    fi
    cd "$HOME/quicprochat"
    cargo build --release --bin qpc --features tui
    cp target/release/qpc "$PREFIX/bin/qpc"
    echo "  Built and installed qpc to $PREFIX/bin/"
else
    # Look for pre-built binary in common locations.
    FOUND=""
    for candidate in \
        /sdcard/Download/qpc \
        /sdcard/qpc \
        "$HOME/qpc" \
        "$HOME/storage/downloads/qpc"; do
        if [ -f "$candidate" ]; then
            FOUND="$candidate"
            break
        fi
    done

    if [ -z "$FOUND" ]; then
        echo "No qpc binary found. Place it in one of:"
        echo "  /sdcard/Download/qpc"
        echo "  ~/qpc"
        echo ""
        echo "Or build from source: bash $0 --build"
        exit 1
    fi

    cp "$FOUND" "$PREFIX/bin/qpc"
    chmod +x "$PREFIX/bin/qpc"
    echo "  Installed $FOUND -> $PREFIX/bin/qpc"
fi

# ── Create directories ────────────────────────────────────────────────────────

mkdir -p "$CONFIG_DIR" "$DATA_DIR"

# ── Create default config ─────────────────────────────────────────────────────

if [ -f "$CONFIG_DIR/config.toml" ]; then
    echo "  Config already exists: $CONFIG_DIR/config.toml (not overwriting)"
else
    # Prompt for basic settings.
    echo ""
    read -rp "Server address [127.0.0.1:7000]: " SERVER
    SERVER="${SERVER:-127.0.0.1:7000}"

    read -rp "Username: " USERNAME

    # Extract hostname for TLS server_name.
    SERVER_HOST="${SERVER%%:*}"

    cat > "$CONFIG_DIR/config.toml" << TOML
# quicprochat client config — generated by termux-setup.sh
# Edit this file instead of passing CLI flags.
# Precedence: CLI flags > env vars > this file > defaults.

# Server to connect to.
server = "$SERVER"
server_name = "$SERVER_HOST"

# Your username (OPAQUE login — password is prompted on first run).
username = "$USERNAME"

# Skip TLS verification for self-signed certs (set false for production).
danger_accept_invalid_certs = true

# Don't try to auto-start a local server.
no_server = true

# State/data files are stored here (relative paths resolve from cwd,
# so use absolute paths on Termux).
state = "$DATA_DIR/qpc-state.bin"
# ca_cert = "/path/to/server-cert.der"
TOML
    echo "  Created config: $CONFIG_DIR/config.toml"
fi

# ── Done ──────────────────────────────────────────────────────────────────────

echo ""
echo "=== Setup complete ==="
echo ""
echo "Start chatting:"
echo "  qpc"
echo ""
echo "Edit your config:"
echo "  nano ~/.config/qpc/config.toml"
echo ""
echo "Your data is stored in: $DATA_DIR"
