#!/bin/bash
# system/scripts/env-init.sh

# Resolve the directory this script is in
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# Walk up directories until we find the .env file
ROOT="$SCRIPT_DIR"
while [[ "$ROOT" != "/" && ! -f "$ROOT/.env" ]]; do
    ROOT="$(dirname "$ROOT")"
done

# Safety check
if [ ! -f "$ROOT/.env" ]; then
    echo "ERROR: Could not find .env file in any parent directory of $SCRIPT_DIR"
    exit 1
fi

export ROOT
export $(grep -v '^#' "$ROOT/.env" | xargs)
