#!/bin/bash

# Define the JSON content
jsonContent='{
  "autoload": {
    "classmap": [
      "includes/vendor_prefixed/"
    ],
    "files": [
      "includes/vendor_prefixed/guzzlehttp/guzzle/src/functions_include.php"
    ]
  }
}'

# Write the JSON content to a new JSON file
echo "$jsonContent" > new_autoload.json

# Detect the OS
OS="`uname`"
case $OS in
  'Linux')
    OS='Linux'
    ;;
  'Darwin')
    OS='Mac'
    ;;
  *) ;;
esac

# Install jq if it's not already installed
if ! [ -x "$(command -v jq)" ]; then
  echo "jq is not installed. Installing..."
  case $OS in
    'Linux')
      # Detect Linux distro
      if [ -n "$(command -v apt-get)" ]; then
        sudo apt-get update
        sudo apt-get install -y jq
      elif [ -n "$(command -v yum)" ]; then
        sudo yum install -y jq
      fi
      ;;
    'Mac')
      brew install jq
      ;;
    *)
      echo "Unsupported OS. Please install jq manually."
      exit 1
  esac
fi

# Use jq to merge the new_autoload.json into the existing composer.json
jq -s '.[0] * .[1]' composer.json new_autoload.json > composer_updated.json

# Replace the original composer.json file with the updated one
mv composer_updated.json composer.json

# Remove the new_autoload.json file
rm new_autoload.json

rm -rf ./includes/vendor/{firebase,google,guzzlehttp,league,masbug,monolog,paragonie,phpseclib,psr,thecodingmachine}/
rm -rf ./includes/vendor/forgravity/gf-addon-skeleton/

composer dump-autoload
