#!/usr/bin/env sh
cd "$(dirname "$0")" || exit 1
echo "Starting Vortex Evolutions portable edition..."
export PATH="$PWD/python:$PWD/python/bin:$PWD/tools:$PWD/jdk/bin:$PWD/java/bin:$HOME/.pyenv/shims:$HOME/.pyenv/bin:$HOME/.asdf/shims:$HOME/.local/bin:/opt/python/bin:/usr/local/bin:/usr/bin:/bin:$PATH"
if command -v python3 >/dev/null 2>&1; then
  export YOTAVON_PYTHON="$(command -v python3)"
elif command -v python >/dev/null 2>&1; then
  export YOTAVON_PYTHON="$(command -v python)"
fi
if command -v code >/dev/null 2>&1; then
  export VORTEX_VSCODE="$(command -v code)"
fi
JAVA_CMD=""
if [ -x "$PWD/jdk/bin/java" ]; then
  JAVA_CMD="$PWD/jdk/bin/java"
elif [ -x "$PWD/java/bin/java" ]; then
  JAVA_CMD="$PWD/java/bin/java"
elif command -v java >/dev/null 2>&1; then
  JAVA_CMD="$(command -v java)"
fi
verify_file() {
  file_path="$1"
  expected_hash="$(awk -v p="$file_path" '$2 == p {print $1}' checksums.sha256 2>/dev/null)"
  if [ -z "$expected_hash" ]; then
    echo "Integrity check failed: missing checksum for $file_path."
    echo "Download a fresh copy from https://www.vortexevolutions.com"
    printf "Press Enter to close."
    read answer
    exit 1
  fi
  if command -v sha256sum >/dev/null 2>&1; then
    actual_hash="$(sha256sum "$file_path" | awk '{print $1}')"
  elif command -v shasum >/dev/null 2>&1; then
    actual_hash="$(shasum -a 256 "$file_path" | awk '{print $1}')"
  else
    echo "Integrity check failed: no SHA-256 tool found."
    echo "Install sha256sum or shasum, then start Vortex again."
    printf "Press Enter to close."
    read answer
    exit 1
  fi
  if [ "$expected_hash" != "$actual_hash" ]; then
    echo "SECURITY WARNING: $file_path has been altered."
    echo "Vortex will not run this modified file."
    echo "Download a fresh copy from https://www.vortexevolutions.com"
    printf "Press Enter to close."
    read answer
    exit 1
  fi
}
verify_file "app/VortexEvolutions.jar"
verify_file "python/yotavon_agency_bridge.py"
if [ -z "$JAVA_CMD" ]; then
  echo "Java is not installed or not on PATH."
  echo "Install Java 17+ or use the web portal: https://vortexevolutions.com"
  printf "Press Enter to close."
  read answer
  exit 1
fi
"$JAVA_CMD" -jar app/VortexEvolutions.jar
printf "\nGame closed. Press Enter to close."
read answer
