#!/bin/sh
set -e

cd /app

mkdir -p /app/data
mkdir -p /app/packages/server/uploads/stamps
mkdir -p /app/packages/server/uploads/articles
mkdir -p /app/packages/server/uploads/music

# Seed stamp assets on first run (idempotent)
if [ -z "$(ls -A /app/packages/server/uploads/stamps 2>/dev/null)" ]; then
  echo "→ Seeding stamp assets into uploads volume..."
  cp /app/stamps-seed/*.jpg /app/packages/server/uploads/stamps/ 2>/dev/null || true
fi

# Seed article assets on first run (idempotent)
if [ -z "$(ls -A /app/packages/server/uploads/articles 2>/dev/null)" ]; then
  echo "→ Seeding article assets into uploads volume..."
  cp /app/articles-seed/*.jpg /app/packages/server/uploads/articles/ 2>/dev/null || true
fi

# Seed music assets on first run (idempotent)
if [ -z "$(ls -A /app/packages/server/uploads/music 2>/dev/null)" ]; then
  echo "→ Seeding music assets into uploads volume..."
  cp /app/music-seed/* /app/packages/server/uploads/music/ 2>/dev/null || true
fi

echo "→ Applying database migrations..."
pnpm exec prisma migrate deploy

echo "→ Starting server on :${SERVER_PORT:-3000}..."
cd /app/packages/server
exec pnpm exec tsx src/index.ts
