<?php
/**
 * Install Drupal with PostgreSQL programmatically.
 */

$drupalDir = $argv[1] ?? '/tmp/drupal';
$sitePath = 'sites/default';

// Change to Drupal root - important for relative paths
chdir($drupalDir);

// Ensure sites/default directory exists
if (!is_dir("$sitePath")) {
    mkdir("$sitePath", 0775, true);
}

// Include autoloader and install API
require_once "vendor/autoload.php";
require_once "core/includes/install.core.inc";

$classLoader = require "vendor/autoload.php";

$parameters = [
  'interactive' => FALSE,
  'site_path' => $sitePath,
  'parameters' => [
    'profile' => 'minimal',
    'langcode' => 'en',
  ],
  'forms' => [
    'install_configure_form' => [
      'site_name' => 'Drupal Test',
      'site_mail' => 'drupal@localhost',
      'account' => [
        'name' => 'admin',
        'mail' => 'admin@localhost',
        'pass' => [
          'pass1' => 'admin',
          'pass2' => 'admin',
        ],
      ],
      'enable_update_status_module' => TRUE,
      'enable_update_status_emails' => NULL,
    ],
  ],
];

try {
  install_drupal($classLoader, $parameters);
  echo "INSTALL_SUCCESS\n";
} catch (Exception $e) {
  echo "INSTALL_FAILED: " . $e->getMessage() . "\n";
  echo "Trace: " . $e->getTraceAsString() . "\n";
  exit(1);
}
