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

# 1. CHECK IF APPLICABLE
if [[ ! -f composer.json ]] && [[ ! -f index.php ]] && [[ ! -f .php-version ]]; then
    exit 100
fi

# 2. GET ARGS
plan_path=$2

# 3. GET CONFIGURATION
php_version=8.2
if [[ -f .php-version ]]; then
    php_version=$(tr <.php-version -d '[:space:]')
fi

nginx_version=1.25.1
if [[ -f .nginx-version ]]; then
    nginx_version=$(tr <.nginx-version -d '[:space:]')
fi

# 4. DECLARE DEPENDENCIES
cat >>"${plan_path}" <<EOL
[[provides]]
name = "php"

[[requires]]
name = "php"

[requires.metadata]
version = "${php_version}"

[[provides]]
name = "nginx"

[[requires]]
name = "nginx"

[requires.metadata]
version = "${nginx_version}"
EOL
