#!/bin/bash
# ─────────────────────────────────────────────────────────
# ffuf-automation — run.sh
# Easy wrapper to build and run the Docker container
# Usage: ./run.sh [ffuf_automation.py arguments]
# ─────────────────────────────────────────────────────────

set -e

IMAGE_NAME="ffuf-automation:latest"
CONTAINER_NAME="ffuf-auto-run"

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
CYAN='\033[0;36m'
YELLOW='\033[1;33m'
NC='\033[0m'

echo -e "${CYAN}"
echo "╔══════════════════════════════════════════════════╗"
echo "║       ffuf-automation — Docker Runner            ║"
echo "║                         ║"
echo "╚══════════════════════════════════════════════════╝"
echo -e "${NC}"

echo -e "${YELLOW}⚠️  AUTHORIZED USE ONLY — Only scan domains you own or have written permission to test.${NC}\n"

# ── Check Docker is installed ──────────────────────────────
if ! command -v docker &>/dev/null; then
    echo -e "${RED}[ERROR] Docker is not installed.${NC}"
    echo "Install: https://docs.docker.com/engine/install/ubuntu/"
    exit 1
fi

# ── Build image if not already built ─────────────────────
if ! docker image inspect "$IMAGE_NAME" &>/dev/null; then
    echo -e "${CYAN}[BUILD] Building Docker image (first time, may take a few minutes)...${NC}"
    docker build -t "$IMAGE_NAME" .
    echo -e "${GREEN}[OK] Image built successfully.${NC}\n"
else
    echo -e "${GREEN}[OK] Using existing image: $IMAGE_NAME${NC}"
    echo -e "     (To rebuild: docker build -t $IMAGE_NAME . --no-cache)\n"
fi

# ── Create results dir on host ────────────────────────────
mkdir -p results

# ── Detect user-provided file args and mount them ─────────
EXTRA_MOUNTS=()

# Auto-mount fixed optional files if present
[ -f "$(pwd)/extensions.txt" ]  && EXTRA_MOUNTS+=("-v" "$(pwd)/extensions.txt:/app/extensions.txt:ro")
[ -f "$(pwd)/interesting.txt" ] && EXTRA_MOUNTS+=("-v" "$(pwd)/interesting.txt:/app/interesting.txt:ro")

# Dynamically mount any file passed to these flags
_mount_flag_file() {
    local flag="$1"; shift
    local args=("$@")
    for ((i=0; i<${#args[@]}; i++)); do
        if [[ "${args[$i]}" == "$flag" && $((i+1)) -lt ${#args[@]} ]]; then
            local val="${args[$((i+1))]}"
            local base; base=$(basename "$val")
            local host_path="$(pwd)/$base"
            if [[ -f "$host_path" ]]; then
                EXTRA_MOUNTS+=("-v" "$host_path:/app/$base:ro")
                echo -e "${GREEN}[MOUNT] $host_path → /app/$base${NC}"
            fi
            break
        fi
    done
}

_mount_flag_file "--force-recursion-paths" "$@"
_mount_flag_file "--extensions-file"       "$@"
_mount_flag_file "--interesting-file"      "$@"

# ── Run ───────────────────────────────────────────────────
echo -e "${CYAN}[RUN] Starting container...${NC}\n"

docker run --rm -it \
    --name "$CONTAINER_NAME" \
    -v "$(pwd)/domains.txt:/app/domains.txt:ro" \
    -v "$(pwd)/wordlists:/app/wordlists:ro" \
    -v "$(pwd)/results:/app/results" \
    "${EXTRA_MOUNTS[@]}" \
    "$IMAGE_NAME" "$@"

echo -e "\n${GREEN}[DONE] Results saved to: $(pwd)/results/${NC}"
