#!/bin/bash

REQUIRED_DART_VERSION="3.5"
REQUIRED_NODE_VERSION="20.14"
REQUIRED_PNPM_VERSION="9.4"

# Check that the 'dart' command is available on the user's path.
if ! command -v dart &> /dev/null; then
  echo "Dart not found. To learn how to setup Dart, follow the instructions at https://dart.dev/get-dart."
  exit 1
fi

# Check that the current version of a tool is the same or
# newer than a required version.
version_is_new_enough() {
  required_version="$1"
  current_version="$2"

  # Split the version strings into lists based on the dot separators.
  IFS='.' read -ra required_list <<< "$required_version"
  IFS='.' read -ra current_list <<< "$current_version"

  # Compare each portion of the version number.
  for ((i = 0; i < ${#required_list[@]}; i += 1)); do
    required_segment="${required_list[i]:-0}"
    current_segment="${current_list[i]:-0}"

    if [[ "$required_segment" -gt "$current_segment" ]]; then
      # The current version is too low.
      return 0
    elif [[ "$required_segment" -lt "$current_segment" ]]; then
      # The current version is higher, which is ok.
      return 1
    fi
  done

  # The current version is (mostly) the same as the required version.
  return 1
}


# Determine the available Dart SDK version.
dart_version=$(dart --version | grep -oE 'Dart SDK version: [0-9.]+' | cut -d ' ' -f 4)

# Validate the version of the installed Dart SDK.
if version_is_new_enough "$REQUIRED_DART_VERSION" "$dart_version"; then
  echo "Dart version $dart_version is too low. Version $REQUIRED_DART_VERSION or later is required."
  exit 1
fi

# Check that the 'node' command is available on the user's path.
if ! command -v node &> /dev/null; then
  echo "'node' command not found. To learn how to install and setup Node, reference the repository README."
  exit 1
fi

# Determine the available Node version.
node_version=$(node --version | cut -d 'v' -f 2)

# Validate the version of the Node installation.
if version_is_new_enough "$REQUIRED_NODE_VERSION" "$node_version"; then
  echo "Node version $node_version is too low. Version $REQUIRED_NODE_VERSION or later is required."
  exit 1
fi

# Check that the 'npx' command is available on the user's path.
if ! command -v npx &> /dev/null; then
  echo "'npx' command from Node not found. Check your Node installation."
  exit 1
fi

# Run extra logic if the 'pnpm' command is available on the user's path.
if command -v pnpm &> /dev/null; then
  # Determine the available pnpm version.
  pnpm_version=$(pnpm --version)

  # Validate the version of the pnpm installation.
  if version_is_new_enough "$REQUIRED_PNPM_VERSION" "$pnpm_version"; then
    echo "pnpm version $pnpm_version is too low. Version $REQUIRED_PNPM_VERSION or later is required."
    exit 1
  fi

  # If using pnpm, update the dependencies silently as it is quick.
  echo "Verifying npm dependencies are up to date..."
  pnpm install --silent
fi

# Verify that Node packages have been installed.
if [[ ! -d "node_modules" ]]; then
  if command -v pnpm &> /dev/null; then
    echo "Node packages not found. Installing with 'pnpm install'..."
    pnpm install
  elif command -v npm &> /dev/null; then
    echo "Node packages not found. Installing with 'npm install'..."
    npm install
  else
    echo "Neither 'pnpm' nor 'npm' found. To learn how to setup pnpm, reference the repository README."
    exit 1
  fi
fi

# Verify that Dart dependencies have been retrieved.
if [[ ! -d ".dart_tool" ]]; then
  dart pub get
fi

# Run the Dart site tool and pass through all arguments.
dart run dart_site "$@"
