#!/bin/sh
set -eu

tmp_dir="$(mktemp -d)"
psr_tmp_dir=""
cleanup() {
  rm -rf "$tmp_dir"
  if [ -n "$psr_tmp_dir" ]; then
    rm -rf "$psr_tmp_dir"
  fi
}
trap cleanup EXIT HUP INT TERM

cat > "$tmp_dir/composer.json" <<'JSON'
{
  "name": "truckwash/composer-entrypoint-fixture",
  "autoload": {
    "classmap": [
      "src/"
    ]
  }
}
JSON

mkdir -p "$tmp_dir/src"
cat > "$tmp_dir/src/FixtureClass.php" <<'PHP'
<?php

class FixtureClass
{
}
PHP

COMPOSER_ALLOW_SUPERUSER=1 composer install \
  --no-dev \
  --prefer-dist \
  --optimize-autoloader \
  --no-interaction \
  -d "$tmp_dir" >/dev/null 2>&1

printf '%s\n' '<?php' > "$tmp_dir/vendor/composer/autoload_real.php"

AUTO_COMPOSER_INSTALL=true \
APP_DIR="$tmp_dir" \
MODULE_DIR="$tmp_dir/no-module" \
LOG_FILE="$tmp_dir/composer-install.log" \
REDIS_CONFIG_HOST= \
/usr/local/bin/docker-entrypoint.sh \
php -r "require \$argv[1]; echo class_exists('FixtureClass') ? 'autoload-ok' . PHP_EOL : 'autoload-missing' . PHP_EOL;" \
"$tmp_dir/vendor/autoload.php" >/dev/null

php -d display_errors=1 -r "require \$argv[1]; exit(class_exists('FixtureClass') ? 0 : 1);" "$tmp_dir/vendor/autoload.php"

psr_tmp_dir="$(mktemp -d)"
cat > "$psr_tmp_dir/composer.json" <<'JSON'
{
  "name": "truckwash/composer-entrypoint-psr-fixture",
  "require": {
    "psr/http-message": "^2.0"
  }
}
JSON

COMPOSER_ALLOW_SUPERUSER=1 composer install \
  --no-dev \
  --prefer-dist \
  --optimize-autoloader \
  --no-interaction \
  -d "$psr_tmp_dir" >/dev/null 2>&1

cat > "$psr_tmp_dir/corrupt-autoload.php" <<'PHP'
<?php

$dir = $argv[1];
$replacements = [
    "__DIR__ . '/..' . '/psr/http-message/src/StreamInterface.php'" => "__DIR__ . '/../..' . '/modules/washcertificates/vendor/psr/http-message/src/StreamInterface.php'",
    "__DIR__ . '/..' . '/psr/http-message/src/UriInterface.php'" => "__DIR__ . '/../..' . '/modules/washcertificates/vendor/psr/http-message/src/UriInterface.php'",
    "\$vendorDir . '/psr/http-message/src/StreamInterface.php'" => "\$vendorDir . '/../modules/washcertificates/vendor/psr/http-message/src/StreamInterface.php'",
    "\$vendorDir . '/psr/http-message/src/UriInterface.php'" => "\$vendorDir . '/../modules/washcertificates/vendor/psr/http-message/src/UriInterface.php'",
];

foreach (['autoload_static.php', 'autoload_classmap.php'] as $file) {
    $path = $dir . '/vendor/composer/' . $file;
    $contents = file_get_contents($path);
    if ($contents === false) {
        fwrite(STDERR, "Unable to read $path\n");
        exit(1);
    }

    $updated = str_replace(array_keys($replacements), array_values($replacements), $contents);
    if ($updated === $contents) {
        fwrite(STDERR, "Fixture did not corrupt $path\n");
        exit(1);
    }

    file_put_contents($path, $updated);
}
PHP

php "$psr_tmp_dir/corrupt-autoload.php" "$psr_tmp_dir"

if php -d display_errors=1 -r "require \$argv[1]; exit(interface_exists('Psr\\\\Http\\\\Message\\\\UriInterface') ? 0 : 1);" "$psr_tmp_dir/vendor/autoload.php" >/dev/null 2>&1; then
  echo "fixture failed to corrupt psr/http-message autoload map" >&2
  exit 1
fi

AUTO_COMPOSER_INSTALL=true \
APP_DIR="$psr_tmp_dir" \
MODULE_DIR="$psr_tmp_dir/no-module" \
LOG_FILE="$psr_tmp_dir/composer-install.log" \
REDIS_CONFIG_HOST= \
/usr/local/bin/docker-entrypoint.sh \
php -d display_errors=1 -r "require \$argv[1]; exit(interface_exists('Psr\\\\Http\\\\Message\\\\UriInterface') && interface_exists('Psr\\\\Http\\\\Message\\\\StreamInterface') ? 0 : 1);" \
"$psr_tmp_dir/vendor/autoload.php"

php -d display_errors=1 -r "require \$argv[1]; exit(interface_exists('Psr\\\\Http\\\\Message\\\\UriInterface') && interface_exists('Psr\\\\Http\\\\Message\\\\StreamInterface') ? 0 : 1);" "$psr_tmp_dir/vendor/autoload.php"

echo "composer-entrypoint-autoload-recovery-ok"
