#!/bin/sh
set -e
if [ "$(id -u)" = 0 ]; then
  mkdir -p /app/data /app/public/uploads
  chown -R nextjs:nodejs /app/data /app/public/uploads
  exec gosu nextjs:nodejs "$0" "$@"
fi

if [ -z "$DATABASE_URL" ]; then
  echo "DATABASE_URL doit être défini (ex. file:/app/data/app.db)" >&2
  exit 1
fi
case "$DATABASE_URL" in
  file:*)
    dbpath="${DATABASE_URL#file:}"
    dir=$(dirname "$dbpath")
    if [ "$dir" != "." ] && [ -n "$dir" ]; then
      mkdir -p "$dir"
    fi
    ;;
esac
npx prisma migrate deploy
tsx prisma/seed.ts
exec node server.js
