init_env.sh 1.09 KB
#!/bin/bash
#
# Initialize .env from .env.example.
# Usage:
#   ./scripts/init_env.sh              # Copy .env.example → .env if missing
#   ./scripts/init_env.sh --force       # Overwrite .env with .env.example (backup to .env.bak)
#
# Production credentials: see docs/QUICKSTART.md §1.6
#
set -euo pipefail

PROJECT_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
cd "${PROJECT_ROOT}"

ENV_FILE="${PROJECT_ROOT}/.env"
EXAMPLE_FILE="${PROJECT_ROOT}/.env.example"

if [ ! -f "${EXAMPLE_FILE}" ]; then
  echo "ERROR: .env.example not found at ${EXAMPLE_FILE}" >&2
  exit 1
fi

force="${1:-}"
if [[ "${force}" == "--force" ]]; then
  if [ -f "${ENV_FILE}" ]; then
    cp "${ENV_FILE}" "${ENV_FILE}.bak"
    echo "[init_env] Backed up .env to .env.bak"
  fi
  cp "${EXAMPLE_FILE}" "${ENV_FILE}"
  echo "[init_env] Created .env from .env.example"
  exit 0
fi

if [ -f "${ENV_FILE}" ]; then
  echo "[init_env] .env already exists, skipping"
  exit 0
fi

cp "${EXAMPLE_FILE}" "${ENV_FILE}"
echo "[init_env] Created .env from .env.example"
echo ""
echo "Next: Edit .env with your credentials (see docs/QUICKSTART.md §1.6)"