#!/bin/bash

set -o errexit    # always exit on error
set -o pipefail   # don't ignore exit codes when piping output

echo "-----> Running post-frontend script"

# Move the frontend build to the app root and clean up
mkdir -p build/
mv src/frontend/dist build/frontend-out

mv src/backend/* ./

# The backend declares jmap-email as a local path dependency
# (tool.uv.sources: path = "../jmap-email"). After flattening the backend into
# the app dir above, uv resolves that path one level UP — i.e. as a sibling of
# the app dir, not a child. Place jmap-email there to match, mirroring the
# Docker build (`COPY --from=jmap-email . /jmap-email`, backend at WORKDIR /app).
# It lands outside `src/`, so `rm -rf src` in scalingo_postcompile won't delete
# it, and `--no-editable` copies it into the venv so runtime needs no path.
mv src/jmap-email ../jmap-email

mkdir -p messages_backend && touch messages_backend/__init__.py

# Download Caddy binary
CADDY_VERSION="2.9.1"
curl -fsSL "https://github.com/caddyserver/caddy/releases/download/v${CADDY_VERSION}/caddy_${CADDY_VERSION}_linux_amd64.tar.gz" | tar -xz -C bin/ caddy
chmod +x bin/caddy

# Copy Caddyfile (uses {$ENV} vars natively, no ERB needed)
cp src/frontend/caddy/Caddyfile ./Caddyfile

echo "3.14" > .python-version
