#!/bin/bash
# View room service logs
#
# Usage:
#   ./logs.sh              # All logs
#   ./logs.sh <service>    # Service compose logs
#   ./logs.sh <container>  # Specific container logs
#   ./logs.sh -f           # Follow mode
#
# This is a TEMPLATE. Copy to your room's ctrl/ and customize.

set -e

cd "$(dirname "$0")/.."

FOLLOW=""
TARGET=""
SERVICE_DIRS=()

for dir in */; do
    [ -f "$dir/docker-compose.yml" ] && SERVICE_DIRS+=("${dir%/}")
done

for arg in "$@"; do
    case $arg in
        -f|--follow) FOLLOW="-f" ;;
        *) TARGET="$arg" ;;
    esac
done

if [ -z "$TARGET" ]; then
    # Show all logs
    for svc in "${SERVICE_DIRS[@]}"; do
        echo "=== $svc ==="
        (cd "$svc" && docker compose logs --tail=20 $FOLLOW) || true
    done
elif [[ " ${SERVICE_DIRS[*]} " =~ " ${TARGET} " ]]; then
    # Service compose logs
    (cd "$TARGET" && docker compose logs $FOLLOW)
else
    # Specific container
    docker logs $FOLLOW "$TARGET"
fi
