#!/usr/bin/env bash
set -euo pipefail

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$ROOT"

if ! command -v php >/dev/null 2>&1; then
  echo "ERROR: PHP CLI is not available." >&2
  exit 1
fi

if [[ ! -f config/config.php ]]; then
  cp config/config.example.php config/config.php
  echo "Created config/config.php from the example. Edit it before opening the panel."
else
  echo "Existing config/config.php preserved."
fi

mkdir -p storage/proposals storage/backups storage/logs
chmod 700 config storage storage/proposals storage/backups storage/logs
chmod 600 config/config.php
chmod 640 bootstrap.php VERSION || true
find src -type f -exec chmod 640 {} \;
find public -type f -exec chmod 644 {} \;
find public -type d -exec chmod 755 {} \;

echo
echo "Generate the panel password hash with:"
echo "php -r \"echo password_hash('YOUR-LONG-PASSWORD', PASSWORD_DEFAULT), PHP_EOL;\""
echo
echo "Then edit: $ROOT/config/config.php"
echo "Finally run: php $ROOT/scripts/health_check.php"
