#!/bin/bash
set -e

cd /app

DB_PATH="/app/database/database.sqlite"

if [ -f "$DB_PATH" ]; then
    echo "[init] Database already exists, skipping first-run init."
    exit 0
fi

echo "[init] First run detected — initializing application..."

touch "$DB_PATH"
chown www-data:www-data "$DB_PATH"
chmod 664 "$DB_PATH"

if [ -z "${APP_KEY}" ]; then
    echo "[init] Generating application key..."
    php artisan key:generate --force
fi

echo "[init] First-run init complete."
