#!/usr/bin/env bash
#
# deploy_site.sh - Create canvas directory for a website
#
# Usage:
#   bash deploy_site.sh --name <site-name> --workspace <workspace-dir>
#
# Outputs:
#   SITE_DIR=<absolute path to site directory>
#   SITE_URL_PATH=/canvas/<site-name>/
#

set -euo pipefail

SITE_NAME=""
WORKSPACE_DIR=""

while [[ $# -gt 0 ]]; do
  case "$1" in
    --name) SITE_NAME="$2"; shift 2 ;;
    --workspace) WORKSPACE_DIR="$2"; shift 2 ;;
    *) echo "Unknown option: $1" >&2; exit 1 ;;
  esac
done

if [[ -z "$SITE_NAME" ]]; then
  echo "Error: --name is required" >&2
  exit 1
fi

# Sanitize site name
SITE_NAME="$(echo "$SITE_NAME" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9-]/-/g' | sed 's/--*/-/g' | sed 's/^-//;s/-$//')"

if [[ -z "$WORKSPACE_DIR" ]]; then
  # Try to infer from environment
  STATE_DIR="${HOME}/.openclaw"
  WORKSPACE_DIR="${STATE_DIR}/workspace"
fi

CANVAS_ROOT="${WORKSPACE_DIR}/canvas"
SITE_DIR="${CANVAS_ROOT}/${SITE_NAME}"

mkdir -p "$SITE_DIR"

echo "SITE_DIR=${SITE_DIR}"
echo "SITE_URL_PATH=/canvas/${SITE_NAME}/"
