#!/bin/bash

# Set the current directory (modify if needed)
WP_ROOT="/web/html/outrightsystems.org/"

# Define the list of required WordPress core files
REQUIRED_FILES=(
  "index.php"
  "wp-config.php"
  "wp-settings.php"
  "wp-load.php"
  "wp-blog-header.php"
  "wp-cron.php"
  "wp-links-opml.php"
  "wp-login.php"
  "wp-signup.php"
  "wp-activate.php"
  "wp-trackback.php"
  "xmlrpc.php"
  "wordfence-waf.php"
  "wp-comments-post.php"
  "wp-mail.php"
  '.htaccess'
)
shopt -s dotglob
# Variable to store deleted file names
DELETED_FILES=""

# Remove all files except the required ones (excluding directories)
for file in "$WP_ROOT"/*; do
  if [[ -f "$file" ]]; then  # Ensure it's a file, not a directory
    filename=$(basename "$file")
    if [[ ! " ${REQUIRED_FILES[@]} " =~ " $filename " ]]; then
      echo "Removing: $filename"
      DELETED_FILES+="$filename\n"
      rm -f "$file"
    fi
  fi
done

echo "Cleanup complete. Only required WordPress files are kept."

if [[ -n "$DELETED_FILES" ]]; then  
echo "Site was modifield, restoring now"
EMAIL_API_URL="https://crm.outrightsystems.org/index.php?entryPoint=send_email&api_key=hr8jobGGDGXCanKKZuRSKfQAiHkhyHEyOZO9r"
EMAIL_JSON=$(cat <<EOF
{
    "recipients": ["ashish@outrightcrm.com"],
    "subject": "Cleanup Completed for outrightsystems.org",
    "body": "The following files have been deleted from $WP_ROOT:\n\n$DELETED_FILES",
    "attachments": [],
    "username": "outrightcrm19@gmail.com",
    "password": "whsbchnnqizapajq"
}
EOF
)
curl -X POST -H "Content-Type: application/json" -d "$EMAIL_JSON" "$EMAIL_API_URL"
echo "Email notification sent!"
else
  echo "No file found to change."
fi