#!/bin/bash
# Load environment variables from .env file
# Usage: source scripts/load-env.sh
#    or: eval $(scripts/load-env.sh --export)

ENV_FILE="${ENV_FILE:-/home/aria/.openclaw/workspace/.env}"

if [ ! -f "$ENV_FILE" ]; then
    echo "Error: .env file not found at $ENV_FILE" >&2
    exit 1
fi

if [ "$1" = "--export" ]; then
    # Output export statements (for eval)
    grep -v '^#' "$ENV_FILE" | grep -v '^$' | while read -r line; do
        # Skip lines without =
        if [[ "$line" == *"="* ]]; then
            key="${line%%=*}"
            value="${line#*=}"
            # Only export if value is not empty
            if [ -n "$value" ]; then
                echo "export $key=\"$value\""
            fi
        fi
    done
else
    # Source directly (must use: source script.sh)
    set -a
    source "$ENV_FILE"
    set +a
    echo "Environment loaded from $ENV_FILE"
fi
