#!/bin/sh
set -eu

# Regenerate Prisma client (needed after bind-mount overlays the image layer)
pnpm --filter @capakraken/db db:generate

# Run pending migrations so a fresh checkout boots against a current schema
pnpm --filter @capakraken/db db:migrate:deploy

pnpm check:exports
pnpm check:imports

repo_uid="$(stat -c '%u' /app)"
repo_gid="$(stat -c '%g' /app)"
repo_home="/tmp/capakraken-dev-home"

mkdir -p /app/apps/web/.next
mkdir -p "$repo_home/.config/pnpm"
chown -R "$repo_uid:$repo_gid" /app/apps/web/.next
chown -R "$repo_uid:$repo_gid" "$repo_home"

exec setpriv --reuid="$repo_uid" --regid="$repo_gid" --clear-groups \
  env HOME="$repo_home" XDG_CONFIG_HOME="$repo_home/.config" \
  pnpm --filter @capakraken/web exec next dev -H 0.0.0.0 -p 3100
