#!/bin/sh

set -eu

drupal_root='/opt/drupal/web'
drush='/opt/drupal/vendor/bin/drush'
data_directory='/var/lib/postfix-admin-console/data'
database_file="${POSTFIX_ADMIN_CONSOLE_DATABASE:?POSTFIX_ADMIN_CONSOLE_DATABASE is required}"
marker="${POSTFIX_ADMIN_CONSOLE_INSTALL_MARKER:?POSTFIX_ADMIN_CONSOLE_INSTALL_MARKER is required}"
persisted_settings="${POSTFIX_ADMIN_CONSOLE_SETTINGS:?POSTFIX_ADMIN_CONSOLE_SETTINGS is required}"
active_settings="${drupal_root}/sites/default/settings.php"

if [ -L "${persisted_settings}" ] || [ -L "${active_settings}" ]; then
  echo 'Refusing a symlink for Drupal settings.' >&2
  exit 1
fi

if [ -f "${marker}" ]; then
  if [ ! -s "${database_file}" ]; then
    echo 'The install marker exists without a usable SQLite database.' >&2
    exit 1
  fi
  if [ ! -s "${persisted_settings}" ]; then
    echo 'The install marker exists without persisted Drupal settings.' >&2
    exit 1
  fi
  install -m 0440 -o root -g www-data "${persisted_settings}" "${active_settings}"
  exit 0
fi

if [ -e "${database_file}" ]; then
  echo 'A database exists without the install marker; refusing to reinstall it.' >&2
  exit 1
fi

cp "${drupal_root}/sites/default/default.settings.php" "${active_settings}"
mkdir -p "${drupal_root}/sites/default/files"
chown -R www-data:www-data "${drupal_root}/sites/default"

internal_password="$(php -r 'echo bin2hex(random_bytes(24));')"
run_drush() {
  runuser -u www-data -- env \
    POSTFIX_ADMIN_CONSOLE_USERNAME="${POSTFIX_ADMIN_CONSOLE_USERNAME:?POSTFIX_ADMIN_CONSOLE_USERNAME is required}" \
    POSTFIX_ADMIN_CONSOLE_PASSWORD="${POSTFIX_ADMIN_CONSOLE_PASSWORD:?POSTFIX_ADMIN_CONSOLE_PASSWORD is required}" \
    "${drush}" --root="${drupal_root}" "$@"
}

run_drush site:install minimal \
  --db-url="sqlite://localhost//var/lib/postfix-admin-console/data/postfix-admin-console.sqlite" \
  --site-name='Postfix Admin Console demo' \
  --account-name='site_owner' \
  --account-pass="${internal_password}" \
  --yes

recipe_root=''
for candidate in \
  /opt/drupal/recipes/postfix_admin_console \
  /opt/drupal/vendor/drupal/postfix_admin_console \
  /opt/drupal/vendor/drupal/postfix_admin_console/recipes/postfix_admin_console; do
  if [ -f "${candidate}/recipe.yml" ]; then
    recipe_root="${candidate}"
    break
  fi
done
if [ -z "${recipe_root}" ]; then
  echo 'The Postfix Admin Console Recipe was not found in the Composer project.' >&2
  exit 1
fi

run_drush recipe "${recipe_root}" --yes
run_drush recipe "${recipe_root}/examples/dedicated-site" --yes

run_drush php:eval '
$username = getenv("POSTFIX_ADMIN_CONSOLE_USERNAME");
$password = getenv("POSTFIX_ADMIN_CONSOLE_PASSWORD");
$storage = \Drupal::entityTypeManager()->getStorage("user");
if ($storage->loadByProperties(["name" => $username])) {
  throw new \RuntimeException("The requested operator account already exists.");
}
$account = $storage->create([
  "name" => $username,
  "mail" => $username . "@example.invalid",
  "status" => 1,
]);
$account->setPassword($password);
$account->addRole("postfix_administrator");
$account->save();
'
run_drush config:set user.settings register admin_only --yes
run_drush cache:rebuild

install -m 0600 -o root -g root "${active_settings}" "${persisted_settings}"
chown root:root "${drupal_root}/sites/default"
chmod 0755 "${drupal_root}/sites/default"
chown root:www-data "${active_settings}"
chmod 0440 "${active_settings}"
touch "${marker}"
chown www-data:www-data "${data_directory}" "${marker}"
chmod 0600 "${marker}"
